blob: e80c6a6bba4df6d754862d6e35bd2c8c7c2292b5 [file] [log] [blame]
Dave Airlie785b93e2009-08-28 15:46:53 +10001/*
2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 *
6 * DRM framebuffer helper functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
29 */
Sachin Kamatd56b1b92012-11-15 03:43:29 +000030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Noralf Trønnescfe63422016-08-23 13:54:06 +020032#include <linux/console.h>
Noralf Trønnesd5365402018-07-03 18:03:48 +020033#include <linux/dma-buf.h>
Andy Shevchenko3b40a442010-02-02 14:40:32 -080034#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100035#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040037#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/drmP.h>
39#include <drm/drm_crtc.h>
40#include <drm/drm_fb_helper.h>
41#include <drm/drm_crtc_helper.h>
Rob Clarkbbb1e522015-08-25 15:35:58 -040042#include <drm/drm_atomic.h>
43#include <drm/drm_atomic_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100044
Hans de Goede8f0cb412017-11-25 20:35:50 +010045#include "drm_crtc_internal.h"
Ville Syrjälä699fbee2016-09-19 16:33:44 +030046#include "drm_crtc_helper_internal.h"
47
Daniel Vetterf64c5572015-08-25 15:45:13 +020048static bool drm_fbdev_emulation = true;
49module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
50MODULE_PARM_DESC(fbdev_emulation,
51 "Enable legacy fbdev emulation [default=true]");
52
Xinliang Liu5f152572017-02-15 17:19:08 +010053static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
54module_param(drm_fbdev_overalloc, int, 0444);
55MODULE_PARM_DESC(drm_fbdev_overalloc,
56 "Overallocation of the fbdev buffer (%) [default="
57 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
58
Neil Armstrong4be9bd12018-09-28 14:05:55 +020059/*
60 * In order to keep user-space compatibility, we want in certain use-cases
61 * to keep leaking the fbdev physical address to the user-space program
62 * handling the fbdev buffer.
63 * This is a bad habit essentially kept into closed source opengl driver
64 * that should really be moved into open-source upstream projects instead
65 * of using legacy physical addresses in user space to communicate with
66 * other out-of-tree kernel modules.
67 *
68 * This module_param *should* be removed as soon as possible and be
69 * considered as a broken and legacy behaviour from a modern fbdev device.
70 */
71#if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
72static bool drm_leak_fbdev_smem = false;
73module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
Wei Yongjunb31a3ca2018-12-04 06:32:15 +000074MODULE_PARM_DESC(drm_leak_fbdev_smem,
Neil Armstrong4be9bd12018-09-28 14:05:55 +020075 "Allow unsafe leaking fbdev physical smem address [default=false]");
76#endif
77
Dave Airlie785b93e2009-08-28 15:46:53 +100078static LIST_HEAD(kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +000079static DEFINE_MUTEX(kernel_fb_helper_lock);
Dave Airlie785b93e2009-08-28 15:46:53 +100080
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010081/**
82 * DOC: fbdev helpers
83 *
84 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020085 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010086 * helper functions used by many drivers to implement the kernel mode setting
87 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010088 *
Noralf Trønnes9060d7f2018-07-03 18:03:52 +020089 * Drivers that support a dumb buffer with a virtual address and mmap support,
90 * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
91 *
Noralf Trønnes95b01372017-12-15 18:51:16 +010092 * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it
93 * down by calling drm_fb_helper_fbdev_teardown().
Daniel Vetter207fd322013-01-20 22:13:14 +010094 *
Noralf Trønnes95b01372017-12-15 18:51:16 +010095 * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use
96 * the setup helper and will need to do the whole four-step setup process with
97 * drm_fb_helper_prepare(), drm_fb_helper_init(),
98 * drm_fb_helper_single_add_all_connectors(), enable hotplugging and
99 * drm_fb_helper_initial_config() to avoid a possible race window.
100 *
101 * At runtime drivers should restore the fbdev console by using
102 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
103 * They should also notify the fb helper code from updates to the output
104 * configuration by using drm_fb_helper_output_poll_changed() as their
105 * &drm_mode_config_funcs.output_poll_changed callback.
106 *
107 * For suspend/resume consider using drm_mode_config_helper_suspend() and
108 * drm_mode_config_helper_resume() which takes care of fbdev as well.
Daniel Vetter207fd322013-01-20 22:13:14 +0100109 *
110 * All other functions exported by the fb helper library can be used to
111 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +0200112 *
113 * It is possible, though perhaps somewhat tricky, to implement race-free
114 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
115 * helper must be called first to initialize the minimum required to make
116 * hotplug detection work. Drivers also need to make sure to properly set up
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100117 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
Thierry Reding10a23102014-06-27 17:19:24 +0200118 * it is safe to enable interrupts and start processing hotplug events. At the
119 * same time, drivers should initialize all modeset objects such as CRTCs,
120 * encoders and connectors. To finish up the fbdev helper initialization, the
121 * drm_fb_helper_init() function is called. To probe for all attached displays
122 * and set up an initial configuration using the detected hardware, drivers
123 * should call drm_fb_helper_single_add_all_connectors() followed by
124 * drm_fb_helper_initial_config().
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200125 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100126 * If &drm_framebuffer_funcs.dirty is set, the
Noralf Trønnes2dad5512016-05-11 18:09:17 +0200127 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100128 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
Noralf Trønnes2dad5512016-05-11 18:09:17 +0200129 * away. This worker then calls the dirty() function ensuring that it will
130 * always run in process context since the fb_*() function could be running in
131 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
132 * callback it will also schedule dirty_work with the damage collected from the
Noralf Trønnes48c95712017-12-15 18:51:17 +0100133 * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup
134 * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()).
Daniel Vetterd0ddc0332012-11-01 14:45:17 +0100135 */
136
Chris Wilson966a6a12016-11-29 12:02:15 +0000137#define drm_fb_helper_for_each_connector(fbh, i__) \
Daniel Vettere13a0582017-07-05 06:56:29 +0200138 for (({ lockdep_assert_held(&(fbh)->lock); }), \
Chris Wilson966a6a12016-11-29 12:02:15 +0000139 i__ = 0; i__ < (fbh)->connector_count; i__++)
140
Thierry Redingaf2405a2017-07-04 17:18:21 +0200141static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
142 struct drm_connector *connector)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200143{
Thierry Reding50021ff2017-03-29 16:43:53 +0200144 struct drm_fb_helper_connector *fb_conn;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200145 struct drm_fb_helper_connector **temp;
Thierry Reding50021ff2017-03-29 16:43:53 +0200146 unsigned int count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200147
148 if (!drm_fbdev_emulation)
149 return 0;
150
Thierry Redinge9827d82017-07-04 17:18:23 +0200151 lockdep_assert_held(&fb_helper->lock);
Thierry Reding50021ff2017-03-29 16:43:53 +0200152
153 count = fb_helper->connector_count + 1;
154
155 if (count > fb_helper->connector_info_alloc_count) {
156 size_t size = count * sizeof(fb_conn);
157
158 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200159 if (!temp)
160 return -ENOMEM;
161
Thierry Reding50021ff2017-03-29 16:43:53 +0200162 fb_helper->connector_info_alloc_count = count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200163 fb_helper->connector_info = temp;
164 }
165
Thierry Reding50021ff2017-03-29 16:43:53 +0200166 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
167 if (!fb_conn)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200168 return -ENOMEM;
169
170 drm_connector_get(connector);
Thierry Reding50021ff2017-03-29 16:43:53 +0200171 fb_conn->connector = connector;
172 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
Thierry Redingaf2405a2017-07-04 17:18:21 +0200173
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200174 return 0;
175}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200176
177int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
178 struct drm_connector *connector)
179{
180 int err;
181
Noralf Trønnesc7779902017-10-30 16:39:37 +0100182 if (!fb_helper)
183 return 0;
184
Thierry Redinge9827d82017-07-04 17:18:23 +0200185 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200186 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200187 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200188
189 return err;
190}
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200191EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
192
Daniel Vetter207fd322013-01-20 22:13:14 +0100193/**
194 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
195 * emulation helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100196 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
Daniel Vetter207fd322013-01-20 22:13:14 +0100197 *
198 * This functions adds all the available connectors for use with the given
199 * fb_helper. This is a separate step to allow drivers to freely assign
200 * connectors to the fbdev, e.g. if some are reserved for special purposes or
201 * not adequate to be used for the fbcon.
202 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200203 * This function is protected against concurrent connector hotadds/removals
204 * using drm_fb_helper_add_one_connector() and
205 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100206 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000207int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000208{
Gustavo A. R. Silva89f3f352017-12-05 11:46:28 -0600209 struct drm_device *dev;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000210 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100211 struct drm_connector_list_iter conn_iter;
212 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000213
Noralf Trønnesc7779902017-10-30 16:39:37 +0100214 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200215 return 0;
216
Gustavo A. R. Silva89f3f352017-12-05 11:46:28 -0600217 dev = fb_helper->dev;
218
Thierry Redinge9827d82017-07-04 17:18:23 +0200219 mutex_lock(&fb_helper->lock);
Thierry Redingb982dab2017-02-28 15:46:43 +0100220 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100221 drm_for_each_connector_iter(connector, &conn_iter) {
Paul Kocialkowski8fd3b902018-11-15 17:32:48 +0100222 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
223 continue;
224
Thierry Redingaf2405a2017-07-04 17:18:21 +0200225 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100226 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000227 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000228 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100229 goto out;
230
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000231fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000232 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300233 struct drm_fb_helper_connector *fb_helper_connector =
234 fb_helper->connector_info[i];
235
Thierry Redingad093602017-02-28 15:46:39 +0100236 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300237
238 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000239 fb_helper->connector_info[i] = NULL;
240 }
241 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100242out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100243 drm_connector_list_iter_end(&conn_iter);
Thierry Redinge9827d82017-07-04 17:18:23 +0200244 mutex_unlock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200245
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100246 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000247}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000248EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000249
Thierry Redingaf2405a2017-07-04 17:18:21 +0200250static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
251 struct drm_connector *connector)
Dave Airlie65c2a892014-06-05 14:01:30 +1000252{
253 struct drm_fb_helper_connector *fb_helper_connector;
254 int i, j;
255
Daniel Vetterf64c5572015-08-25 15:45:13 +0200256 if (!drm_fbdev_emulation)
257 return 0;
258
Thierry Redinge9827d82017-07-04 17:18:23 +0200259 lockdep_assert_held(&fb_helper->lock);
Dave Airlie65c2a892014-06-05 14:01:30 +1000260
Thierry Redinge9827d82017-07-04 17:18:23 +0200261 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie65c2a892014-06-05 14:01:30 +1000262 if (fb_helper->connector_info[i]->connector == connector)
263 break;
264 }
265
266 if (i == fb_helper->connector_count)
267 return -EINVAL;
268 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100269 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000270
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200271 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000272 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200273
Dave Airlie65c2a892014-06-05 14:01:30 +1000274 fb_helper->connector_count--;
275 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500276
Dave Airlie65c2a892014-06-05 14:01:30 +1000277 return 0;
278}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200279
280int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
281 struct drm_connector *connector)
282{
283 int err;
284
Noralf Trønnesc7779902017-10-30 16:39:37 +0100285 if (!fb_helper)
286 return 0;
287
Thierry Redinge9827d82017-07-04 17:18:23 +0200288 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200289 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200290 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200291
292 return err;
293}
Dave Airlie65c2a892014-06-05 14:01:30 +1000294EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
295
Jason Wessel99231022010-10-13 14:09:43 -0500296static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
297{
298 uint16_t *r_base, *g_base, *b_base;
299
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200300 if (crtc->funcs->gamma_set == NULL)
301 return;
302
Jason Wessel99231022010-10-13 14:09:43 -0500303 r_base = crtc->gamma_store;
304 g_base = r_base + crtc->gamma_size;
305 b_base = g_base + crtc->gamma_size;
306
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200307 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
308 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500309}
310
Daniel Vetter207fd322013-01-20 22:13:14 +0100311/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100312 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100313 * @info: fbdev registered by the helper
314 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500315int drm_fb_helper_debug_enter(struct fb_info *info)
316{
317 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200318 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500319 int i;
320
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500321 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
322 for (i = 0; i < helper->crtc_count; i++) {
323 struct drm_mode_set *mode_set =
324 &helper->crtc_info[i].mode_set;
325
326 if (!mode_set->crtc->enabled)
327 continue;
328
329 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100330 if (funcs->mode_set_base_atomic == NULL)
331 continue;
332
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200333 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
334 continue;
335
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500336 funcs->mode_set_base_atomic(mode_set->crtc,
337 mode_set->fb,
338 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500339 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500340 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500341 }
342 }
343
344 return 0;
345}
346EXPORT_SYMBOL(drm_fb_helper_debug_enter);
347
Daniel Vetter207fd322013-01-20 22:13:14 +0100348/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100349 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100350 * @info: fbdev registered by the helper
351 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500352int drm_fb_helper_debug_leave(struct fb_info *info)
353{
354 struct drm_fb_helper *helper = info->par;
355 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200356 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500357 struct drm_framebuffer *fb;
358 int i;
359
360 for (i = 0; i < helper->crtc_count; i++) {
361 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200362
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500363 crtc = mode_set->crtc;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200364 if (drm_drv_uses_atomic_modeset(crtc->dev))
365 continue;
366
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500367 funcs = crtc->helper_private;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200368 fb = crtc->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500369
370 if (!crtc->enabled)
371 continue;
372
373 if (!fb) {
374 DRM_ERROR("no fb to restore??\n");
375 continue;
376 }
377
Stefan Christ1b99b722016-11-14 00:03:11 +0100378 if (funcs->mode_set_base_atomic == NULL)
379 continue;
380
Jason Wessel99231022010-10-13 14:09:43 -0500381 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500382 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500383 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500384 }
385
386 return 0;
387}
388EXPORT_SYMBOL(drm_fb_helper_debug_leave);
389
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200390static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
Rob Clarkbbb1e522015-08-25 15:35:58 -0400391{
392 struct drm_device *dev = fb_helper->dev;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100393 struct drm_plane_state *plane_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400394 struct drm_plane *plane;
395 struct drm_atomic_state *state;
396 int i, ret;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200397 struct drm_modeset_acquire_ctx ctx;
398
399 drm_modeset_acquire_init(&ctx, 0);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400400
401 state = drm_atomic_state_alloc(dev);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200402 if (!state) {
403 ret = -ENOMEM;
404 goto out_ctx;
405 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400406
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200407 state->acquire_ctx = &ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400408retry:
409 drm_for_each_plane(plane, dev) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400410 plane_state = drm_atomic_get_plane_state(state, plane);
411 if (IS_ERR(plane_state)) {
412 ret = PTR_ERR(plane_state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200413 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400414 }
415
Robert Fossc2c446a2017-05-19 16:50:17 -0400416 plane_state->rotation = DRM_MODE_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400417
418 /* disable non-primary: */
419 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
420 continue;
421
422 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
423 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200424 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400425 }
426
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200427 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400428 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100429 struct drm_plane *primary = mode_set->crtc->primary;
430
431 /* Cannot fail as we've already gotten the plane state above */
432 plane_state = drm_atomic_get_new_plane_state(state, primary);
433 plane_state->rotation = fb_helper->crtc_info[i].rotation;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400434
435 ret = __drm_atomic_helper_set_config(mode_set, state);
436 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200437 goto out_state;
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200438
439 /*
440 * __drm_atomic_helper_set_config() sets active when a
441 * mode is set, unconditionally clear it if we force DPMS off
442 */
443 if (!active) {
444 struct drm_crtc *crtc = mode_set->crtc;
445 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
446
447 crtc_state->active = false;
448 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400449 }
450
451 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400452
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200453out_state:
Rob Clarkbbb1e522015-08-25 15:35:58 -0400454 if (ret == -EDEADLK)
455 goto backoff;
456
Chris Wilson08536952016-10-14 13:18:18 +0100457 drm_atomic_state_put(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200458out_ctx:
459 drm_modeset_drop_locks(&ctx);
460 drm_modeset_acquire_fini(&ctx);
461
Rob Clarkbbb1e522015-08-25 15:35:58 -0400462 return ret;
463
464backoff:
465 drm_atomic_state_clear(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200466 drm_modeset_backoff(&ctx);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400467
468 goto retry;
469}
470
Daniel Vetter71286452017-04-03 10:33:04 +0200471static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100472{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300473 struct drm_device *dev = fb_helper->dev;
474 struct drm_plane *plane;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200475 int i, ret = 0;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100476
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200477 drm_modeset_lock_all(fb_helper->dev);
Daniel Vetter6295d602015-07-09 23:44:25 +0200478 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700479 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
480 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100481
Ville Syrjälä6686df82016-10-21 22:22:45 +0300482 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300483 drm_mode_plane_set_obj_prop(plane,
484 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400485 DRM_MODE_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530486 }
487
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100488 for (i = 0; i < fb_helper->crtc_count; i++) {
489 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300490 struct drm_crtc *crtc = mode_set->crtc;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300491
Alex Deucher03f9abb2015-09-30 14:47:37 -0400492 if (crtc->funcs->cursor_set2) {
493 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
494 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200495 goto out;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400496 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300497 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
498 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200499 goto out;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300500 }
501
Daniel Vetter2d13b672012-12-11 13:47:23 +0100502 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100503 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200504 goto out;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100505 }
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200506out:
507 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200508
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200509 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100510}
Rob Clark5ea1f752014-05-30 12:29:48 -0400511
Daniel Vetter71286452017-04-03 10:33:04 +0200512static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
513{
514 struct drm_device *dev = fb_helper->dev;
515
Daniel Vetter71286452017-04-03 10:33:04 +0200516 if (drm_drv_uses_atomic_modeset(dev))
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200517 return restore_fbdev_mode_atomic(fb_helper, true);
Daniel Vetter71286452017-04-03 10:33:04 +0200518 else
519 return restore_fbdev_mode_legacy(fb_helper);
520}
521
Rob Clark5ea1f752014-05-30 12:29:48 -0400522/**
523 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
Noralf Trønnesc7779902017-10-30 16:39:37 +0100524 * @fb_helper: driver-allocated fbdev helper, can be NULL
Rob Clark5ea1f752014-05-30 12:29:48 -0400525 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100526 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400527 * when implementing an fbcon on top of kms using this helper. This ensures that
528 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200529 *
530 * RETURNS:
531 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400532 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200533int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400534{
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200535 bool do_delayed;
536 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000537
Noralf Trønnesc7779902017-10-30 16:39:37 +0100538 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200539 return -ENODEV;
540
Daniel Vetterca91a272017-07-06 15:00:21 +0200541 if (READ_ONCE(fb_helper->deferred_setup))
542 return 0;
543
Thierry Redinge9827d82017-07-04 17:18:23 +0200544 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400545 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000546
547 do_delayed = fb_helper->delayed_hotplug;
548 if (do_delayed)
549 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200550 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000551
552 if (do_delayed)
553 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200554
Rob Clark5ea1f752014-05-30 12:29:48 -0400555 return ret;
556}
557EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100558
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200559static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
560{
561 struct drm_device *dev = fb_helper->dev;
562 struct drm_crtc *crtc;
563 int bound = 0, crtcs_bound = 0;
564
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200565 /*
566 * Sometimes user space wants everything disabled, so don't steal the
567 * display if there's a master.
568 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200569 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200570 return false;
571
572 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200573 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200574 if (crtc->primary->fb)
575 crtcs_bound++;
576 if (crtc->primary->fb == fb_helper->fb)
577 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200578 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200579 }
580
581 if (bound < crtcs_bound)
582 return false;
583
584 return true;
585}
586
587#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100588/*
589 * restore fbcon display for all kms driver's using this helper, used for sysrq
590 * and panic handling.
591 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530592static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000593{
Dave Airlie785b93e2009-08-28 15:46:53 +1000594 bool ret, error = false;
595 struct drm_fb_helper *helper;
596
597 if (list_empty(&kernel_fb_helper_list))
598 return false;
599
600 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200601 struct drm_device *dev = helper->dev;
602
603 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100604 continue;
605
Thierry Redinge9827d82017-07-04 17:18:23 +0200606 mutex_lock(&helper->lock);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200607 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100608 if (ret)
609 error = true;
Thierry Redinge9827d82017-07-04 17:18:23 +0200610 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000611 }
612 return error;
613}
614
Dave Airlie785b93e2009-08-28 15:46:53 +1000615static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
616{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100617 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200618
Daniel Vetterd21bf462013-01-20 18:09:52 +0100619 ret = drm_fb_helper_force_kernel_mode();
620 if (ret == true)
621 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000622}
623static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
624
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700625static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000626{
627 schedule_work(&drm_fb_helper_restore_work);
628}
629
630static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
631 .handler = drm_fb_helper_sysrq,
632 .help_msg = "force-fb(V)",
633 .action_msg = "Restore framebuffer console",
634};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000635#else
636static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200637#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000638
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200639static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000640{
Dave Airlie785b93e2009-08-28 15:46:53 +1000641 struct drm_device *dev = fb_helper->dev;
642 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700643 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700644 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000645
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200646 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700647 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000648 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000649
Dave Airlie8be48d92010-03-30 05:34:14 +0000650 if (!crtc->enabled)
651 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000652
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100653 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000654 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700655 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200656 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500657 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100658 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700659 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000660 }
Daniel Vetter84849902012-12-02 00:28:11 +0100661 drm_modeset_unlock_all(dev);
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200662}
663
664static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
665{
666 struct drm_fb_helper *fb_helper = info->par;
667
668 /*
669 * For each CRTC in this fb, turn the connectors on/off.
670 */
671 mutex_lock(&fb_helper->lock);
672 if (!drm_fb_helper_is_bound(fb_helper)) {
673 mutex_unlock(&fb_helper->lock);
674 return;
675 }
676
677 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
678 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
679 else
680 dpms_legacy(fb_helper, dpms_mode);
Thierry Redinge9827d82017-07-04 17:18:23 +0200681 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000682}
683
Daniel Vetter207fd322013-01-20 22:13:14 +0100684/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100685 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100686 * @blank: desired blanking state
687 * @info: fbdev registered by the helper
688 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000689int drm_fb_helper_blank(int blank, struct fb_info *info)
690{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200691 if (oops_in_progress)
692 return -EBUSY;
693
Dave Airlie785b93e2009-08-28 15:46:53 +1000694 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000695 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000696 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100697 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000698 break;
James Simmons731b5a12009-10-29 20:39:07 +0000699 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000700 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100701 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000702 break;
James Simmons731b5a12009-10-29 20:39:07 +0000703 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000704 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100705 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000706 break;
James Simmons731b5a12009-10-29 20:39:07 +0000707 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000708 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100709 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000710 break;
James Simmons731b5a12009-10-29 20:39:07 +0000711 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000712 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100713 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000714 break;
715 }
716 return 0;
717}
718EXPORT_SYMBOL(drm_fb_helper_blank);
719
Ville Syrjäläa2889602016-10-26 17:41:18 +0300720static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
721 struct drm_mode_set *modeset)
722{
723 int i;
724
725 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100726 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300727 modeset->connectors[i] = NULL;
728 }
729 modeset->num_connectors = 0;
730
731 drm_mode_destroy(helper->dev, modeset->mode);
732 modeset->mode = NULL;
733
734 /* FIXME should hold a ref? */
735 modeset->fb = NULL;
736}
737
Dave Airlie785b93e2009-08-28 15:46:53 +1000738static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
739{
740 int i;
741
Dave Airlie6e86d582016-04-27 11:24:51 +1000742 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100743 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000744 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000745 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000746 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300747
Sascha Hauera1b77362012-02-01 11:38:22 +0100748 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300749 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
750
751 drm_fb_helper_modeset_release(helper, modeset);
752 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100753 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000754 kfree(helper->crtc_info);
755}
756
Noralf Trønnescfe63422016-08-23 13:54:06 +0200757static void drm_fb_helper_resume_worker(struct work_struct *work)
758{
759 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
760 resume_work);
761
762 console_lock();
763 fb_set_suspend(helper->fbdev, 0);
764 console_unlock();
765}
766
Noralf Trønnesd5365402018-07-03 18:03:48 +0200767static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper,
768 struct drm_clip_rect *clip)
769{
770 struct drm_framebuffer *fb = fb_helper->fb;
771 unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
772 size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp;
773 void *src = fb_helper->fbdev->screen_buffer + offset;
774 void *dst = fb_helper->buffer->vaddr + offset;
775 size_t len = (clip->x2 - clip->x1) * cpp;
776 unsigned int y;
777
778 for (y = clip->y1; y < clip->y2; y++) {
779 memcpy(dst, src, len);
780 src += fb->pitches[0];
781 dst += fb->pitches[0];
782 }
783}
784
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200785static void drm_fb_helper_dirty_work(struct work_struct *work)
786{
787 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
788 dirty_work);
789 struct drm_clip_rect *clip = &helper->dirty_clip;
790 struct drm_clip_rect clip_copy;
791 unsigned long flags;
792
793 spin_lock_irqsave(&helper->dirty_lock, flags);
794 clip_copy = *clip;
795 clip->x1 = clip->y1 = ~0;
796 clip->x2 = clip->y2 = 0;
797 spin_unlock_irqrestore(&helper->dirty_lock, flags);
798
Takashi Iwai87d3b652016-10-20 17:05:30 +0200799 /* call dirty callback only when it has been really touched */
Noralf Trønnesd5365402018-07-03 18:03:48 +0200800 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) {
801 /* Generic fbdev uses a shadow buffer */
802 if (helper->buffer)
803 drm_fb_helper_dirty_blit_real(helper, &clip_copy);
Takashi Iwai87d3b652016-10-20 17:05:30 +0200804 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønnesd5365402018-07-03 18:03:48 +0200805 }
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200806}
807
Daniel Vetter207fd322013-01-20 22:13:14 +0100808/**
Thierry Reding10a23102014-06-27 17:19:24 +0200809 * drm_fb_helper_prepare - setup a drm_fb_helper structure
810 * @dev: DRM device
811 * @helper: driver-allocated fbdev helper structure to set up
812 * @funcs: pointer to structure of functions associate with this helper
813 *
814 * Sets up the bare minimum to make the framebuffer helper usable. This is
815 * useful to implement race-free initialization of the polling helpers.
816 */
817void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
818 const struct drm_fb_helper_funcs *funcs)
819{
820 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200821 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200822 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200823 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
824 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200825 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200826 helper->funcs = funcs;
827 helper->dev = dev;
828}
829EXPORT_SYMBOL(drm_fb_helper_prepare);
830
831/**
Daniel Vettered84e252017-02-07 15:10:49 +0100832 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100833 * @dev: drm device
834 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100835 * @max_conn_count: max connector count
836 *
837 * This allocates the structures for the fbdev helper with the given limits.
838 * Note that this won't yet touch the hardware (through the driver interfaces)
839 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
840 * to allow driver writes more control over the exact init sequence.
841 *
Thierry Reding10a23102014-06-27 17:19:24 +0200842 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100843 *
844 * RETURNS:
845 * Zero if everything went ok, nonzero otherwise.
846 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000847int drm_fb_helper_init(struct drm_device *dev,
848 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200849 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000850{
Dave Airlie785b93e2009-08-28 15:46:53 +1000851 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200852 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000853 int i;
854
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100855 if (!drm_fbdev_emulation) {
856 dev->fb_helper = fb_helper;
Daniel Vetterf64c5572015-08-25 15:45:13 +0200857 return 0;
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100858 }
Daniel Vetterf64c5572015-08-25 15:45:13 +0200859
Xiubo Li04cfe972014-03-10 09:33:58 +0800860 if (!max_conn_count)
861 return -EINVAL;
862
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200863 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000864 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000865 return -ENOMEM;
866
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200867 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000868 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
869 if (!fb_helper->connector_info) {
870 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000871 return -ENOMEM;
872 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000873 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000874 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000875
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200876 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000877 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000878 kcalloc(max_conn_count,
879 sizeof(struct drm_connector *),
880 GFP_KERNEL);
881
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200882 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000883 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000884 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100885 fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000886 }
887
888 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200889 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000890 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000891 i++;
892 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100893
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100894 dev->fb_helper = fb_helper;
895
Dave Airlie785b93e2009-08-28 15:46:53 +1000896 return 0;
897out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000898 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000899 return -ENOMEM;
900}
Dave Airlie4abe3522010-03-30 05:34:18 +0000901EXPORT_SYMBOL(drm_fb_helper_init);
902
Archit Tanejab8017d62015-07-22 14:57:56 +0530903/**
904 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
905 * @fb_helper: driver-allocated fbdev helper
906 *
907 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100908 * by the driver within the fb_probe fb_helper callback function. Drivers do not
909 * need to release the allocated fb_info structure themselves, this is
910 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530911 *
912 * RETURNS:
913 * fb_info pointer if things went okay, pointer containing error code
914 * otherwise
915 */
916struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
917{
918 struct device *dev = fb_helper->dev->dev;
919 struct fb_info *info;
920 int ret;
921
922 info = framebuffer_alloc(0, dev);
923 if (!info)
924 return ERR_PTR(-ENOMEM);
925
926 ret = fb_alloc_cmap(&info->cmap, 256, 0);
927 if (ret)
928 goto err_release;
929
930 info->apertures = alloc_apertures(1);
931 if (!info->apertures) {
932 ret = -ENOMEM;
933 goto err_free_cmap;
934 }
935
936 fb_helper->fbdev = info;
Daniel Vetter8782c642018-11-27 18:34:24 +0100937 info->skip_vt_switch = true;
Archit Tanejab8017d62015-07-22 14:57:56 +0530938
939 return info;
940
941err_free_cmap:
942 fb_dealloc_cmap(&info->cmap);
943err_release:
944 framebuffer_release(info);
945 return ERR_PTR(ret);
946}
947EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
948
949/**
950 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
Noralf Trønnesc7779902017-10-30 16:39:37 +0100951 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Tanejab8017d62015-07-22 14:57:56 +0530952 *
953 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100954 * framebuffer device. This must be called before releasing all resources for
955 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530956 */
957void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
958{
959 if (fb_helper && fb_helper->fbdev)
960 unregister_framebuffer(fb_helper->fbdev);
961}
962EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
963
964/**
Daniel Vettered84e252017-02-07 15:10:49 +0100965 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100966 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vettered84e252017-02-07 15:10:49 +0100967 *
968 * This cleans up all remaining resources associated with @fb_helper. Must be
969 * called after drm_fb_helper_unlink_fbi() was called.
970 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000971void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
972{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100973 struct fb_info *info;
974
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100975 if (!fb_helper)
976 return;
977
978 fb_helper->dev->fb_helper = NULL;
979
980 if (!drm_fbdev_emulation)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200981 return;
982
Noralf Trønnesb52f09c2017-08-28 19:17:43 +0200983 cancel_work_sync(&fb_helper->resume_work);
984 cancel_work_sync(&fb_helper->dirty_work);
985
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100986 info = fb_helper->fbdev;
987 if (info) {
988 if (info->cmap.len)
989 fb_dealloc_cmap(&info->cmap);
990 framebuffer_release(info);
991 }
992 fb_helper->fbdev = NULL;
993
Chris Wilsona53ca632016-11-29 12:02:17 +0000994 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000995 if (!list_empty(&fb_helper->kernel_fb_list)) {
996 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200997 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000998 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000999 }
Chris Wilsona53ca632016-11-29 12:02:17 +00001000 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +00001001
Thierry Redinge9827d82017-07-04 17:18:23 +02001002 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +00001003 drm_fb_helper_crtc_free(fb_helper);
1004
Dave Airlie4abe3522010-03-30 05:34:18 +00001005}
1006EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +10001007
Archit Taneja47074ab2015-07-22 14:57:57 +05301008/**
1009 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
Noralf Trønnesc7779902017-10-30 16:39:37 +01001010 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Taneja47074ab2015-07-22 14:57:57 +05301011 *
1012 * A wrapper around unlink_framebuffer implemented by fbdev core
1013 */
1014void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
1015{
1016 if (fb_helper && fb_helper->fbdev)
1017 unlink_framebuffer(fb_helper->fbdev);
1018}
1019EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
1020
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001021static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
1022 u32 width, u32 height)
1023{
1024 struct drm_fb_helper *helper = info->par;
1025 struct drm_clip_rect *clip = &helper->dirty_clip;
1026 unsigned long flags;
1027
1028 if (!helper->fb->funcs->dirty)
1029 return;
1030
1031 spin_lock_irqsave(&helper->dirty_lock, flags);
1032 clip->x1 = min_t(u32, clip->x1, x);
1033 clip->y1 = min_t(u32, clip->y1, y);
1034 clip->x2 = max_t(u32, clip->x2, x + width);
1035 clip->y2 = max_t(u32, clip->y2, y + height);
1036 spin_unlock_irqrestore(&helper->dirty_lock, flags);
1037
1038 schedule_work(&helper->dirty_work);
1039}
1040
1041/**
1042 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
1043 * @info: fb_info struct pointer
1044 * @pagelist: list of dirty mmap framebuffer pages
1045 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001046 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001047 * callback function for flushing the fbdev mmap writes.
1048 */
1049void drm_fb_helper_deferred_io(struct fb_info *info,
1050 struct list_head *pagelist)
1051{
1052 unsigned long start, end, min, max;
1053 struct page *page;
1054 u32 y1, y2;
1055
1056 min = ULONG_MAX;
1057 max = 0;
1058 list_for_each_entry(page, pagelist, lru) {
1059 start = page->index << PAGE_SHIFT;
1060 end = start + PAGE_SIZE - 1;
1061 min = min(min, start);
1062 max = max(max, end);
1063 }
1064
1065 if (min < max) {
1066 y1 = min / info->fix.line_length;
1067 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1068 info->var.yres);
1069 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1070 }
1071}
1072EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1073
Archit Tanejacbb1a822015-07-31 16:21:41 +05301074/**
Noralf Trønnes48c95712017-12-15 18:51:17 +01001075 * drm_fb_helper_defio_init - fbdev deferred I/O initialization
1076 * @fb_helper: driver-allocated fbdev helper
1077 *
1078 * This function allocates &fb_deferred_io, sets callback to
1079 * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init().
1080 * It should be called from the &drm_fb_helper_funcs->fb_probe callback.
1081 * drm_fb_helper_fbdev_teardown() cleans up deferred I/O.
1082 *
1083 * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done
1084 * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby
1085 * affect other instances of that &fb_ops.
1086 *
1087 * Returns:
1088 * 0 on success or a negative error code on failure.
1089 */
1090int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper)
1091{
1092 struct fb_info *info = fb_helper->fbdev;
1093 struct fb_deferred_io *fbdefio;
1094 struct fb_ops *fbops;
1095
1096 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
1097 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
1098 if (!fbdefio || !fbops) {
1099 kfree(fbdefio);
1100 kfree(fbops);
1101 return -ENOMEM;
1102 }
1103
1104 info->fbdefio = fbdefio;
1105 fbdefio->delay = msecs_to_jiffies(50);
1106 fbdefio->deferred_io = drm_fb_helper_deferred_io;
1107
1108 *fbops = *info->fbops;
1109 info->fbops = fbops;
1110
1111 fb_deferred_io_init(info);
1112
1113 return 0;
1114}
1115EXPORT_SYMBOL(drm_fb_helper_defio_init);
1116
1117/**
Archit Tanejacbb1a822015-07-31 16:21:41 +05301118 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1119 * @info: fb_info struct pointer
1120 * @buf: userspace buffer to read from framebuffer memory
1121 * @count: number of bytes to read from framebuffer memory
1122 * @ppos: read offset within framebuffer memory
1123 *
1124 * A wrapper around fb_sys_read implemented by fbdev core
1125 */
1126ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1127 size_t count, loff_t *ppos)
1128{
1129 return fb_sys_read(info, buf, count, ppos);
1130}
1131EXPORT_SYMBOL(drm_fb_helper_sys_read);
1132
1133/**
1134 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1135 * @info: fb_info struct pointer
1136 * @buf: userspace buffer to write to framebuffer memory
1137 * @count: number of bytes to write to framebuffer memory
1138 * @ppos: write offset within framebuffer memory
1139 *
1140 * A wrapper around fb_sys_write implemented by fbdev core
1141 */
1142ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1143 size_t count, loff_t *ppos)
1144{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001145 ssize_t ret;
1146
1147 ret = fb_sys_write(info, buf, count, ppos);
1148 if (ret > 0)
1149 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1150 info->var.yres);
1151
1152 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301153}
1154EXPORT_SYMBOL(drm_fb_helper_sys_write);
1155
Archit Taneja742547b2015-07-31 16:21:42 +05301156/**
1157 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1158 * @info: fbdev registered by the helper
1159 * @rect: info about rectangle to fill
1160 *
1161 * A wrapper around sys_fillrect implemented by fbdev core
1162 */
1163void drm_fb_helper_sys_fillrect(struct fb_info *info,
1164 const struct fb_fillrect *rect)
1165{
1166 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001167 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1168 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301169}
1170EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1171
1172/**
1173 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1174 * @info: fbdev registered by the helper
1175 * @area: info about area to copy
1176 *
1177 * A wrapper around sys_copyarea implemented by fbdev core
1178 */
1179void drm_fb_helper_sys_copyarea(struct fb_info *info,
1180 const struct fb_copyarea *area)
1181{
1182 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001183 drm_fb_helper_dirty(info, area->dx, area->dy,
1184 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301185}
1186EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1187
1188/**
1189 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1190 * @info: fbdev registered by the helper
1191 * @image: info about image to blit
1192 *
1193 * A wrapper around sys_imageblit implemented by fbdev core
1194 */
1195void drm_fb_helper_sys_imageblit(struct fb_info *info,
1196 const struct fb_image *image)
1197{
1198 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001199 drm_fb_helper_dirty(info, image->dx, image->dy,
1200 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301201}
1202EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1203
1204/**
1205 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1206 * @info: fbdev registered by the helper
1207 * @rect: info about rectangle to fill
1208 *
Daniel Vetterd1043282018-05-24 11:01:05 +02001209 * A wrapper around cfb_fillrect implemented by fbdev core
Archit Taneja742547b2015-07-31 16:21:42 +05301210 */
1211void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1212 const struct fb_fillrect *rect)
1213{
1214 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001215 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1216 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301217}
1218EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1219
1220/**
1221 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1222 * @info: fbdev registered by the helper
1223 * @area: info about area to copy
1224 *
1225 * A wrapper around cfb_copyarea implemented by fbdev core
1226 */
1227void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1228 const struct fb_copyarea *area)
1229{
1230 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001231 drm_fb_helper_dirty(info, area->dx, area->dy,
1232 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301233}
1234EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1235
1236/**
1237 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1238 * @info: fbdev registered by the helper
1239 * @image: info about image to blit
1240 *
1241 * A wrapper around cfb_imageblit implemented by fbdev core
1242 */
1243void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1244 const struct fb_image *image)
1245{
1246 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001247 drm_fb_helper_dirty(info, image->dx, image->dy,
1248 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301249}
1250EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1251
Archit Tanejafdefa582015-07-31 16:21:43 +05301252/**
1253 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
Noralf Trønnesc7779902017-10-30 16:39:37 +01001254 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001255 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301256 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001257 * A wrapper around fb_set_suspend implemented by fbdev core.
1258 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1259 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301260 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001261void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301262{
1263 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001264 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301265}
1266EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1267
Noralf Trønnescfe63422016-08-23 13:54:06 +02001268/**
1269 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1270 * takes the console lock
Noralf Trønnesc7779902017-10-30 16:39:37 +01001271 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001272 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001273 *
1274 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1275 * isn't available on resume, a worker is tasked with waiting for the lock
1276 * to become available. The console lock can be pretty contented on resume
1277 * due to all the printk activity.
1278 *
1279 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001280 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001281 *
1282 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1283 */
1284void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001285 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001286{
1287 if (!fb_helper || !fb_helper->fbdev)
1288 return;
1289
1290 /* make sure there's no pending/ongoing resume */
1291 flush_work(&fb_helper->resume_work);
1292
1293 if (suspend) {
1294 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1295 return;
1296
1297 console_lock();
1298
1299 } else {
1300 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1301 return;
1302
1303 if (!console_trylock()) {
1304 schedule_work(&fb_helper->resume_work);
1305 return;
1306 }
1307 }
1308
1309 fb_set_suspend(fb_helper->fbdev, suspend);
1310 console_unlock();
1311}
1312EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1313
Peter Rosinb8e2b012017-07-04 12:36:57 +02001314static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1315{
1316 u32 *palette = (u32 *)info->pseudo_palette;
1317 int i;
1318
1319 if (cmap->start + cmap->len > 16)
1320 return -EINVAL;
1321
1322 for (i = 0; i < cmap->len; ++i) {
1323 u16 red = cmap->red[i];
1324 u16 green = cmap->green[i];
1325 u16 blue = cmap->blue[i];
1326 u32 value;
1327
1328 red >>= 16 - info->var.red.length;
1329 green >>= 16 - info->var.green.length;
1330 blue >>= 16 - info->var.blue.length;
1331 value = (red << info->var.red.offset) |
1332 (green << info->var.green.offset) |
1333 (blue << info->var.blue.offset);
1334 if (info->var.transp.length > 0) {
1335 u32 mask = (1 << info->var.transp.length) - 1;
1336
1337 mask <<= info->var.transp.offset;
1338 value |= mask;
1339 }
1340 palette[cmap->start + i] = value;
1341 }
1342
1343 return 0;
1344}
1345
Peter Rosin964c6002017-07-13 18:25:27 +02001346static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
Dave Airlie068143d2009-10-05 09:58:02 +10001347{
1348 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie068143d2009-10-05 09:58:02 +10001349 struct drm_crtc *crtc;
Peter Rosina3562a02017-07-04 12:36:58 +02001350 u16 *r, *g, *b;
Peter Rosin964c6002017-07-13 18:25:27 +02001351 int i, ret = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001352
Peter Rosin964c6002017-07-13 18:25:27 +02001353 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001354 for (i = 0; i < fb_helper->crtc_count; i++) {
1355 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Peter Rosin964c6002017-07-13 18:25:27 +02001356 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1357 return -EINVAL;
Dave Airlie068143d2009-10-05 09:58:02 +10001358
Peter Rosin964c6002017-07-13 18:25:27 +02001359 if (cmap->start + cmap->len > crtc->gamma_size)
1360 return -EINVAL;
Peter Rosina3562a02017-07-04 12:36:58 +02001361
1362 r = crtc->gamma_store;
1363 g = r + crtc->gamma_size;
1364 b = g + crtc->gamma_size;
1365
1366 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1367 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1368 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1369
Peter Rosin964c6002017-07-13 18:25:27 +02001370 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1371 crtc->gamma_size, NULL);
1372 if (ret)
1373 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001374 }
Peter Rosin964c6002017-07-13 18:25:27 +02001375 drm_modeset_unlock_all(fb_helper->dev);
1376
1377 return ret;
1378}
1379
1380static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1381 struct fb_cmap *cmap)
1382{
1383 struct drm_device *dev = crtc->dev;
1384 struct drm_property_blob *gamma_lut;
1385 struct drm_color_lut *lut;
1386 int size = crtc->gamma_size;
1387 int i;
1388
1389 if (!size || cmap->start + cmap->len > size)
1390 return ERR_PTR(-EINVAL);
1391
1392 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1393 if (IS_ERR(gamma_lut))
1394 return gamma_lut;
1395
Ville Syrjälä11b83e32018-02-23 21:25:02 +02001396 lut = gamma_lut->data;
Peter Rosin964c6002017-07-13 18:25:27 +02001397 if (cmap->start || cmap->len != size) {
1398 u16 *r = crtc->gamma_store;
1399 u16 *g = r + crtc->gamma_size;
1400 u16 *b = g + crtc->gamma_size;
1401
1402 for (i = 0; i < cmap->start; i++) {
1403 lut[i].red = r[i];
1404 lut[i].green = g[i];
1405 lut[i].blue = b[i];
1406 }
1407 for (i = cmap->start + cmap->len; i < size; i++) {
1408 lut[i].red = r[i];
1409 lut[i].green = g[i];
1410 lut[i].blue = b[i];
1411 }
1412 }
1413
1414 for (i = 0; i < cmap->len; i++) {
1415 lut[cmap->start + i].red = cmap->red[i];
1416 lut[cmap->start + i].green = cmap->green[i];
1417 lut[cmap->start + i].blue = cmap->blue[i];
1418 }
1419
1420 return gamma_lut;
1421}
1422
1423static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1424{
1425 struct drm_fb_helper *fb_helper = info->par;
1426 struct drm_device *dev = fb_helper->dev;
1427 struct drm_property_blob *gamma_lut = NULL;
1428 struct drm_modeset_acquire_ctx ctx;
1429 struct drm_crtc_state *crtc_state;
1430 struct drm_atomic_state *state;
1431 struct drm_crtc *crtc;
1432 u16 *r, *g, *b;
1433 int i, ret = 0;
1434 bool replaced;
1435
1436 drm_modeset_acquire_init(&ctx, 0);
1437
1438 state = drm_atomic_state_alloc(dev);
1439 if (!state) {
1440 ret = -ENOMEM;
1441 goto out_ctx;
1442 }
1443
1444 state->acquire_ctx = &ctx;
1445retry:
1446 for (i = 0; i < fb_helper->crtc_count; i++) {
1447 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1448
1449 if (!gamma_lut)
1450 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1451 if (IS_ERR(gamma_lut)) {
1452 ret = PTR_ERR(gamma_lut);
1453 gamma_lut = NULL;
1454 goto out_state;
1455 }
1456
1457 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1458 if (IS_ERR(crtc_state)) {
1459 ret = PTR_ERR(crtc_state);
1460 goto out_state;
1461 }
1462
1463 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1464 NULL);
1465 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1466 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1467 gamma_lut);
1468 crtc_state->color_mgmt_changed |= replaced;
1469 }
1470
1471 ret = drm_atomic_commit(state);
1472 if (ret)
1473 goto out_state;
1474
1475 for (i = 0; i < fb_helper->crtc_count; i++) {
1476 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1477
1478 r = crtc->gamma_store;
1479 g = r + crtc->gamma_size;
1480 b = g + crtc->gamma_size;
1481
1482 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1483 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1484 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1485 }
1486
1487out_state:
1488 if (ret == -EDEADLK)
1489 goto backoff;
1490
1491 drm_property_blob_put(gamma_lut);
1492 drm_atomic_state_put(state);
1493out_ctx:
1494 drm_modeset_drop_locks(&ctx);
1495 drm_modeset_acquire_fini(&ctx);
1496
1497 return ret;
1498
1499backoff:
1500 drm_atomic_state_clear(state);
1501 drm_modeset_backoff(&ctx);
1502 goto retry;
1503}
1504
1505/**
1506 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1507 * @cmap: cmap to set
1508 * @info: fbdev registered by the helper
1509 */
1510int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1511{
1512 struct drm_fb_helper *fb_helper = info->par;
1513 int ret;
1514
1515 if (oops_in_progress)
1516 return -EBUSY;
1517
1518 mutex_lock(&fb_helper->lock);
1519
1520 if (!drm_fb_helper_is_bound(fb_helper)) {
1521 ret = -EBUSY;
1522 goto out;
1523 }
1524
1525 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1526 ret = setcmap_pseudo_palette(cmap, info);
1527 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1528 ret = setcmap_atomic(cmap, info);
1529 else
1530 ret = setcmap_legacy(cmap, info);
1531
1532out:
Thierry Redinge9827d82017-07-04 17:18:23 +02001533 mutex_unlock(&fb_helper->lock);
Peter Rosin964c6002017-07-13 18:25:27 +02001534
1535 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001536}
1537EXPORT_SYMBOL(drm_fb_helper_setcmap);
1538
Daniel Vetter207fd322013-01-20 22:13:14 +01001539/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001540 * drm_fb_helper_ioctl - legacy ioctl implementation
1541 * @info: fbdev registered by the helper
1542 * @cmd: ioctl command
1543 * @arg: ioctl argument
1544 *
1545 * A helper to implement the standard fbdev ioctl. Only
1546 * FBIO_WAITFORVSYNC is implemented for now.
1547 */
1548int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1549 unsigned long arg)
1550{
1551 struct drm_fb_helper *fb_helper = info->par;
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001552 struct drm_mode_set *mode_set;
1553 struct drm_crtc *crtc;
1554 int ret = 0;
1555
Thierry Redinge9827d82017-07-04 17:18:23 +02001556 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001557 if (!drm_fb_helper_is_bound(fb_helper)) {
1558 ret = -EBUSY;
1559 goto unlock;
1560 }
1561
1562 switch (cmd) {
1563 case FBIO_WAITFORVSYNC:
1564 /*
1565 * Only consider the first CRTC.
1566 *
1567 * This ioctl is supposed to take the CRTC number as
1568 * an argument, but in fbdev times, what that number
1569 * was supposed to be was quite unclear, different
1570 * drivers were passing that argument differently
1571 * (some by reference, some by value), and most of the
1572 * userspace applications were just hardcoding 0 as an
1573 * argument.
1574 *
1575 * The first CRTC should be the integrated panel on
1576 * most drivers, so this is the best choice we can
1577 * make. If we're not smart enough here, one should
1578 * just consider switch the userspace to KMS.
1579 */
1580 mode_set = &fb_helper->crtc_info[0].mode_set;
1581 crtc = mode_set->crtc;
1582
1583 /*
1584 * Only wait for a vblank event if the CRTC is
1585 * enabled, otherwise just don't do anythintg,
1586 * not even report an error.
1587 */
1588 ret = drm_crtc_vblank_get(crtc);
1589 if (!ret) {
1590 drm_crtc_wait_one_vblank(crtc);
1591 drm_crtc_vblank_put(crtc);
1592 }
1593
1594 ret = 0;
1595 goto unlock;
1596 default:
1597 ret = -ENOTTY;
1598 }
1599
1600unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001601 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001602 return ret;
1603}
1604EXPORT_SYMBOL(drm_fb_helper_ioctl);
1605
Eugeniy Paltsevdb05c482018-10-03 19:45:38 +03001606static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
1607 const struct fb_var_screeninfo *var_2)
1608{
1609 return var_1->bits_per_pixel == var_2->bits_per_pixel &&
1610 var_1->grayscale == var_2->grayscale &&
1611 var_1->red.offset == var_2->red.offset &&
1612 var_1->red.length == var_2->red.length &&
1613 var_1->red.msb_right == var_2->red.msb_right &&
1614 var_1->green.offset == var_2->green.offset &&
1615 var_1->green.length == var_2->green.length &&
1616 var_1->green.msb_right == var_2->green.msb_right &&
1617 var_1->blue.offset == var_2->blue.offset &&
1618 var_1->blue.length == var_2->blue.length &&
1619 var_1->blue.msb_right == var_2->blue.msb_right &&
1620 var_1->transp.offset == var_2->transp.offset &&
1621 var_1->transp.length == var_2->transp.length &&
1622 var_1->transp.msb_right == var_2->transp.msb_right;
1623}
1624
Ivan Mironov62d85b32019-01-08 12:23:52 +05001625static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
1626 u8 depth)
1627{
1628 switch (depth) {
1629 case 8:
1630 var->red.offset = 0;
1631 var->green.offset = 0;
1632 var->blue.offset = 0;
1633 var->red.length = 8; /* 8bit DAC */
1634 var->green.length = 8;
1635 var->blue.length = 8;
1636 var->transp.offset = 0;
1637 var->transp.length = 0;
1638 break;
1639 case 15:
1640 var->red.offset = 10;
1641 var->green.offset = 5;
1642 var->blue.offset = 0;
1643 var->red.length = 5;
1644 var->green.length = 5;
1645 var->blue.length = 5;
1646 var->transp.offset = 15;
1647 var->transp.length = 1;
1648 break;
1649 case 16:
1650 var->red.offset = 11;
1651 var->green.offset = 5;
1652 var->blue.offset = 0;
1653 var->red.length = 5;
1654 var->green.length = 6;
1655 var->blue.length = 5;
1656 var->transp.offset = 0;
1657 break;
1658 case 24:
1659 var->red.offset = 16;
1660 var->green.offset = 8;
1661 var->blue.offset = 0;
1662 var->red.length = 8;
1663 var->green.length = 8;
1664 var->blue.length = 8;
1665 var->transp.offset = 0;
1666 var->transp.length = 0;
1667 break;
1668 case 32:
1669 var->red.offset = 16;
1670 var->green.offset = 8;
1671 var->blue.offset = 0;
1672 var->red.length = 8;
1673 var->green.length = 8;
1674 var->blue.length = 8;
1675 var->transp.offset = 24;
1676 var->transp.length = 8;
1677 break;
1678 default:
1679 break;
1680 }
1681}
1682
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001683/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001684 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001685 * @var: screeninfo to check
1686 * @info: fbdev registered by the helper
1687 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001688int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1689 struct fb_info *info)
1690{
1691 struct drm_fb_helper *fb_helper = info->par;
1692 struct drm_framebuffer *fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +10001693
Ivan Mironov66a8d5b2019-01-08 12:23:53 +05001694 if (in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001695 return -EINVAL;
1696
Ivan Mironov66a8d5b2019-01-08 12:23:53 +05001697 if (var->pixclock != 0) {
1698 DRM_DEBUG("fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1699 var->pixclock = 0;
1700 }
1701
Alexandru Gheorghe042bf752018-11-01 17:02:05 +00001702 if ((drm_format_info_block_width(fb->format, 0) > 1) ||
1703 (drm_format_info_block_height(fb->format, 0) > 1))
1704 return -EINVAL;
1705
Stefan Agner865afb12016-10-11 16:15:04 -07001706 /*
1707 * Changes struct fb_var_screeninfo are currently not pushed back
1708 * to KMS, hence fail if different settings are requested.
1709 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001710 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001711 var->xres > fb->width || var->yres > fb->height ||
1712 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1713 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001714 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1715 var->xres, var->yres, var->bits_per_pixel,
1716 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001717 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001718 return -EINVAL;
1719 }
1720
Eugeniy Paltsevdb05c482018-10-03 19:45:38 +03001721 /*
Ivan Mironov62d85b32019-01-08 12:23:52 +05001722 * Workaround for SDL 1.2, which is known to be setting all pixel format
1723 * fields values to zero in some cases. We treat this situation as a
1724 * kind of "use some reasonable autodetected values".
1725 */
1726 if (!var->red.offset && !var->green.offset &&
1727 !var->blue.offset && !var->transp.offset &&
1728 !var->red.length && !var->green.length &&
1729 !var->blue.length && !var->transp.length &&
1730 !var->red.msb_right && !var->green.msb_right &&
1731 !var->blue.msb_right && !var->transp.msb_right) {
1732 drm_fb_helper_fill_pixel_fmt(var, fb->format->depth);
1733 }
1734
1735 /*
Eugeniy Paltsevdb05c482018-10-03 19:45:38 +03001736 * drm fbdev emulation doesn't support changing the pixel format at all,
1737 * so reject all pixel format changing requests.
1738 */
1739 if (!drm_fb_pixel_format_equal(var, &info->var)) {
1740 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001741 return -EINVAL;
1742 }
Eugeniy Paltsevdb05c482018-10-03 19:45:38 +03001743
Dave Airlie785b93e2009-08-28 15:46:53 +10001744 return 0;
1745}
1746EXPORT_SYMBOL(drm_fb_helper_check_var);
1747
Daniel Vetter207fd322013-01-20 22:13:14 +01001748/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001749 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001750 * @info: fbdev registered by the helper
1751 *
1752 * This will let fbcon do the mode init and is called at initialization time by
1753 * the fbdev core when registering the driver, and later on through the hotplug
1754 * callback.
1755 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001756int drm_fb_helper_set_par(struct fb_info *info)
1757{
1758 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001759 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001760
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001761 if (oops_in_progress)
1762 return -EBUSY;
1763
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001764 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001765 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001766 return -EINVAL;
1767 }
1768
Rob Clark5ea1f752014-05-30 12:29:48 -04001769 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001770
Dave Airlie785b93e2009-08-28 15:46:53 +10001771 return 0;
1772}
1773EXPORT_SYMBOL(drm_fb_helper_set_par);
1774
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001775static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
Rob Clark1edf0262015-08-25 15:35:59 -04001776{
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001777 int i;
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001778
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001779 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001780 struct drm_mode_set *mode_set;
1781
1782 mode_set = &fb_helper->crtc_info[i].mode_set;
1783
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001784 mode_set->x = x;
1785 mode_set->y = y;
Rob Clark1edf0262015-08-25 15:35:59 -04001786 }
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001787}
Rob Clark1edf0262015-08-25 15:35:59 -04001788
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001789static int pan_display_atomic(struct fb_var_screeninfo *var,
1790 struct fb_info *info)
1791{
1792 struct drm_fb_helper *fb_helper = info->par;
1793 int ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001794
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001795 pan_set(fb_helper, var->xoffset, var->yoffset);
Rob Clark1edf0262015-08-25 15:35:59 -04001796
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001797 ret = restore_fbdev_mode_atomic(fb_helper, true);
1798 if (!ret) {
1799 info->var.xoffset = var->xoffset;
1800 info->var.yoffset = var->yoffset;
1801 } else
1802 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001803
Rob Clark1edf0262015-08-25 15:35:59 -04001804 return ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001805}
1806
Daniel Vetter71286452017-04-03 10:33:04 +02001807static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001808 struct fb_info *info)
1809{
1810 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001811 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001812 int ret = 0;
1813 int i;
1814
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001815 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001816 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001817 modeset = &fb_helper->crtc_info[i].mode_set;
1818
1819 modeset->x = var->xoffset;
1820 modeset->y = var->yoffset;
1821
1822 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001823 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001824 if (!ret) {
1825 info->var.xoffset = var->xoffset;
1826 info->var.yoffset = var->yoffset;
1827 }
1828 }
1829 }
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001830 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001831
1832 return ret;
1833}
1834
1835/**
1836 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1837 * @var: updated screen information
1838 * @info: fbdev registered by the helper
1839 */
1840int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1841 struct fb_info *info)
1842{
1843 struct drm_fb_helper *fb_helper = info->par;
1844 struct drm_device *dev = fb_helper->dev;
1845 int ret;
1846
1847 if (oops_in_progress)
1848 return -EBUSY;
1849
Thierry Redinge9827d82017-07-04 17:18:23 +02001850 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001851 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001852 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001853 return -EBUSY;
1854 }
1855
1856 if (drm_drv_uses_atomic_modeset(dev))
1857 ret = pan_display_atomic(var, info);
1858 else
1859 ret = pan_display_legacy(var, info);
Thierry Redinge9827d82017-07-04 17:18:23 +02001860 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001861
Dave Airlie785b93e2009-08-28 15:46:53 +10001862 return ret;
1863}
1864EXPORT_SYMBOL(drm_fb_helper_pan_display);
1865
Daniel Vetter8acf6582013-01-21 23:38:37 +01001866/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001867 * Allocates the backing storage and sets up the fbdev info structure through
Daniel Vetterca91a272017-07-06 15:00:21 +02001868 * the ->fb_probe callback.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001869 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001870static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1871 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001872{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001873 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001874 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001875 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001876 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001877 int gamma_size = 0;
Linus Walleijf4bd5422019-01-10 12:40:49 +01001878 int best_depth = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001879
1880 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1881 sizes.surface_depth = 24;
1882 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001883 sizes.fb_width = (u32)-1;
1884 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001885
Linus Walleijf4bd5422019-01-10 12:40:49 +01001886 /*
1887 * If driver picks 8 or 16 by default use that for both depth/bpp
1888 * to begin with
1889 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001890 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001891 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001892
Dave Airlie785b93e2009-08-28 15:46:53 +10001893 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001894 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001895 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001896 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001897
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001898 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001899
1900 if (cmdline_mode->bpp_specified) {
1901 switch (cmdline_mode->bpp) {
1902 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001903 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001904 break;
1905 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001906 sizes.surface_depth = 15;
1907 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001908 break;
1909 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001910 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001911 break;
1912 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001913 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001914 break;
1915 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001916 sizes.surface_depth = 24;
1917 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001918 break;
1919 }
1920 break;
1921 }
1922 }
1923
Linus Walleijf4bd5422019-01-10 12:40:49 +01001924 /*
1925 * If we run into a situation where, for example, the primary plane
1926 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1927 * 16) we need to scale down the depth of the sizes we request.
1928 */
1929 for (i = 0; i < fb_helper->crtc_count; i++) {
1930 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
1931 struct drm_crtc *crtc = mode_set->crtc;
1932 struct drm_plane *plane = crtc->primary;
1933 int j;
1934
1935 DRM_DEBUG("test CRTC %d primary plane\n", i);
1936
1937 for (j = 0; j < plane->format_count; j++) {
1938 const struct drm_format_info *fmt;
1939
1940 fmt = drm_format_info(plane->format_types[j]);
1941
1942 /*
1943 * Do not consider YUV or other complicated formats
1944 * for framebuffers. This means only legacy formats
1945 * are supported (fmt->depth is a legacy field) but
1946 * the framebuffer emulation can only deal with such
1947 * formats, specifically RGB/BGA formats.
1948 */
1949 if (fmt->depth == 0)
1950 continue;
1951
1952 /* We found a perfect fit, great */
1953 if (fmt->depth == sizes.surface_depth) {
1954 best_depth = fmt->depth;
1955 break;
1956 }
1957
1958 /* Skip depths above what we're looking for */
1959 if (fmt->depth > sizes.surface_depth)
1960 continue;
1961
1962 /* Best depth found so far */
1963 if (fmt->depth > best_depth)
1964 best_depth = fmt->depth;
1965 }
1966 }
1967 if (sizes.surface_depth != best_depth) {
1968 DRM_INFO("requested bpp %d, scaled depth down to %d",
1969 sizes.surface_bpp, best_depth);
1970 sizes.surface_depth = best_depth;
1971 }
1972
Dave Airlie8be48d92010-03-30 05:34:14 +00001973 crtc_count = 0;
1974 for (i = 0; i < fb_helper->crtc_count; i++) {
1975 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001976 struct drm_mode_set *mode_set;
1977 int x, y, j;
1978 /* in case of tile group, are we the last tile vert or horiz?
1979 * If no tile group you are always the last one both vertically
1980 * and horizontally
1981 */
1982 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001983
Dave Airlie8be48d92010-03-30 05:34:14 +00001984 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001985 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001986
1987 if (!desired_mode)
1988 continue;
1989
1990 crtc_count++;
1991
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001992 x = fb_helper->crtc_info[i].x;
1993 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001994
1995 if (gamma_size == 0)
1996 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1997
1998 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1999 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04002000
2001 for (j = 0; j < mode_set->num_connectors; j++) {
2002 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002003
Rob Clark0e3704c2015-03-11 10:23:14 -04002004 if (connector->has_tile) {
2005 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
2006 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
2007 /* cloning to multiple tiles is just crazy-talk, so: */
2008 break;
2009 }
2010 }
2011
2012 if (lasth)
2013 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
2014 if (lastv)
2015 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10002016 }
2017
Dave Airlie38651672010-03-30 05:34:13 +00002018 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Daniel Vetterca91a272017-07-06 15:00:21 +02002019 DRM_INFO("Cannot find any crtc or sizes\n");
Maarten Lankhorst52dd0652017-11-28 12:16:03 +01002020
2021 /* First time: disable all crtc's.. */
2022 if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
2023 restore_fbdev_mode(fb_helper);
Daniel Vetterca91a272017-07-06 15:00:21 +02002024 return -EAGAIN;
Dave Airlie785b93e2009-08-28 15:46:53 +10002025 }
2026
Xinliang Liu5f152572017-02-15 17:19:08 +01002027 /* Handle our overallocation */
2028 sizes.surface_height *= drm_fbdev_overalloc;
2029 sizes.surface_height /= 100;
2030
Dave Airlie38651672010-03-30 05:34:13 +00002031 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01002032 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
2033 if (ret < 0)
2034 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10002035
Maarten Lankhorst8d44e9e2017-12-20 10:35:44 +01002036 strcpy(fb_helper->fb->comm, "[fbcon]");
Dave Airlie785b93e2009-08-28 15:46:53 +10002037 return 0;
2038}
Dave Airlie785b93e2009-08-28 15:46:53 +10002039
Daniel Vetter207fd322013-01-20 22:13:14 +01002040/**
2041 * drm_fb_helper_fill_fix - initializes fixed fbdev information
2042 * @info: fbdev registered by the helper
2043 * @pitch: desired pitch
2044 * @depth: desired depth
2045 *
2046 * Helper to fill in the fixed fbdev information useful for a non-accelerated
2047 * fbdev emulations. Drivers which support acceleration methods which impose
2048 * additional constraints need to set up their own limits.
2049 *
2050 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002051 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01002052 */
Dave Airlie3632ef82011-01-15 09:27:00 +10002053void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
2054 uint32_t depth)
2055{
2056 info->fix.type = FB_TYPE_PACKED_PIXELS;
2057 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
2058 FB_VISUAL_TRUECOLOR;
2059 info->fix.mmio_start = 0;
2060 info->fix.mmio_len = 0;
2061 info->fix.type_aux = 0;
2062 info->fix.xpanstep = 1; /* doing it in hw */
2063 info->fix.ypanstep = 1; /* doing it in hw */
2064 info->fix.ywrapstep = 0;
2065 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10002066
2067 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10002068}
2069EXPORT_SYMBOL(drm_fb_helper_fill_fix);
2070
Daniel Vetter207fd322013-01-20 22:13:14 +01002071/**
2072 * drm_fb_helper_fill_var - initalizes variable fbdev information
2073 * @info: fbdev instance to set up
2074 * @fb_helper: fb helper instance to use as template
2075 * @fb_width: desired fb width
2076 * @fb_height: desired fb height
2077 *
2078 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002079 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01002080 *
2081 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002082 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
2083 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01002084 */
Dave Airlie38651672010-03-30 05:34:13 +00002085void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10002086 uint32_t fb_width, uint32_t fb_height)
2087{
Dave Airlie38651672010-03-30 05:34:13 +00002088 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002089
Alexandru Gheorghe042bf752018-11-01 17:02:05 +00002090 WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) ||
2091 (drm_format_info_block_height(fb->format, 0) > 1));
Dave Airlie38651672010-03-30 05:34:13 +00002092 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10002093 info->var.xres_virtual = fb->width;
2094 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02002095 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00002096 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10002097 info->var.xoffset = 0;
2098 info->var.yoffset = 0;
2099 info->var.activate = FB_ACTIVATE_NOW;
Dave Airlie785b93e2009-08-28 15:46:53 +10002100
Ivan Mironov62d85b32019-01-08 12:23:52 +05002101 drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth);
Dave Airlie785b93e2009-08-28 15:46:53 +10002102
2103 info->var.xres = fb_width;
2104 info->var.yres = fb_height;
2105}
2106EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00002107
Daniel Vetter3df31162019-03-26 14:19:48 +01002108/**
2109 * drm_fb_helper_fill_info - initializes fbdev information
2110 * @info: fbdev instance to set up
2111 * @fb_helper: fb helper instance to use as template
2112 * @sizes: describes fbdev size and scanout surface size
2113 *
2114 * Sets up the variable and fixed fbdev metainformation from the given fb helper
2115 * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
2116 *
2117 * Drivers should call this (or their equivalent setup code) from their
2118 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
2119 * backing storage framebuffer.
2120 */
2121void drm_fb_helper_fill_info(struct fb_info *info,
2122 struct drm_fb_helper *fb_helper,
2123 struct drm_fb_helper_surface_size *sizes)
2124{
2125 struct drm_framebuffer *fb = fb_helper->fb;
2126
2127 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
2128 drm_fb_helper_fill_var(info, fb_helper,
2129 sizes->fb_width, sizes->fb_height);
2130
2131}
2132EXPORT_SYMBOL(drm_fb_helper_fill_info);
2133
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002134static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
Daniel Vettere13a0582017-07-05 06:56:29 +02002135 uint32_t maxX,
2136 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00002137{
2138 struct drm_connector *connector;
Daniel Vettere13a0582017-07-05 06:56:29 +02002139 int i, count = 0;
Dave Airlie38651672010-03-30 05:34:13 +00002140
Chris Wilson966a6a12016-11-29 12:02:15 +00002141 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002142 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002143 count += connector->funcs->fill_modes(connector, maxX, maxY);
2144 }
2145
2146 return count;
2147}
2148
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002149struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00002150{
2151 struct drm_display_mode *mode;
2152
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002153 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01002154 if (mode->hdisplay > width ||
2155 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00002156 continue;
2157 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2158 return mode;
2159 }
2160 return NULL;
2161}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002162EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002163
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002164static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00002165{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02002166 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00002167}
2168
Vincent Abrioua09759e2017-01-06 17:44:43 +01002169struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00002170{
Chris Wilson1794d252011-04-17 07:43:32 +01002171 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002172 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01002173 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00002174
Chris Wilsoneaf99c72014-08-06 10:08:32 +02002175 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00002176 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00002177 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00002178
2179 /* attempt to find a matching mode in the list of modes
2180 * we have gotten so far, if not add a CVT mode that conforms
2181 */
2182 if (cmdline_mode->rb || cmdline_mode->margins)
2183 goto create_mode;
2184
Takashi Iwaic683f422014-03-19 14:53:13 +01002185 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002186again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002187 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00002188 /* check width/height */
2189 if (mode->hdisplay != cmdline_mode->xres ||
2190 mode->vdisplay != cmdline_mode->yres)
2191 continue;
2192
2193 if (cmdline_mode->refresh_specified) {
2194 if (mode->vrefresh != cmdline_mode->refresh)
2195 continue;
2196 }
2197
2198 if (cmdline_mode->interlace) {
2199 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2200 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01002201 } else if (prefer_non_interlace) {
2202 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2203 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002204 }
2205 return mode;
2206 }
2207
Takashi Iwaic683f422014-03-19 14:53:13 +01002208 if (prefer_non_interlace) {
2209 prefer_non_interlace = false;
2210 goto again;
2211 }
2212
Dave Airlie38651672010-03-30 05:34:13 +00002213create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01002214 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2215 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002216 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00002217 return mode;
2218}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002219EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002220
2221static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2222{
2223 bool enable;
2224
Dave Airlieb5f05382017-10-16 05:08:39 +01002225 if (connector->display_info.non_desktop)
2226 return false;
2227
Sachin Kamat96081cd2012-11-15 03:43:30 +00002228 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00002229 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002230 else
Dave Airlie38651672010-03-30 05:34:13 +00002231 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002232
Dave Airlie38651672010-03-30 05:34:13 +00002233 return enable;
2234}
2235
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002236static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2237 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00002238{
2239 bool any_enabled = false;
2240 struct drm_connector *connector;
2241 int i = 0;
2242
Chris Wilson966a6a12016-11-29 12:02:15 +00002243 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002244 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002245 enabled[i] = drm_connector_enabled(connector, true);
2246 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
Dave Airlieb5f05382017-10-16 05:08:39 +01002247 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
2248
Dave Airlie38651672010-03-30 05:34:13 +00002249 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00002250 }
2251
2252 if (any_enabled)
2253 return;
2254
Chris Wilson966a6a12016-11-29 12:02:15 +00002255 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002256 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002257 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00002258 }
2259}
2260
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002261static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2262 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002263 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002264 bool *enabled, int width, int height)
2265{
2266 int count, i, j;
2267 bool can_clone = false;
2268 struct drm_fb_helper_connector *fb_helper_conn;
2269 struct drm_display_mode *dmt_mode, *mode;
2270
2271 /* only contemplate cloning in the single crtc case */
2272 if (fb_helper->crtc_count > 1)
2273 return false;
2274
2275 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002276 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002277 if (enabled[i])
2278 count++;
2279 }
2280
2281 /* only contemplate cloning if more than one connector is enabled */
2282 if (count <= 1)
2283 return false;
2284
2285 /* check the command line or if nothing common pick 1024x768 */
2286 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002287 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002288 if (!enabled[i])
2289 continue;
2290 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002291 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002292 if (!modes[i]) {
2293 can_clone = false;
2294 break;
2295 }
2296 for (j = 0; j < i; j++) {
2297 if (!enabled[j])
2298 continue;
Shashank Sharma222ec162018-05-08 16:39:44 +05302299 if (!drm_mode_match(modes[j], modes[i],
2300 DRM_MODE_MATCH_TIMINGS |
2301 DRM_MODE_MATCH_CLOCK |
2302 DRM_MODE_MATCH_FLAGS |
2303 DRM_MODE_MATCH_3D_FLAGS))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002304 can_clone = false;
2305 }
2306 }
2307
2308 if (can_clone) {
2309 DRM_DEBUG_KMS("can clone using command line\n");
2310 return true;
2311 }
2312
2313 /* try and find a 1024x768 mode on each connector */
2314 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002315 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002316
Chris Wilson966a6a12016-11-29 12:02:15 +00002317 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002318 if (!enabled[i])
2319 continue;
2320
2321 fb_helper_conn = fb_helper->connector_info[i];
2322 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Shashank Sharma222ec162018-05-08 16:39:44 +05302323 if (drm_mode_match(mode, dmt_mode,
2324 DRM_MODE_MATCH_TIMINGS |
2325 DRM_MODE_MATCH_CLOCK |
2326 DRM_MODE_MATCH_FLAGS |
2327 DRM_MODE_MATCH_3D_FLAGS))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002328 modes[i] = mode;
2329 }
2330 if (!modes[i])
2331 can_clone = false;
2332 }
2333
2334 if (can_clone) {
2335 DRM_DEBUG_KMS("can clone using 1024x768\n");
2336 return true;
2337 }
2338 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2339 return false;
2340}
2341
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002342static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2343 struct drm_display_mode **modes,
2344 struct drm_fb_offset *offsets,
2345 int idx,
2346 int h_idx, int v_idx)
2347{
2348 struct drm_fb_helper_connector *fb_helper_conn;
2349 int i;
2350 int hoffset = 0, voffset = 0;
2351
Chris Wilson966a6a12016-11-29 12:02:15 +00002352 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002353 fb_helper_conn = fb_helper->connector_info[i];
2354 if (!fb_helper_conn->connector->has_tile)
2355 continue;
2356
2357 if (!modes[i] && (h_idx || v_idx)) {
2358 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2359 fb_helper_conn->connector->base.id);
2360 continue;
2361 }
2362 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2363 hoffset += modes[i]->hdisplay;
2364
2365 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2366 voffset += modes[i]->vdisplay;
2367 }
2368 offsets[idx].x = hoffset;
2369 offsets[idx].y = voffset;
2370 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2371 return 0;
2372}
2373
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002374static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002375 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002376 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002377 bool *enabled, int width, int height)
2378{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002379 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002380 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2381 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002382 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002383 int i;
2384
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002385retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002386 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002387 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002388
Chris Wilsonc96521e2016-11-27 17:09:10 +00002389 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002390 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002391
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002392 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002393 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002394 continue;
2395 }
2396
2397 /* first pass over all the untiled connectors */
2398 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2399 continue;
2400
2401 if (tile_pass == 1) {
2402 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2403 fb_helper_conn->connector->tile_v_loc != 0)
2404 continue;
2405
2406 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002407 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002408 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2409 /* if this tile_pass doesn't cover any of the tiles - keep going */
2410 continue;
2411
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002412 /*
2413 * find the tile offsets for this pass - need to find
2414 * all tiles left and above
2415 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002416 drm_get_tile_offsets(fb_helper, modes, offsets,
2417 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2418 }
Dave Airlie38651672010-03-30 05:34:13 +00002419 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002420 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002421
2422 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002423 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002424 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002425 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2426 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002427 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002428 }
2429 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002430 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2431 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002432 break;
2433 }
2434 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2435 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002436 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002437 }
2438
2439 if ((conn_configured & mask) != mask) {
2440 tile_pass++;
2441 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002442 }
2443 return true;
2444}
2445
Ville Syrjälä0d9988912018-06-28 16:13:07 +03002446static bool connector_has_possible_crtc(struct drm_connector *connector,
2447 struct drm_crtc *crtc)
2448{
Ville Syrjälä83aefbb2018-06-28 16:13:09 +03002449 struct drm_encoder *encoder;
Ville Syrjälä0d9988912018-06-28 16:13:07 +03002450 int i;
2451
Ville Syrjälä83aefbb2018-06-28 16:13:09 +03002452 drm_connector_for_each_possible_encoder(connector, encoder, i) {
Ville Syrjälä0d9988912018-06-28 16:13:07 +03002453 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
2454 return true;
2455 }
2456
2457 return false;
2458}
2459
Dave Airlie8be48d92010-03-30 05:34:14 +00002460static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2461 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002462 struct drm_display_mode **modes,
2463 int n, int width, int height)
2464{
2465 int c, o;
2466 struct drm_connector *connector;
Dave Airlie38651672010-03-30 05:34:13 +00002467 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002468 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002469 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002470
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002471 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002472 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002473
2474 fb_helper_conn = fb_helper->connector_info[n];
2475 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002476
2477 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002478 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002479 if (modes[n] == NULL)
2480 return best_score;
2481
Harsha Sharma4b947b1c2017-10-13 13:07:47 +05302482 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002483 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002484 if (!crtcs)
2485 return best_score;
2486
2487 my_score = 1;
2488 if (connector->status == connector_status_connected)
2489 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002490 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002491 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002492 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002493 my_score++;
2494
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002495 /*
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002496 * select a crtc for this connector and then attempt to configure
2497 * remaining connectors
2498 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002499 for (c = 0; c < fb_helper->crtc_count; c++) {
2500 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002501
Ville Syrjälä0d9988912018-06-28 16:13:07 +03002502 if (!connector_has_possible_crtc(connector,
2503 crtc->mode_set.crtc))
Dave Airlie38651672010-03-30 05:34:13 +00002504 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002505
2506 for (o = 0; o < n; o++)
2507 if (best_crtcs[o] == crtc)
2508 break;
2509
2510 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002511 /* ignore cloning unless only a single crtc */
2512 if (fb_helper->crtc_count > 1)
2513 continue;
2514
2515 if (!drm_mode_equal(modes[o], modes[n]))
2516 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002517 }
2518
2519 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002520 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2521 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002522 width, height);
2523 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002524 best_score = score;
2525 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002526 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002527 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002528 }
Dave Airlie38651672010-03-30 05:34:13 +00002529 }
Ville Syrjälä0d9988912018-06-28 16:13:07 +03002530
Dave Airlie38651672010-03-30 05:34:13 +00002531 kfree(crtcs);
2532 return best_score;
2533}
2534
Hans de Goede8f0cb412017-11-25 20:35:50 +01002535/*
2536 * This function checks if rotation is necessary because of panel orientation
2537 * and if it is, if it is supported.
Matt Roper1e55a532019-02-01 17:23:26 -08002538 * If rotation is necessary and supported, it gets set in fb_crtc.rotation.
Hans de Goede8f0cb412017-11-25 20:35:50 +01002539 * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets
2540 * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only
2541 * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do
2542 * the unsupported rotation.
2543 */
2544static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper,
2545 struct drm_fb_helper_crtc *fb_crtc,
2546 struct drm_connector *connector)
2547{
2548 struct drm_plane *plane = fb_crtc->mode_set.crtc->primary;
2549 uint64_t valid_mask = 0;
2550 int i, rotation;
2551
2552 fb_crtc->rotation = DRM_MODE_ROTATE_0;
2553
2554 switch (connector->display_info.panel_orientation) {
2555 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
2556 rotation = DRM_MODE_ROTATE_180;
2557 break;
2558 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
2559 rotation = DRM_MODE_ROTATE_90;
2560 break;
2561 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
2562 rotation = DRM_MODE_ROTATE_270;
2563 break;
2564 default:
2565 rotation = DRM_MODE_ROTATE_0;
2566 }
2567
2568 /*
2569 * TODO: support 90 / 270 degree hardware rotation,
2570 * depending on the hardware this may require the framebuffer
2571 * to be in a specific tiling format.
2572 */
2573 if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) {
2574 fb_helper->sw_rotations |= rotation;
2575 return;
2576 }
2577
2578 for (i = 0; i < plane->rotation_property->num_values; i++)
2579 valid_mask |= (1ULL << plane->rotation_property->values[i]);
2580
2581 if (!(rotation & valid_mask)) {
2582 fb_helper->sw_rotations |= rotation;
2583 return;
2584 }
2585
2586 fb_crtc->rotation = rotation;
2587 /* Rotating in hardware, fbcon should not rotate */
2588 fb_helper->sw_rotations |= DRM_MODE_ROTATE_0;
2589}
2590
Chris Wilson64e94402016-11-29 12:02:16 +00002591static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2592 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002593{
Dave Airlie8be48d92010-03-30 05:34:14 +00002594 struct drm_device *dev = fb_helper->dev;
2595 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002596 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002597 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002598 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002599 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002600
2601 DRM_DEBUG_KMS("\n");
Chris Wilson966a6a12016-11-29 12:02:15 +00002602 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002603 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002604
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002605 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002606 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002607 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002608 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002609 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002610 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002611 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002612 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002613 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002614 DRM_ERROR("Memory allocation failed\n");
2615 goto out;
2616 }
2617
Daniel Vettere13a0582017-07-05 06:56:29 +02002618 mutex_lock(&fb_helper->dev->mode_config.mutex);
2619 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2620 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002621 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002622
Jesse Barnes11e17a02013-02-19 13:31:39 -08002623 if (!(fb_helper->funcs->initial_config &&
2624 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002625 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002626 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002627 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2628 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2629 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002630
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002631 if (!drm_target_cloned(fb_helper, modes, offsets,
2632 enabled, width, height) &&
2633 !drm_target_preferred(fb_helper, modes, offsets,
2634 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002635 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002636
2637 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2638 width, height);
2639
2640 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002641 }
Daniel Vettere13a0582017-07-05 06:56:29 +02002642 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002643
Dave Airlie8be48d92010-03-30 05:34:14 +00002644 /* need to set the modesets up here for use later */
2645 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002646 for (i = 0; i < fb_helper->crtc_count; i++)
2647 drm_fb_helper_modeset_release(fb_helper,
2648 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002649
Hans de Goede8f0cb412017-11-25 20:35:50 +01002650 fb_helper->sw_rotations = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002651 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002652 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002653 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002654 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie38651672010-03-30 05:34:13 +00002655
Dave Airlie8be48d92010-03-30 05:34:14 +00002656 if (mode && fb_crtc) {
David Lechner27a061f2017-08-02 13:00:13 -05002657 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Ville Syrjäläa2889602016-10-26 17:41:18 +03002658 struct drm_connector *connector =
2659 fb_helper->connector_info[i]->connector;
2660
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002661 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2662 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002663
Dave Airlie8be48d92010-03-30 05:34:14 +00002664 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002665 fb_crtc->x = offset->x;
2666 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002667 modeset->mode = drm_mode_duplicate(dev,
2668 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002669 drm_connector_get(connector);
Hans de Goede8f0cb412017-11-25 20:35:50 +01002670 drm_setup_crtc_rotation(fb_helper, fb_crtc, connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002671 modeset->connectors[modeset->num_connectors++] = connector;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002672 modeset->x = offset->x;
2673 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002674 }
Dave Airlie38651672010-03-30 05:34:13 +00002675 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002676out:
Dave Airlie38651672010-03-30 05:34:13 +00002677 kfree(crtcs);
2678 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002679 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002680 kfree(enabled);
2681}
2682
David Lechnerf461bd22017-08-03 11:19:08 -05002683/*
2684 * This is a continuation of drm_setup_crtcs() that sets up anything related
2685 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2686 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2687 * So, any setup that touches those fields needs to be done here instead of in
2688 * drm_setup_crtcs().
2689 */
2690static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2691{
David Lechner991a3992017-08-04 11:25:24 -05002692 struct fb_info *info = fb_helper->fbdev;
David Lechnerf461bd22017-08-03 11:19:08 -05002693 int i;
2694
2695 for (i = 0; i < fb_helper->crtc_count; i++)
2696 if (fb_helper->crtc_info[i].mode_set.num_connectors)
2697 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
David Lechner991a3992017-08-04 11:25:24 -05002698
2699 mutex_lock(&fb_helper->dev->mode_config.mutex);
2700 drm_fb_helper_for_each_connector(fb_helper, i) {
2701 struct drm_connector *connector =
2702 fb_helper->connector_info[i]->connector;
2703
2704 /* use first connected connector for the physical dimensions */
2705 if (connector->status == connector_status_connected) {
2706 info->var.width = connector->display_info.width_mm;
2707 info->var.height = connector->display_info.height_mm;
2708 break;
2709 }
2710 }
2711 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Hans de Goede8f0cb412017-11-25 20:35:50 +01002712
2713 switch (fb_helper->sw_rotations) {
2714 case DRM_MODE_ROTATE_0:
2715 info->fbcon_rotate_hint = FB_ROTATE_UR;
2716 break;
2717 case DRM_MODE_ROTATE_90:
2718 info->fbcon_rotate_hint = FB_ROTATE_CCW;
2719 break;
2720 case DRM_MODE_ROTATE_180:
2721 info->fbcon_rotate_hint = FB_ROTATE_UD;
2722 break;
2723 case DRM_MODE_ROTATE_270:
2724 info->fbcon_rotate_hint = FB_ROTATE_CW;
2725 break;
2726 default:
2727 /*
2728 * Multiple bits are set / multiple rotations requested
2729 * fbcon cannot handle separate rotation settings per
2730 * output, so fallback to unrotated.
2731 */
2732 info->fbcon_rotate_hint = FB_ROTATE_UR;
2733 }
David Lechnerf461bd22017-08-03 11:19:08 -05002734}
2735
Daniel Vetterca91a272017-07-06 15:00:21 +02002736/* Note: Drops fb_helper->lock before returning. */
2737static int
2738__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2739 int bpp_sel)
2740{
2741 struct drm_device *dev = fb_helper->dev;
2742 struct fb_info *info;
2743 unsigned int width, height;
2744 int ret;
2745
2746 width = dev->mode_config.max_width;
2747 height = dev->mode_config.max_height;
2748
2749 drm_setup_crtcs(fb_helper, width, height);
2750 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2751 if (ret < 0) {
2752 if (ret == -EAGAIN) {
2753 fb_helper->preferred_bpp = bpp_sel;
2754 fb_helper->deferred_setup = true;
2755 ret = 0;
2756 }
2757 mutex_unlock(&fb_helper->lock);
2758
2759 return ret;
2760 }
David Lechnerf461bd22017-08-03 11:19:08 -05002761 drm_setup_crtcs_fb(fb_helper);
Daniel Vetterca91a272017-07-06 15:00:21 +02002762
2763 fb_helper->deferred_setup = false;
2764
2765 info = fb_helper->fbdev;
2766 info->var.pixclock = 0;
Neil Armstrong4be9bd12018-09-28 14:05:55 +02002767 /* Shamelessly allow physical address leaking to userspace */
2768#if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2769 if (!drm_leak_fbdev_smem)
2770#endif
2771 /* don't leak any physical addresses to userspace */
2772 info->flags |= FBINFO_HIDE_SMEM_START;
Daniel Vetterca91a272017-07-06 15:00:21 +02002773
2774 /* Need to drop locks to avoid recursive deadlock in
2775 * register_framebuffer. This is ok because the only thing left to do is
2776 * register the fbdev emulation instance in kernel_fb_helper_list. */
2777 mutex_unlock(&fb_helper->lock);
2778
2779 ret = register_framebuffer(info);
2780 if (ret < 0)
2781 return ret;
2782
2783 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2784 info->node, info->fix.id);
2785
2786 mutex_lock(&kernel_fb_helper_lock);
2787 if (list_empty(&kernel_fb_helper_list))
2788 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2789
2790 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2791 mutex_unlock(&kernel_fb_helper_lock);
2792
2793 return 0;
2794}
2795
Dave Airlie38651672010-03-30 05:34:13 +00002796/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002797 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002798 * @fb_helper: fb_helper device struct
2799 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002800 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002801 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002802 * At the moment, this is a cloned configuration across all heads with
2803 * a new framebuffer object as the backing store.
2804 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002805 * Note that this also registers the fbdev and so allows userspace to call into
2806 * the driver through the fbdev interfaces.
2807 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002808 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2809 * to let the driver allocate and initialize the fbdev info structure and the
2810 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002811 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2812 * values for the fbdev info structure.
2813 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002814 * HANG DEBUGGING:
2815 *
2816 * When you have fbcon support built-in or already loaded, this function will do
2817 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2818 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2819 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2820 * to consoles, not even serial console. This means when your driver crashes,
2821 * you will see absolutely nothing else but a system stuck in this function,
2822 * with no further output. Any kind of printk() you place within your own driver
2823 * or in the drm core modeset code will also never show up.
2824 *
2825 * Standard debug practice is to run the fbcon setup without taking the
2826 * console_lock as a hack, to be able to see backtraces and crashes on the
2827 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2828 * cmdline option.
2829 *
2830 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002831 * first modeset from userspace will crash in the same way, and is even easier
2832 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002833 * kernel cmdline option.
2834 *
Dave Airlie38651672010-03-30 05:34:13 +00002835 * RETURNS:
2836 * Zero if everything went ok, nonzero otherwise.
2837 */
Thierry Reding01934c22014-12-19 11:21:32 +01002838int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002839{
Chris Wilson966a6a12016-11-29 12:02:15 +00002840 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002841
Daniel Vetterf64c5572015-08-25 15:45:13 +02002842 if (!drm_fbdev_emulation)
2843 return 0;
2844
Thierry Redinge9827d82017-07-04 17:18:23 +02002845 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002846 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00002847
Daniel Vetterca91a272017-07-06 15:00:21 +02002848 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002849}
Dave Airlie8be48d92010-03-30 05:34:14 +00002850EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002851
Chris Wilson73943712011-04-22 11:03:57 +01002852/**
2853 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002854 * probing all the outputs attached to the fb
Noralf Trønnesc7779902017-10-30 16:39:37 +01002855 * @fb_helper: driver-allocated fbdev helper, can be NULL
Chris Wilson73943712011-04-22 11:03:57 +01002856 *
Chris Wilson73943712011-04-22 11:03:57 +01002857 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002858 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002859 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002860 * Called at runtime, takes the mode config locks to be able to check/change the
2861 * modeset configuration. Must be run from process context (which usually means
2862 * either the output polling work or a work item launched from the driver's
2863 * hotplug interrupt).
2864 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002865 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002866 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002867 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2868 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002869 *
Chris Wilson73943712011-04-22 11:03:57 +01002870 * RETURNS:
2871 * 0 on success and a non-zero error code otherwise.
2872 */
2873int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002874{
Thierry Redinge9827d82017-07-04 17:18:23 +02002875 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002876
Noralf Trønnesc7779902017-10-30 16:39:37 +01002877 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +02002878 return 0;
2879
Thierry Redinge9827d82017-07-04 17:18:23 +02002880 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002881 if (fb_helper->deferred_setup) {
2882 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
2883 fb_helper->preferred_bpp);
2884 return err;
2885 }
2886
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002887 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002888 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002889 mutex_unlock(&fb_helper->lock);
2890 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002891 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002892
Dave Airlie38651672010-03-30 05:34:13 +00002893 DRM_DEBUG_KMS("\n");
2894
Chris Wilson64e94402016-11-29 12:02:16 +00002895 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
David Lechnerf461bd22017-08-03 11:19:08 -05002896 drm_setup_crtcs_fb(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +02002897 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002898
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002899 drm_fb_helper_set_par(fb_helper->fbdev);
2900
2901 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002902}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002903EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002904
Noralf Trønnes304a4f62017-10-30 16:39:39 +01002905/**
Noralf Trønnes87412162017-12-15 18:51:14 +01002906 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
2907 * @dev: DRM device
2908 * @fb_helper: fbdev helper structure to set up
2909 * @funcs: fbdev helper functions
2910 * @preferred_bpp: Preferred bits per pixel for the device.
2911 * @dev->mode_config.preferred_depth is used if this is zero.
2912 * @max_conn_count: Maximum number of connectors.
2913 * @dev->mode_config.num_connector is used if this is zero.
2914 *
2915 * This function sets up fbdev emulation and registers fbdev for access by
2916 * userspace. If all connectors are disconnected, setup is deferred to the next
2917 * time drm_fb_helper_hotplug_event() is called.
2918 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
2919 * function.
2920 *
Peter Wuc10802b2018-09-07 00:18:10 +02002921 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev.
2922 *
2923 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup().
Noralf Trønnes87412162017-12-15 18:51:14 +01002924 *
2925 * Returns:
2926 * Zero on success or negative error code on failure.
2927 */
2928int drm_fb_helper_fbdev_setup(struct drm_device *dev,
2929 struct drm_fb_helper *fb_helper,
2930 const struct drm_fb_helper_funcs *funcs,
2931 unsigned int preferred_bpp,
2932 unsigned int max_conn_count)
2933{
2934 int ret;
2935
2936 if (!preferred_bpp)
2937 preferred_bpp = dev->mode_config.preferred_depth;
2938 if (!preferred_bpp)
2939 preferred_bpp = 32;
2940
2941 if (!max_conn_count)
2942 max_conn_count = dev->mode_config.num_connector;
2943 if (!max_conn_count) {
Noralf Trønnes61293692018-09-08 15:46:29 +02002944 DRM_DEV_ERROR(dev->dev, "fbdev: No connectors\n");
Noralf Trønnes87412162017-12-15 18:51:14 +01002945 return -EINVAL;
2946 }
2947
2948 drm_fb_helper_prepare(dev, fb_helper, funcs);
2949
2950 ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
2951 if (ret < 0) {
Noralf Trønnes61293692018-09-08 15:46:29 +02002952 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret);
Noralf Trønnes87412162017-12-15 18:51:14 +01002953 return ret;
2954 }
2955
2956 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
2957 if (ret < 0) {
Noralf Trønnes61293692018-09-08 15:46:29 +02002958 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret);
Noralf Trønnes87412162017-12-15 18:51:14 +01002959 goto err_drm_fb_helper_fini;
2960 }
2961
2962 if (!drm_drv_uses_atomic_modeset(dev))
2963 drm_helper_disable_unused_functions(dev);
2964
2965 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
2966 if (ret < 0) {
Noralf Trønnes61293692018-09-08 15:46:29 +02002967 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret);
Noralf Trønnes87412162017-12-15 18:51:14 +01002968 goto err_drm_fb_helper_fini;
2969 }
2970
2971 return 0;
2972
2973err_drm_fb_helper_fini:
Peter Wu00eb5b02018-12-23 01:55:07 +01002974 drm_fb_helper_fbdev_teardown(dev);
Noralf Trønnes87412162017-12-15 18:51:14 +01002975
2976 return ret;
2977}
2978EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
2979
2980/**
2981 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
2982 * @dev: DRM device
2983 *
2984 * This function unregisters fbdev if not already done and cleans up the
2985 * associated resources including the &drm_framebuffer.
2986 * The driver is responsible for freeing the &drm_fb_helper structure which is
2987 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
2988 * when this function returns.
2989 *
2990 * In order to support device removal/unplug while file handles are still open,
2991 * drm_fb_helper_unregister_fbi() should be called on device removal and
2992 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
2993 * file handles are closed.
2994 */
2995void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
2996{
2997 struct drm_fb_helper *fb_helper = dev->fb_helper;
Noralf Trønnes48c95712017-12-15 18:51:17 +01002998 struct fb_ops *fbops = NULL;
Noralf Trønnes87412162017-12-15 18:51:14 +01002999
3000 if (!fb_helper)
3001 return;
3002
3003 /* Unregister if it hasn't been done already */
3004 if (fb_helper->fbdev && fb_helper->fbdev->dev)
3005 drm_fb_helper_unregister_fbi(fb_helper);
3006
Noralf Trønnes48c95712017-12-15 18:51:17 +01003007 if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) {
3008 fb_deferred_io_cleanup(fb_helper->fbdev);
3009 kfree(fb_helper->fbdev->fbdefio);
3010 fbops = fb_helper->fbdev->fbops;
3011 }
3012
Noralf Trønnes87412162017-12-15 18:51:14 +01003013 drm_fb_helper_fini(fb_helper);
Noralf Trønnes48c95712017-12-15 18:51:17 +01003014 kfree(fbops);
Noralf Trønnes87412162017-12-15 18:51:14 +01003015
3016 if (fb_helper->fb)
3017 drm_framebuffer_remove(fb_helper->fb);
3018}
3019EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
3020
3021/**
Noralf Trønnes304a4f62017-10-30 16:39:39 +01003022 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
3023 * @dev: DRM device
3024 *
3025 * This function can be used as the &drm_driver->lastclose callback for drivers
3026 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
3027 */
3028void drm_fb_helper_lastclose(struct drm_device *dev)
3029{
3030 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
3031}
3032EXPORT_SYMBOL(drm_fb_helper_lastclose);
3033
3034/**
3035 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
3036 * helper for fbdev emulation
3037 * @dev: DRM device
3038 *
3039 * This function can be used as the
3040 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
3041 * need to call drm_fb_helper_hotplug_event().
3042 */
3043void drm_fb_helper_output_poll_changed(struct drm_device *dev)
3044{
3045 drm_fb_helper_hotplug_event(dev->fb_helper);
3046}
3047EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
3048
Noralf Trønnesd5365402018-07-03 18:03:48 +02003049/* @user: 1=userspace, 0=fbcon */
3050static int drm_fbdev_fb_open(struct fb_info *info, int user)
3051{
3052 struct drm_fb_helper *fb_helper = info->par;
3053
Noralf Trønnes6ab20a02019-02-10 14:10:39 +01003054 /* No need to take a ref for fbcon because it unbinds on unregister */
3055 if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
Noralf Trønnesd5365402018-07-03 18:03:48 +02003056 return -ENODEV;
3057
3058 return 0;
3059}
3060
3061static int drm_fbdev_fb_release(struct fb_info *info, int user)
3062{
3063 struct drm_fb_helper *fb_helper = info->par;
3064
Noralf Trønnes6ab20a02019-02-10 14:10:39 +01003065 if (user)
3066 module_put(fb_helper->dev->driver->fops->owner);
Noralf Trønnesd5365402018-07-03 18:03:48 +02003067
3068 return 0;
3069}
3070
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003071static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
Noralf Trønnesd5365402018-07-03 18:03:48 +02003072{
Noralf Trønnesd5365402018-07-03 18:03:48 +02003073 struct fb_info *fbi = fb_helper->fbdev;
3074 struct fb_ops *fbops = NULL;
3075 void *shadow = NULL;
3076
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003077 if (!fb_helper->dev)
3078 return;
3079
3080 if (fbi && fbi->fbdefio) {
Noralf Trønnesd5365402018-07-03 18:03:48 +02003081 fb_deferred_io_cleanup(fbi);
3082 shadow = fbi->screen_buffer;
3083 fbops = fbi->fbops;
3084 }
3085
3086 drm_fb_helper_fini(fb_helper);
3087
3088 if (shadow) {
3089 vfree(shadow);
3090 kfree(fbops);
3091 }
3092
3093 drm_client_framebuffer_delete(fb_helper->buffer);
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003094}
3095
3096static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
3097{
3098 drm_fbdev_cleanup(fb_helper);
Noralf Trønnes2de304b2019-01-14 13:10:59 +01003099 drm_client_release(&fb_helper->client);
3100 kfree(fb_helper);
Noralf Trønnesd5365402018-07-03 18:03:48 +02003101}
3102
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003103/*
3104 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
3105 * unregister_framebuffer() or fb_release().
3106 */
3107static void drm_fbdev_fb_destroy(struct fb_info *info)
3108{
3109 drm_fbdev_release(info->par);
3110}
3111
Noralf Trønnesd5365402018-07-03 18:03:48 +02003112static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
3113{
3114 struct drm_fb_helper *fb_helper = info->par;
3115
3116 if (fb_helper->dev->driver->gem_prime_mmap)
3117 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
3118 else
3119 return -ENODEV;
3120}
3121
3122static struct fb_ops drm_fbdev_fb_ops = {
3123 .owner = THIS_MODULE,
3124 DRM_FB_HELPER_DEFAULT_OPS,
3125 .fb_open = drm_fbdev_fb_open,
3126 .fb_release = drm_fbdev_fb_release,
3127 .fb_destroy = drm_fbdev_fb_destroy,
3128 .fb_mmap = drm_fbdev_fb_mmap,
3129 .fb_read = drm_fb_helper_sys_read,
3130 .fb_write = drm_fb_helper_sys_write,
3131 .fb_fillrect = drm_fb_helper_sys_fillrect,
3132 .fb_copyarea = drm_fb_helper_sys_copyarea,
3133 .fb_imageblit = drm_fb_helper_sys_imageblit,
3134};
3135
3136static struct fb_deferred_io drm_fbdev_defio = {
3137 .delay = HZ / 20,
3138 .deferred_io = drm_fb_helper_deferred_io,
3139};
3140
3141/**
3142 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
3143 * @fb_helper: fbdev helper structure
3144 * @sizes: describes fbdev size and scanout surface size
3145 *
Peter Wuc10802b2018-09-07 00:18:10 +02003146 * This function uses the client API to create a framebuffer backed by a dumb buffer.
Noralf Trønnesd5365402018-07-03 18:03:48 +02003147 *
3148 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
3149 * fb_copyarea, fb_imageblit.
3150 *
3151 * Returns:
3152 * Zero on success or negative error code on failure.
3153 */
3154int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
3155 struct drm_fb_helper_surface_size *sizes)
3156{
3157 struct drm_client_dev *client = &fb_helper->client;
3158 struct drm_client_buffer *buffer;
3159 struct drm_framebuffer *fb;
3160 struct fb_info *fbi;
3161 u32 format;
Noralf Trønnesd5365402018-07-03 18:03:48 +02003162
3163 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
3164 sizes->surface_width, sizes->surface_height,
3165 sizes->surface_bpp);
3166
3167 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
3168 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
3169 sizes->surface_height, format);
3170 if (IS_ERR(buffer))
3171 return PTR_ERR(buffer);
3172
3173 fb_helper->buffer = buffer;
3174 fb_helper->fb = buffer->fb;
3175 fb = buffer->fb;
3176
3177 fbi = drm_fb_helper_alloc_fbi(fb_helper);
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003178 if (IS_ERR(fbi))
3179 return PTR_ERR(fbi);
Noralf Trønnesd5365402018-07-03 18:03:48 +02003180
3181 fbi->par = fb_helper;
3182 fbi->fbops = &drm_fbdev_fb_ops;
3183 fbi->screen_size = fb->height * fb->pitches[0];
3184 fbi->fix.smem_len = fbi->screen_size;
3185 fbi->screen_buffer = buffer->vaddr;
Neil Armstrong4be9bd12018-09-28 14:05:55 +02003186 /* Shamelessly leak the physical address to user-space */
3187#if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
3188 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0)
3189 fbi->fix.smem_start =
3190 page_to_phys(virt_to_page(fbi->screen_buffer));
3191#endif
Noralf Trønnesd5365402018-07-03 18:03:48 +02003192 strcpy(fbi->fix.id, "DRM emulated");
3193
Daniel Vetter3df31162019-03-26 14:19:48 +01003194 drm_fb_helper_fill_info(fbi, fb_helper, sizes);
Noralf Trønnesd5365402018-07-03 18:03:48 +02003195
3196 if (fb->funcs->dirty) {
3197 struct fb_ops *fbops;
3198 void *shadow;
3199
3200 /*
3201 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per
3202 * instance version is necessary.
3203 */
3204 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
3205 shadow = vzalloc(fbi->screen_size);
3206 if (!fbops || !shadow) {
3207 kfree(fbops);
3208 vfree(shadow);
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003209 return -ENOMEM;
Noralf Trønnesd5365402018-07-03 18:03:48 +02003210 }
3211
3212 *fbops = *fbi->fbops;
3213 fbi->fbops = fbops;
3214 fbi->screen_buffer = shadow;
3215 fbi->fbdefio = &drm_fbdev_defio;
3216
3217 fb_deferred_io_init(fbi);
3218 }
3219
3220 return 0;
Noralf Trønnesd5365402018-07-03 18:03:48 +02003221}
3222EXPORT_SYMBOL(drm_fb_helper_generic_probe);
3223
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003224static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
3225 .fb_probe = drm_fb_helper_generic_probe,
3226};
3227
3228static void drm_fbdev_client_unregister(struct drm_client_dev *client)
3229{
3230 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3231
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003232 if (fb_helper->fbdev)
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003233 /* drm_fbdev_fb_destroy() takes care of cleanup */
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003234 drm_fb_helper_unregister_fbi(fb_helper);
3235 else
3236 drm_fbdev_release(fb_helper);
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003237}
3238
3239static int drm_fbdev_client_restore(struct drm_client_dev *client)
3240{
Noralf Trønnes78de14c2019-01-25 16:03:00 +01003241 drm_fb_helper_lastclose(client->dev);
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003242
3243 return 0;
3244}
3245
3246static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
3247{
3248 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3249 struct drm_device *dev = client->dev;
3250 int ret;
3251
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003252 /* Setup is not retried if it has failed */
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003253 if (!fb_helper->dev && fb_helper->funcs)
3254 return 0;
3255
3256 if (dev->fb_helper)
3257 return drm_fb_helper_hotplug_event(dev->fb_helper);
3258
Peter Wuc10802b2018-09-07 00:18:10 +02003259 if (!dev->mode_config.num_connector) {
3260 DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n");
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003261 return 0;
Peter Wuc10802b2018-09-07 00:18:10 +02003262 }
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003263
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003264 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
3265
3266 ret = drm_fb_helper_init(dev, fb_helper, dev->mode_config.num_connector);
3267 if (ret)
3268 goto err;
3269
3270 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
3271 if (ret)
3272 goto err_cleanup;
3273
3274 if (!drm_drv_uses_atomic_modeset(dev))
3275 drm_helper_disable_unused_functions(dev);
3276
3277 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
3278 if (ret)
3279 goto err_cleanup;
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003280
3281 return 0;
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003282
3283err_cleanup:
3284 drm_fbdev_cleanup(fb_helper);
3285err:
3286 fb_helper->dev = NULL;
3287 fb_helper->fbdev = NULL;
3288
3289 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
3290
3291 return ret;
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003292}
3293
3294static const struct drm_client_funcs drm_fbdev_client_funcs = {
3295 .owner = THIS_MODULE,
3296 .unregister = drm_fbdev_client_unregister,
3297 .restore = drm_fbdev_client_restore,
3298 .hotplug = drm_fbdev_client_hotplug,
3299};
3300
3301/**
Peter Wuc10802b2018-09-07 00:18:10 +02003302 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003303 * @dev: DRM device
3304 * @preferred_bpp: Preferred bits per pixel for the device.
3305 * @dev->mode_config.preferred_depth is used if this is zero.
3306 *
3307 * This function sets up generic fbdev emulation for drivers that supports
Peter Wuc10802b2018-09-07 00:18:10 +02003308 * dumb buffers with a virtual address and that can be mmap'ed. If the driver
3309 * does not support these functions, it could use drm_fb_helper_fbdev_setup().
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003310 *
3311 * Restore, hotplug events and teardown are all taken care of. Drivers that do
3312 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
3313 * Simple drivers might use drm_mode_config_helper_suspend().
3314 *
3315 * Drivers that set the dirty callback on their framebuffer will get a shadow
3316 * fbdev buffer that is blitted onto the real buffer. This is done in order to
3317 * make deferred I/O work with all kinds of buffers.
3318 *
3319 * This function is safe to call even when there are no connectors present.
3320 * Setup will be retried on the next hotplug event.
3321 *
Peter Wuc10802b2018-09-07 00:18:10 +02003322 * The fbdev is destroyed by drm_dev_unregister().
3323 *
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003324 * Returns:
3325 * Zero on success or negative error code on failure.
3326 */
3327int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
3328{
3329 struct drm_fb_helper *fb_helper;
3330 int ret;
3331
Peter Wuc10802b2018-09-07 00:18:10 +02003332 WARN(dev->fb_helper, "fb_helper is already set!\n");
3333
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003334 if (!drm_fbdev_emulation)
3335 return 0;
3336
3337 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
3338 if (!fb_helper)
3339 return -ENOMEM;
3340
Noralf Trønnes4d4c2d82018-10-01 21:45:36 +02003341 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003342 if (ret) {
3343 kfree(fb_helper);
Peter Wuc10802b2018-09-07 00:18:10 +02003344 DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret);
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003345 return ret;
3346 }
3347
Noralf Trønnes4d4c2d82018-10-01 21:45:36 +02003348 drm_client_add(&fb_helper->client);
3349
Noralf Trønnes6e1490c2019-01-05 19:18:46 +01003350 if (!preferred_bpp)
3351 preferred_bpp = dev->mode_config.preferred_depth;
3352 if (!preferred_bpp)
3353 preferred_bpp = 32;
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003354 fb_helper->preferred_bpp = preferred_bpp;
3355
Peter Wuc10802b2018-09-07 00:18:10 +02003356 ret = drm_fbdev_client_hotplug(&fb_helper->client);
3357 if (ret)
3358 DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
Noralf Trønnes9060d7f2018-07-03 18:03:52 +02003359
3360 return 0;
3361}
3362EXPORT_SYMBOL(drm_fbdev_generic_setup);
3363
David Rientjes6a108a12011-01-20 14:44:16 -08003364/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06003365 * but the module doesn't depend on any fb console symbols. At least
3366 * attempt to load fbcon to avoid leaving the system without a usable console.
3367 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08003368int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06003369{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08003370#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07003371 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06003372 struct module *fbcon;
3373
3374 mutex_lock(&module_mutex);
3375 fbcon = find_module(name);
3376 mutex_unlock(&module_mutex);
3377
3378 if (!fbcon)
3379 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08003380#endif
David Fries3ce05162010-12-12 12:39:22 -06003381 return 0;
3382}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08003383EXPORT_SYMBOL(drm_fb_helper_modinit);