blob: 1915e64c6e6967af76f24bcc2d71ddb801e6a5d5 [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>
Andy Shevchenko3b40a442010-02-02 14:40:32 -080033#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100034#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_crtc_helper.h>
Rob Clarkbbb1e522015-08-25 15:35:58 -040041#include <drm/drm_atomic.h>
42#include <drm/drm_atomic_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100043
Ville Syrjälä699fbee2016-09-19 16:33:44 +030044#include "drm_crtc_helper_internal.h"
45
Daniel Vetterf64c5572015-08-25 15:45:13 +020046static bool drm_fbdev_emulation = true;
47module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48MODULE_PARM_DESC(fbdev_emulation,
49 "Enable legacy fbdev emulation [default=true]");
50
Xinliang Liu5f152572017-02-15 17:19:08 +010051static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
52module_param(drm_fbdev_overalloc, int, 0444);
53MODULE_PARM_DESC(drm_fbdev_overalloc,
54 "Overallocation of the fbdev buffer (%) [default="
55 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
56
Dave Airlie785b93e2009-08-28 15:46:53 +100057static LIST_HEAD(kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +000058static DEFINE_MUTEX(kernel_fb_helper_lock);
Dave Airlie785b93e2009-08-28 15:46:53 +100059
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010060/**
61 * DOC: fbdev helpers
62 *
63 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020064 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010065 * helper functions used by many drivers to implement the kernel mode setting
66 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010067 *
Thierry Reding10a23102014-06-27 17:19:24 +020068 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71 * default behaviour can override the third step with their own code.
Daniel Vettered84e252017-02-07 15:10:49 +010072 * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73 * unregisters using drm_fb_helper_unregister_fbi().
Daniel Vetter207fd322013-01-20 22:13:14 +010074 *
75 * At runtime drivers should restore the fbdev console by calling
Daniel Vetter6806cdf2017-01-25 07:26:43 +010076 * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77 * callback. They should also notify the fb helper code from updates to the
78 * output configuration by calling drm_fb_helper_hotplug_event(). For easier
Daniel Vetter207fd322013-01-20 22:13:14 +010079 * integration with the output polling code in drm_crtc_helper.c the modeset
Daniel Vetter6806cdf2017-01-25 07:26:43 +010080 * code provides a &drm_mode_config_funcs.output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010081 *
82 * All other functions exported by the fb helper library can be used to
83 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020084 *
85 * It is possible, though perhaps somewhat tricky, to implement race-free
86 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87 * helper must be called first to initialize the minimum required to make
88 * hotplug detection work. Drivers also need to make sure to properly set up
Daniel Vetter6806cdf2017-01-25 07:26:43 +010089 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
Thierry Reding10a23102014-06-27 17:19:24 +020090 * it is safe to enable interrupts and start processing hotplug events. At the
91 * same time, drivers should initialize all modeset objects such as CRTCs,
92 * encoders and connectors. To finish up the fbdev helper initialization, the
93 * drm_fb_helper_init() function is called. To probe for all attached displays
94 * and set up an initial configuration using the detected hardware, drivers
95 * should call drm_fb_helper_single_add_all_connectors() followed by
96 * drm_fb_helper_initial_config().
Noralf Trønneseaa434d2016-04-28 17:18:33 +020097 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +010098 * If &drm_framebuffer_funcs.dirty is set, the
Noralf Trønnes2dad5512016-05-11 18:09:17 +020099 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100100 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
Noralf Trønnes2dad5512016-05-11 18:09:17 +0200101 * away. This worker then calls the dirty() function ensuring that it will
102 * always run in process context since the fb_*() function could be running in
103 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
104 * callback it will also schedule dirty_work with the damage collected from the
105 * mmap page writes.
Daniel Vetterd0ddc0332012-11-01 14:45:17 +0100106 */
107
Chris Wilson966a6a12016-11-29 12:02:15 +0000108#define drm_fb_helper_for_each_connector(fbh, i__) \
Daniel Vettere13a0582017-07-05 06:56:29 +0200109 for (({ lockdep_assert_held(&(fbh)->lock); }), \
Chris Wilson966a6a12016-11-29 12:02:15 +0000110 i__ = 0; i__ < (fbh)->connector_count; i__++)
111
Thierry Redingaf2405a2017-07-04 17:18:21 +0200112static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
113 struct drm_connector *connector)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200114{
Thierry Reding50021ff2017-03-29 16:43:53 +0200115 struct drm_fb_helper_connector *fb_conn;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200116 struct drm_fb_helper_connector **temp;
Thierry Reding50021ff2017-03-29 16:43:53 +0200117 unsigned int count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200118
119 if (!drm_fbdev_emulation)
120 return 0;
121
Thierry Redinge9827d82017-07-04 17:18:23 +0200122 lockdep_assert_held(&fb_helper->lock);
Thierry Reding50021ff2017-03-29 16:43:53 +0200123
124 count = fb_helper->connector_count + 1;
125
126 if (count > fb_helper->connector_info_alloc_count) {
127 size_t size = count * sizeof(fb_conn);
128
129 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200130 if (!temp)
131 return -ENOMEM;
132
Thierry Reding50021ff2017-03-29 16:43:53 +0200133 fb_helper->connector_info_alloc_count = count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200134 fb_helper->connector_info = temp;
135 }
136
Thierry Reding50021ff2017-03-29 16:43:53 +0200137 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
138 if (!fb_conn)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200139 return -ENOMEM;
140
141 drm_connector_get(connector);
Thierry Reding50021ff2017-03-29 16:43:53 +0200142 fb_conn->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
Thierry Redingaf2405a2017-07-04 17:18:21 +0200144
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200145 return 0;
146}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200147
148int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
149 struct drm_connector *connector)
150{
151 int err;
152
Thierry Redinge9827d82017-07-04 17:18:23 +0200153 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200154 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200155 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200156
157 return err;
158}
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200159EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
160
Daniel Vetter207fd322013-01-20 22:13:14 +0100161/**
162 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
163 * emulation helper
164 * @fb_helper: fbdev initialized with drm_fb_helper_init
165 *
166 * This functions adds all the available connectors for use with the given
167 * fb_helper. This is a separate step to allow drivers to freely assign
168 * connectors to the fbdev, e.g. if some are reserved for special purposes or
169 * not adequate to be used for the fbcon.
170 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200171 * This function is protected against concurrent connector hotadds/removals
172 * using drm_fb_helper_add_one_connector() and
173 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100174 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000175int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000176{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000177 struct drm_device *dev = fb_helper->dev;
178 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100179 struct drm_connector_list_iter conn_iter;
180 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000181
Daniel Vetterf64c5572015-08-25 15:45:13 +0200182 if (!drm_fbdev_emulation)
183 return 0;
184
Thierry Redinge9827d82017-07-04 17:18:23 +0200185 mutex_lock(&fb_helper->lock);
Thierry Redingb982dab2017-02-28 15:46:43 +0100186 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100187 drm_for_each_connector_iter(connector, &conn_iter) {
Thierry Redingaf2405a2017-07-04 17:18:21 +0200188 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100189 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000190 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000191 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100192 goto out;
193
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000194fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000195 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300196 struct drm_fb_helper_connector *fb_helper_connector =
197 fb_helper->connector_info[i];
198
Thierry Redingad093602017-02-28 15:46:39 +0100199 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300200
201 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000202 fb_helper->connector_info[i] = NULL;
203 }
204 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100205out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100206 drm_connector_list_iter_end(&conn_iter);
Thierry Redinge9827d82017-07-04 17:18:23 +0200207 mutex_unlock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200208
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100209 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000210}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000211EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000212
Thierry Redingaf2405a2017-07-04 17:18:21 +0200213static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
214 struct drm_connector *connector)
Dave Airlie65c2a892014-06-05 14:01:30 +1000215{
216 struct drm_fb_helper_connector *fb_helper_connector;
217 int i, j;
218
Daniel Vetterf64c5572015-08-25 15:45:13 +0200219 if (!drm_fbdev_emulation)
220 return 0;
221
Thierry Redinge9827d82017-07-04 17:18:23 +0200222 lockdep_assert_held(&fb_helper->lock);
Dave Airlie65c2a892014-06-05 14:01:30 +1000223
Thierry Redinge9827d82017-07-04 17:18:23 +0200224 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie65c2a892014-06-05 14:01:30 +1000225 if (fb_helper->connector_info[i]->connector == connector)
226 break;
227 }
228
229 if (i == fb_helper->connector_count)
230 return -EINVAL;
231 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100232 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000233
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200234 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000235 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200236
Dave Airlie65c2a892014-06-05 14:01:30 +1000237 fb_helper->connector_count--;
238 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500239
Dave Airlie65c2a892014-06-05 14:01:30 +1000240 return 0;
241}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200242
243int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
244 struct drm_connector *connector)
245{
246 int err;
247
Thierry Redinge9827d82017-07-04 17:18:23 +0200248 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200249 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200250 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200251
252 return err;
253}
Dave Airlie65c2a892014-06-05 14:01:30 +1000254EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
255
Jason Wessel99231022010-10-13 14:09:43 -0500256static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
257{
258 uint16_t *r_base, *g_base, *b_base;
259 int i;
260
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300261 if (helper->funcs->gamma_get == NULL)
262 return;
263
Jason Wessel99231022010-10-13 14:09:43 -0500264 r_base = crtc->gamma_store;
265 g_base = r_base + crtc->gamma_size;
266 b_base = g_base + crtc->gamma_size;
267
268 for (i = 0; i < crtc->gamma_size; i++)
269 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
270}
271
272static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
273{
274 uint16_t *r_base, *g_base, *b_base;
275
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200276 if (crtc->funcs->gamma_set == NULL)
277 return;
278
Jason Wessel99231022010-10-13 14:09:43 -0500279 r_base = crtc->gamma_store;
280 g_base = r_base + crtc->gamma_size;
281 b_base = g_base + crtc->gamma_size;
282
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200283 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
284 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500285}
286
Daniel Vetter207fd322013-01-20 22:13:14 +0100287/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100288 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100289 * @info: fbdev registered by the helper
290 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500291int drm_fb_helper_debug_enter(struct fb_info *info)
292{
293 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200294 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500295 int i;
296
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500297 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
298 for (i = 0; i < helper->crtc_count; i++) {
299 struct drm_mode_set *mode_set =
300 &helper->crtc_info[i].mode_set;
301
302 if (!mode_set->crtc->enabled)
303 continue;
304
305 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100306 if (funcs->mode_set_base_atomic == NULL)
307 continue;
308
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200309 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
310 continue;
311
Jason Wessel99231022010-10-13 14:09:43 -0500312 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500313 funcs->mode_set_base_atomic(mode_set->crtc,
314 mode_set->fb,
315 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500316 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500317 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500318 }
319 }
320
321 return 0;
322}
323EXPORT_SYMBOL(drm_fb_helper_debug_enter);
324
Daniel Vetter207fd322013-01-20 22:13:14 +0100325/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100326 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100327 * @info: fbdev registered by the helper
328 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500329int drm_fb_helper_debug_leave(struct fb_info *info)
330{
331 struct drm_fb_helper *helper = info->par;
332 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200333 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500334 struct drm_framebuffer *fb;
335 int i;
336
337 for (i = 0; i < helper->crtc_count; i++) {
338 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200339
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500340 crtc = mode_set->crtc;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200341 if (drm_drv_uses_atomic_modeset(crtc->dev))
342 continue;
343
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500344 funcs = crtc->helper_private;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200345 fb = crtc->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500346
347 if (!crtc->enabled)
348 continue;
349
350 if (!fb) {
351 DRM_ERROR("no fb to restore??\n");
352 continue;
353 }
354
Stefan Christ1b99b722016-11-14 00:03:11 +0100355 if (funcs->mode_set_base_atomic == NULL)
356 continue;
357
Jason Wessel99231022010-10-13 14:09:43 -0500358 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500359 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500360 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500361 }
362
363 return 0;
364}
365EXPORT_SYMBOL(drm_fb_helper_debug_leave);
366
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200367static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
Rob Clarkbbb1e522015-08-25 15:35:58 -0400368{
369 struct drm_device *dev = fb_helper->dev;
370 struct drm_plane *plane;
371 struct drm_atomic_state *state;
372 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200373 unsigned int plane_mask;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200374 struct drm_modeset_acquire_ctx ctx;
375
376 drm_modeset_acquire_init(&ctx, 0);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400377
378 state = drm_atomic_state_alloc(dev);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200379 if (!state) {
380 ret = -ENOMEM;
381 goto out_ctx;
382 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400383
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200384 state->acquire_ctx = &ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400385retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100386 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400387 drm_for_each_plane(plane, dev) {
388 struct drm_plane_state *plane_state;
389
390 plane_state = drm_atomic_get_plane_state(state, plane);
391 if (IS_ERR(plane_state)) {
392 ret = PTR_ERR(plane_state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200393 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400394 }
395
Robert Fossc2c446a2017-05-19 16:50:17 -0400396 plane_state->rotation = DRM_MODE_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400397
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100398 plane->old_fb = plane->fb;
399 plane_mask |= 1 << drm_plane_index(plane);
400
Rob Clarkbbb1e522015-08-25 15:35:58 -0400401 /* disable non-primary: */
402 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
403 continue;
404
405 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
406 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200407 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400408 }
409
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200410 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400411 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
412
413 ret = __drm_atomic_helper_set_config(mode_set, state);
414 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200415 goto out_state;
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200416
417 /*
418 * __drm_atomic_helper_set_config() sets active when a
419 * mode is set, unconditionally clear it if we force DPMS off
420 */
421 if (!active) {
422 struct drm_crtc *crtc = mode_set->crtc;
423 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
424
425 crtc_state->active = false;
426 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400427 }
428
429 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400430
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200431out_state:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100432 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700433
Rob Clarkbbb1e522015-08-25 15:35:58 -0400434 if (ret == -EDEADLK)
435 goto backoff;
436
Chris Wilson08536952016-10-14 13:18:18 +0100437 drm_atomic_state_put(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200438out_ctx:
439 drm_modeset_drop_locks(&ctx);
440 drm_modeset_acquire_fini(&ctx);
441
Rob Clarkbbb1e522015-08-25 15:35:58 -0400442 return ret;
443
444backoff:
445 drm_atomic_state_clear(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200446 drm_modeset_backoff(&ctx);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400447
448 goto retry;
449}
450
Daniel Vetter71286452017-04-03 10:33:04 +0200451static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100452{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300453 struct drm_device *dev = fb_helper->dev;
454 struct drm_plane *plane;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200455 int i, ret = 0;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100456
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200457 drm_modeset_lock_all(fb_helper->dev);
Daniel Vetter6295d602015-07-09 23:44:25 +0200458 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700459 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
460 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100461
Ville Syrjälä6686df82016-10-21 22:22:45 +0300462 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300463 drm_mode_plane_set_obj_prop(plane,
464 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400465 DRM_MODE_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530466 }
467
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100468 for (i = 0; i < fb_helper->crtc_count; i++) {
469 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300470 struct drm_crtc *crtc = mode_set->crtc;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300471
Alex Deucher03f9abb2015-09-30 14:47:37 -0400472 if (crtc->funcs->cursor_set2) {
473 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
474 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200475 goto out;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400476 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300477 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
478 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200479 goto out;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300480 }
481
Daniel Vetter2d13b672012-12-11 13:47:23 +0100482 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100483 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200484 goto out;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100485 }
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200486out:
487 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200488
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200489 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100490}
Rob Clark5ea1f752014-05-30 12:29:48 -0400491
Daniel Vetter71286452017-04-03 10:33:04 +0200492static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
493{
494 struct drm_device *dev = fb_helper->dev;
495
Daniel Vetter71286452017-04-03 10:33:04 +0200496 if (drm_drv_uses_atomic_modeset(dev))
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200497 return restore_fbdev_mode_atomic(fb_helper, true);
Daniel Vetter71286452017-04-03 10:33:04 +0200498 else
499 return restore_fbdev_mode_legacy(fb_helper);
500}
501
Rob Clark5ea1f752014-05-30 12:29:48 -0400502/**
503 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
504 * @fb_helper: fbcon to restore
505 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100506 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400507 * when implementing an fbcon on top of kms using this helper. This ensures that
508 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200509 *
510 * RETURNS:
511 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400512 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200513int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400514{
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200515 bool do_delayed;
516 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000517
Daniel Vetterf64c5572015-08-25 15:45:13 +0200518 if (!drm_fbdev_emulation)
519 return -ENODEV;
520
Thierry Redinge9827d82017-07-04 17:18:23 +0200521 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400522 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000523
524 do_delayed = fb_helper->delayed_hotplug;
525 if (do_delayed)
526 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200527 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000528
529 if (do_delayed)
530 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200531
Rob Clark5ea1f752014-05-30 12:29:48 -0400532 return ret;
533}
534EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100535
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200536static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
537{
538 struct drm_device *dev = fb_helper->dev;
539 struct drm_crtc *crtc;
540 int bound = 0, crtcs_bound = 0;
541
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200542 /*
543 * Sometimes user space wants everything disabled, so don't steal the
544 * display if there's a master.
545 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200546 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200547 return false;
548
549 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200550 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200551 if (crtc->primary->fb)
552 crtcs_bound++;
553 if (crtc->primary->fb == fb_helper->fb)
554 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200555 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200556 }
557
558 if (bound < crtcs_bound)
559 return false;
560
561 return true;
562}
563
564#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100565/*
566 * restore fbcon display for all kms driver's using this helper, used for sysrq
567 * and panic handling.
568 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530569static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000570{
Dave Airlie785b93e2009-08-28 15:46:53 +1000571 bool ret, error = false;
572 struct drm_fb_helper *helper;
573
574 if (list_empty(&kernel_fb_helper_list))
575 return false;
576
577 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200578 struct drm_device *dev = helper->dev;
579
580 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100581 continue;
582
Thierry Redinge9827d82017-07-04 17:18:23 +0200583 mutex_lock(&helper->lock);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200584 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100585 if (ret)
586 error = true;
Thierry Redinge9827d82017-07-04 17:18:23 +0200587 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000588 }
589 return error;
590}
591
Dave Airlie785b93e2009-08-28 15:46:53 +1000592static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
593{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100594 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200595
Daniel Vetterd21bf462013-01-20 18:09:52 +0100596 ret = drm_fb_helper_force_kernel_mode();
597 if (ret == true)
598 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000599}
600static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
601
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700602static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000603{
604 schedule_work(&drm_fb_helper_restore_work);
605}
606
607static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
608 .handler = drm_fb_helper_sysrq,
609 .help_msg = "force-fb(V)",
610 .action_msg = "Restore framebuffer console",
611};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000612#else
613static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200614#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000615
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200616static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000617{
Dave Airlie785b93e2009-08-28 15:46:53 +1000618 struct drm_device *dev = fb_helper->dev;
619 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700620 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700621 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000622
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200623 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700624 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000625 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000626
Dave Airlie8be48d92010-03-30 05:34:14 +0000627 if (!crtc->enabled)
628 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000629
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100630 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000631 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700632 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200633 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500634 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100635 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700636 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000637 }
Daniel Vetter84849902012-12-02 00:28:11 +0100638 drm_modeset_unlock_all(dev);
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200639}
640
641static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
642{
643 struct drm_fb_helper *fb_helper = info->par;
644
645 /*
646 * For each CRTC in this fb, turn the connectors on/off.
647 */
648 mutex_lock(&fb_helper->lock);
649 if (!drm_fb_helper_is_bound(fb_helper)) {
650 mutex_unlock(&fb_helper->lock);
651 return;
652 }
653
654 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
655 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
656 else
657 dpms_legacy(fb_helper, dpms_mode);
Thierry Redinge9827d82017-07-04 17:18:23 +0200658 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000659}
660
Daniel Vetter207fd322013-01-20 22:13:14 +0100661/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100662 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100663 * @blank: desired blanking state
664 * @info: fbdev registered by the helper
665 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000666int drm_fb_helper_blank(int blank, struct fb_info *info)
667{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200668 if (oops_in_progress)
669 return -EBUSY;
670
Dave Airlie785b93e2009-08-28 15:46:53 +1000671 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000672 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000673 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100674 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000675 break;
James Simmons731b5a12009-10-29 20:39:07 +0000676 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000677 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100678 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000679 break;
James Simmons731b5a12009-10-29 20:39:07 +0000680 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000681 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100682 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000683 break;
James Simmons731b5a12009-10-29 20:39:07 +0000684 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000685 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100686 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000687 break;
James Simmons731b5a12009-10-29 20:39:07 +0000688 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000689 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100690 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000691 break;
692 }
693 return 0;
694}
695EXPORT_SYMBOL(drm_fb_helper_blank);
696
Ville Syrjäläa2889602016-10-26 17:41:18 +0300697static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
698 struct drm_mode_set *modeset)
699{
700 int i;
701
702 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100703 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300704 modeset->connectors[i] = NULL;
705 }
706 modeset->num_connectors = 0;
707
708 drm_mode_destroy(helper->dev, modeset->mode);
709 modeset->mode = NULL;
710
711 /* FIXME should hold a ref? */
712 modeset->fb = NULL;
713}
714
Dave Airlie785b93e2009-08-28 15:46:53 +1000715static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
716{
717 int i;
718
Dave Airlie6e86d582016-04-27 11:24:51 +1000719 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100720 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000721 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000722 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000723 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300724
Sascha Hauera1b77362012-02-01 11:38:22 +0100725 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300726 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
727
728 drm_fb_helper_modeset_release(helper, modeset);
729 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100730 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000731 kfree(helper->crtc_info);
732}
733
Noralf Trønnescfe63422016-08-23 13:54:06 +0200734static void drm_fb_helper_resume_worker(struct work_struct *work)
735{
736 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
737 resume_work);
738
739 console_lock();
740 fb_set_suspend(helper->fbdev, 0);
741 console_unlock();
742}
743
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200744static void drm_fb_helper_dirty_work(struct work_struct *work)
745{
746 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
747 dirty_work);
748 struct drm_clip_rect *clip = &helper->dirty_clip;
749 struct drm_clip_rect clip_copy;
750 unsigned long flags;
751
752 spin_lock_irqsave(&helper->dirty_lock, flags);
753 clip_copy = *clip;
754 clip->x1 = clip->y1 = ~0;
755 clip->x2 = clip->y2 = 0;
756 spin_unlock_irqrestore(&helper->dirty_lock, flags);
757
Takashi Iwai87d3b652016-10-20 17:05:30 +0200758 /* call dirty callback only when it has been really touched */
759 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
760 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200761}
762
Daniel Vetter207fd322013-01-20 22:13:14 +0100763/**
Thierry Reding10a23102014-06-27 17:19:24 +0200764 * drm_fb_helper_prepare - setup a drm_fb_helper structure
765 * @dev: DRM device
766 * @helper: driver-allocated fbdev helper structure to set up
767 * @funcs: pointer to structure of functions associate with this helper
768 *
769 * Sets up the bare minimum to make the framebuffer helper usable. This is
770 * useful to implement race-free initialization of the polling helpers.
771 */
772void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
773 const struct drm_fb_helper_funcs *funcs)
774{
775 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200776 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200777 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200778 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
779 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200780 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200781 helper->funcs = funcs;
782 helper->dev = dev;
783}
784EXPORT_SYMBOL(drm_fb_helper_prepare);
785
786/**
Daniel Vettered84e252017-02-07 15:10:49 +0100787 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100788 * @dev: drm device
789 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100790 * @max_conn_count: max connector count
791 *
792 * This allocates the structures for the fbdev helper with the given limits.
793 * Note that this won't yet touch the hardware (through the driver interfaces)
794 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
795 * to allow driver writes more control over the exact init sequence.
796 *
Thierry Reding10a23102014-06-27 17:19:24 +0200797 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100798 *
799 * RETURNS:
800 * Zero if everything went ok, nonzero otherwise.
801 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000802int drm_fb_helper_init(struct drm_device *dev,
803 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200804 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000805{
Dave Airlie785b93e2009-08-28 15:46:53 +1000806 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200807 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000808 int i;
809
Daniel Vetterf64c5572015-08-25 15:45:13 +0200810 if (!drm_fbdev_emulation)
811 return 0;
812
Xiubo Li04cfe972014-03-10 09:33:58 +0800813 if (!max_conn_count)
814 return -EINVAL;
815
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200816 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000817 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000818 return -ENOMEM;
819
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200820 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000821 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
822 if (!fb_helper->connector_info) {
823 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000824 return -ENOMEM;
825 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000826 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000827 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000828
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200829 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000830 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000831 kcalloc(max_conn_count,
832 sizeof(struct drm_connector *),
833 GFP_KERNEL);
834
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200835 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000836 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000837 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000838 }
839
840 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200841 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000842 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000843 i++;
844 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100845
Dave Airlie785b93e2009-08-28 15:46:53 +1000846 return 0;
847out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000848 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000849 return -ENOMEM;
850}
Dave Airlie4abe3522010-03-30 05:34:18 +0000851EXPORT_SYMBOL(drm_fb_helper_init);
852
Archit Tanejab8017d62015-07-22 14:57:56 +0530853/**
854 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
855 * @fb_helper: driver-allocated fbdev helper
856 *
857 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100858 * by the driver within the fb_probe fb_helper callback function. Drivers do not
859 * need to release the allocated fb_info structure themselves, this is
860 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530861 *
862 * RETURNS:
863 * fb_info pointer if things went okay, pointer containing error code
864 * otherwise
865 */
866struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
867{
868 struct device *dev = fb_helper->dev->dev;
869 struct fb_info *info;
870 int ret;
871
872 info = framebuffer_alloc(0, dev);
873 if (!info)
874 return ERR_PTR(-ENOMEM);
875
876 ret = fb_alloc_cmap(&info->cmap, 256, 0);
877 if (ret)
878 goto err_release;
879
880 info->apertures = alloc_apertures(1);
881 if (!info->apertures) {
882 ret = -ENOMEM;
883 goto err_free_cmap;
884 }
885
886 fb_helper->fbdev = info;
887
888 return info;
889
890err_free_cmap:
891 fb_dealloc_cmap(&info->cmap);
892err_release:
893 framebuffer_release(info);
894 return ERR_PTR(ret);
895}
896EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
897
898/**
899 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
900 * @fb_helper: driver-allocated fbdev helper
901 *
902 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100903 * framebuffer device. This must be called before releasing all resources for
904 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530905 */
906void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
907{
908 if (fb_helper && fb_helper->fbdev)
909 unregister_framebuffer(fb_helper->fbdev);
910}
911EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
912
913/**
Daniel Vettered84e252017-02-07 15:10:49 +0100914 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
915 * @fb_helper: driver-allocated fbdev helper
916 *
917 * This cleans up all remaining resources associated with @fb_helper. Must be
918 * called after drm_fb_helper_unlink_fbi() was called.
919 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000920void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
921{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100922 struct fb_info *info;
923
924 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200925 return;
926
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100927 info = fb_helper->fbdev;
928 if (info) {
929 if (info->cmap.len)
930 fb_dealloc_cmap(&info->cmap);
931 framebuffer_release(info);
932 }
933 fb_helper->fbdev = NULL;
934
Chris Wilson24f76b22017-02-07 12:49:56 +0000935 cancel_work_sync(&fb_helper->resume_work);
Chris Wilsonf21b9a92017-02-07 12:49:55 +0000936 cancel_work_sync(&fb_helper->dirty_work);
937
Chris Wilsona53ca632016-11-29 12:02:17 +0000938 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000939 if (!list_empty(&fb_helper->kernel_fb_list)) {
940 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200941 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000942 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000943 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000944 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000945
Thierry Redinge9827d82017-07-04 17:18:23 +0200946 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000947 drm_fb_helper_crtc_free(fb_helper);
948
Dave Airlie4abe3522010-03-30 05:34:18 +0000949}
950EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000951
Archit Taneja47074ab2015-07-22 14:57:57 +0530952/**
953 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
954 * @fb_helper: driver-allocated fbdev helper
955 *
956 * A wrapper around unlink_framebuffer implemented by fbdev core
957 */
958void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
959{
960 if (fb_helper && fb_helper->fbdev)
961 unlink_framebuffer(fb_helper->fbdev);
962}
963EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
964
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200965static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
966 u32 width, u32 height)
967{
968 struct drm_fb_helper *helper = info->par;
969 struct drm_clip_rect *clip = &helper->dirty_clip;
970 unsigned long flags;
971
972 if (!helper->fb->funcs->dirty)
973 return;
974
975 spin_lock_irqsave(&helper->dirty_lock, flags);
976 clip->x1 = min_t(u32, clip->x1, x);
977 clip->y1 = min_t(u32, clip->y1, y);
978 clip->x2 = max_t(u32, clip->x2, x + width);
979 clip->y2 = max_t(u32, clip->y2, y + height);
980 spin_unlock_irqrestore(&helper->dirty_lock, flags);
981
982 schedule_work(&helper->dirty_work);
983}
984
985/**
986 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
987 * @info: fb_info struct pointer
988 * @pagelist: list of dirty mmap framebuffer pages
989 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100990 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200991 * callback function for flushing the fbdev mmap writes.
992 */
993void drm_fb_helper_deferred_io(struct fb_info *info,
994 struct list_head *pagelist)
995{
996 unsigned long start, end, min, max;
997 struct page *page;
998 u32 y1, y2;
999
1000 min = ULONG_MAX;
1001 max = 0;
1002 list_for_each_entry(page, pagelist, lru) {
1003 start = page->index << PAGE_SHIFT;
1004 end = start + PAGE_SIZE - 1;
1005 min = min(min, start);
1006 max = max(max, end);
1007 }
1008
1009 if (min < max) {
1010 y1 = min / info->fix.line_length;
1011 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1012 info->var.yres);
1013 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1014 }
1015}
1016EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1017
Archit Tanejacbb1a822015-07-31 16:21:41 +05301018/**
1019 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1020 * @info: fb_info struct pointer
1021 * @buf: userspace buffer to read from framebuffer memory
1022 * @count: number of bytes to read from framebuffer memory
1023 * @ppos: read offset within framebuffer memory
1024 *
1025 * A wrapper around fb_sys_read implemented by fbdev core
1026 */
1027ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1028 size_t count, loff_t *ppos)
1029{
1030 return fb_sys_read(info, buf, count, ppos);
1031}
1032EXPORT_SYMBOL(drm_fb_helper_sys_read);
1033
1034/**
1035 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1036 * @info: fb_info struct pointer
1037 * @buf: userspace buffer to write to framebuffer memory
1038 * @count: number of bytes to write to framebuffer memory
1039 * @ppos: write offset within framebuffer memory
1040 *
1041 * A wrapper around fb_sys_write implemented by fbdev core
1042 */
1043ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1044 size_t count, loff_t *ppos)
1045{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001046 ssize_t ret;
1047
1048 ret = fb_sys_write(info, buf, count, ppos);
1049 if (ret > 0)
1050 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1051 info->var.yres);
1052
1053 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301054}
1055EXPORT_SYMBOL(drm_fb_helper_sys_write);
1056
Archit Taneja742547b2015-07-31 16:21:42 +05301057/**
1058 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1059 * @info: fbdev registered by the helper
1060 * @rect: info about rectangle to fill
1061 *
1062 * A wrapper around sys_fillrect implemented by fbdev core
1063 */
1064void drm_fb_helper_sys_fillrect(struct fb_info *info,
1065 const struct fb_fillrect *rect)
1066{
1067 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001068 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1069 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301070}
1071EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1072
1073/**
1074 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1075 * @info: fbdev registered by the helper
1076 * @area: info about area to copy
1077 *
1078 * A wrapper around sys_copyarea implemented by fbdev core
1079 */
1080void drm_fb_helper_sys_copyarea(struct fb_info *info,
1081 const struct fb_copyarea *area)
1082{
1083 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001084 drm_fb_helper_dirty(info, area->dx, area->dy,
1085 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301086}
1087EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1088
1089/**
1090 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1091 * @info: fbdev registered by the helper
1092 * @image: info about image to blit
1093 *
1094 * A wrapper around sys_imageblit implemented by fbdev core
1095 */
1096void drm_fb_helper_sys_imageblit(struct fb_info *info,
1097 const struct fb_image *image)
1098{
1099 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001100 drm_fb_helper_dirty(info, image->dx, image->dy,
1101 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301102}
1103EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1104
1105/**
1106 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1107 * @info: fbdev registered by the helper
1108 * @rect: info about rectangle to fill
1109 *
1110 * A wrapper around cfb_imageblit implemented by fbdev core
1111 */
1112void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1113 const struct fb_fillrect *rect)
1114{
1115 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001116 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1117 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301118}
1119EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1120
1121/**
1122 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1123 * @info: fbdev registered by the helper
1124 * @area: info about area to copy
1125 *
1126 * A wrapper around cfb_copyarea implemented by fbdev core
1127 */
1128void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1129 const struct fb_copyarea *area)
1130{
1131 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001132 drm_fb_helper_dirty(info, area->dx, area->dy,
1133 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301134}
1135EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1136
1137/**
1138 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1139 * @info: fbdev registered by the helper
1140 * @image: info about image to blit
1141 *
1142 * A wrapper around cfb_imageblit implemented by fbdev core
1143 */
1144void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1145 const struct fb_image *image)
1146{
1147 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001148 drm_fb_helper_dirty(info, image->dx, image->dy,
1149 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301150}
1151EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1152
Archit Tanejafdefa582015-07-31 16:21:43 +05301153/**
1154 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1155 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001156 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301157 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001158 * A wrapper around fb_set_suspend implemented by fbdev core.
1159 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1160 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301161 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001162void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301163{
1164 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001165 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301166}
1167EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1168
Noralf Trønnescfe63422016-08-23 13:54:06 +02001169/**
1170 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1171 * takes the console lock
1172 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001173 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001174 *
1175 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1176 * isn't available on resume, a worker is tasked with waiting for the lock
1177 * to become available. The console lock can be pretty contented on resume
1178 * due to all the printk activity.
1179 *
1180 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001181 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001182 *
1183 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1184 */
1185void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001186 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001187{
1188 if (!fb_helper || !fb_helper->fbdev)
1189 return;
1190
1191 /* make sure there's no pending/ongoing resume */
1192 flush_work(&fb_helper->resume_work);
1193
1194 if (suspend) {
1195 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1196 return;
1197
1198 console_lock();
1199
1200 } else {
1201 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1202 return;
1203
1204 if (!console_trylock()) {
1205 schedule_work(&fb_helper->resume_work);
1206 return;
1207 }
1208 }
1209
1210 fb_set_suspend(fb_helper->fbdev, suspend);
1211 console_unlock();
1212}
1213EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1214
Dave Airliec850cb72009-10-23 18:49:03 +10001215static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001216 u16 blue, u16 regno, struct fb_info *info)
1217{
1218 struct drm_fb_helper *fb_helper = info->par;
1219 struct drm_framebuffer *fb = fb_helper->fb;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001220
Dave Airliec850cb72009-10-23 18:49:03 +10001221 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1222 u32 *palette;
1223 u32 value;
1224 /* place color in psuedopalette */
1225 if (regno > 16)
1226 return -EINVAL;
1227 palette = (u32 *)info->pseudo_palette;
1228 red >>= (16 - info->var.red.length);
1229 green >>= (16 - info->var.green.length);
1230 blue >>= (16 - info->var.blue.length);
1231 value = (red << info->var.red.offset) |
1232 (green << info->var.green.offset) |
1233 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +00001234 if (info->var.transp.length > 0) {
1235 u32 mask = (1 << info->var.transp.length) - 1;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001236
Rob Clark9da12b6a2011-02-16 02:45:51 +00001237 mask <<= info->var.transp.offset;
1238 value |= mask;
1239 }
Dave Airliec850cb72009-10-23 18:49:03 +10001240 palette[regno] = value;
1241 return 0;
1242 }
1243
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001244 /*
1245 * The driver really shouldn't advertise pseudo/directcolor
1246 * visuals if it can't deal with the palette.
1247 */
1248 if (WARN_ON(!fb_helper->funcs->gamma_set ||
1249 !fb_helper->funcs->gamma_get))
1250 return -EINVAL;
1251
Ville Syrjälä272725c2016-12-14 23:32:20 +02001252 WARN_ON(fb->format->cpp[0] != 1);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001253
Daniel Vetterfef14802016-03-30 11:51:17 +02001254 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001255
Dave Airliec850cb72009-10-23 18:49:03 +10001256 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001257}
1258
Daniel Vetter207fd322013-01-20 22:13:14 +01001259/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001260 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
Daniel Vetter207fd322013-01-20 22:13:14 +01001261 * @cmap: cmap to set
1262 * @info: fbdev registered by the helper
1263 */
Dave Airlie068143d2009-10-05 09:58:02 +10001264int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1265{
1266 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001267 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +02001268 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +10001269 u16 *red, *green, *blue, *transp;
1270 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +01001271 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001272 int start;
1273
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001274 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001275 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001276
Thierry Redinge9827d82017-07-04 17:18:23 +02001277 mutex_lock(&fb_helper->lock);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001278 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001279 mutex_unlock(&fb_helper->lock);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001280 return -EBUSY;
1281 }
1282
Daniel Vetterbdac4a02017-07-04 17:18:24 +02001283 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001284 for (i = 0; i < fb_helper->crtc_count; i++) {
1285 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1286 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +10001287
1288 red = cmap->red;
1289 green = cmap->green;
1290 blue = cmap->blue;
1291 transp = cmap->transp;
1292 start = cmap->start;
1293
roel062ac622011-03-07 18:00:34 +01001294 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +10001295 u16 hred, hgreen, hblue, htransp = 0xffff;
1296
1297 hred = *red++;
1298 hgreen = *green++;
1299 hblue = *blue++;
1300
1301 if (transp)
1302 htransp = *transp++;
1303
Dave Airliec850cb72009-10-23 18:49:03 +10001304 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1305 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001306 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +10001307 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001308 if (crtc_funcs->load_lut)
1309 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +10001310 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001311 out:
1312 drm_modeset_unlock_all(dev);
Thierry Redinge9827d82017-07-04 17:18:23 +02001313 mutex_unlock(&fb_helper->lock);
Dave Airlie068143d2009-10-05 09:58:02 +10001314 return rc;
1315}
1316EXPORT_SYMBOL(drm_fb_helper_setcmap);
1317
Daniel Vetter207fd322013-01-20 22:13:14 +01001318/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001319 * drm_fb_helper_ioctl - legacy ioctl implementation
1320 * @info: fbdev registered by the helper
1321 * @cmd: ioctl command
1322 * @arg: ioctl argument
1323 *
1324 * A helper to implement the standard fbdev ioctl. Only
1325 * FBIO_WAITFORVSYNC is implemented for now.
1326 */
1327int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1328 unsigned long arg)
1329{
1330 struct drm_fb_helper *fb_helper = info->par;
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001331 struct drm_mode_set *mode_set;
1332 struct drm_crtc *crtc;
1333 int ret = 0;
1334
Thierry Redinge9827d82017-07-04 17:18:23 +02001335 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001336 if (!drm_fb_helper_is_bound(fb_helper)) {
1337 ret = -EBUSY;
1338 goto unlock;
1339 }
1340
1341 switch (cmd) {
1342 case FBIO_WAITFORVSYNC:
1343 /*
1344 * Only consider the first CRTC.
1345 *
1346 * This ioctl is supposed to take the CRTC number as
1347 * an argument, but in fbdev times, what that number
1348 * was supposed to be was quite unclear, different
1349 * drivers were passing that argument differently
1350 * (some by reference, some by value), and most of the
1351 * userspace applications were just hardcoding 0 as an
1352 * argument.
1353 *
1354 * The first CRTC should be the integrated panel on
1355 * most drivers, so this is the best choice we can
1356 * make. If we're not smart enough here, one should
1357 * just consider switch the userspace to KMS.
1358 */
1359 mode_set = &fb_helper->crtc_info[0].mode_set;
1360 crtc = mode_set->crtc;
1361
1362 /*
1363 * Only wait for a vblank event if the CRTC is
1364 * enabled, otherwise just don't do anythintg,
1365 * not even report an error.
1366 */
1367 ret = drm_crtc_vblank_get(crtc);
1368 if (!ret) {
1369 drm_crtc_wait_one_vblank(crtc);
1370 drm_crtc_vblank_put(crtc);
1371 }
1372
1373 ret = 0;
1374 goto unlock;
1375 default:
1376 ret = -ENOTTY;
1377 }
1378
1379unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001380 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001381 return ret;
1382}
1383EXPORT_SYMBOL(drm_fb_helper_ioctl);
1384
1385/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001386 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001387 * @var: screeninfo to check
1388 * @info: fbdev registered by the helper
1389 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001390int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1391 struct fb_info *info)
1392{
1393 struct drm_fb_helper *fb_helper = info->par;
1394 struct drm_framebuffer *fb = fb_helper->fb;
1395 int depth;
1396
Jason Wesself90ebd92010-08-05 09:22:32 -05001397 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001398 return -EINVAL;
1399
Stefan Agner865afb12016-10-11 16:15:04 -07001400 /*
1401 * Changes struct fb_var_screeninfo are currently not pushed back
1402 * to KMS, hence fail if different settings are requested.
1403 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001404 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001405 var->xres > fb->width || var->yres > fb->height ||
1406 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1407 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001408 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1409 var->xres, var->yres, var->bits_per_pixel,
1410 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001411 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001412 return -EINVAL;
1413 }
1414
1415 switch (var->bits_per_pixel) {
1416 case 16:
1417 depth = (var->green.length == 6) ? 16 : 15;
1418 break;
1419 case 32:
1420 depth = (var->transp.length > 0) ? 32 : 24;
1421 break;
1422 default:
1423 depth = var->bits_per_pixel;
1424 break;
1425 }
1426
1427 switch (depth) {
1428 case 8:
1429 var->red.offset = 0;
1430 var->green.offset = 0;
1431 var->blue.offset = 0;
1432 var->red.length = 8;
1433 var->green.length = 8;
1434 var->blue.length = 8;
1435 var->transp.length = 0;
1436 var->transp.offset = 0;
1437 break;
1438 case 15:
1439 var->red.offset = 10;
1440 var->green.offset = 5;
1441 var->blue.offset = 0;
1442 var->red.length = 5;
1443 var->green.length = 5;
1444 var->blue.length = 5;
1445 var->transp.length = 1;
1446 var->transp.offset = 15;
1447 break;
1448 case 16:
1449 var->red.offset = 11;
1450 var->green.offset = 5;
1451 var->blue.offset = 0;
1452 var->red.length = 5;
1453 var->green.length = 6;
1454 var->blue.length = 5;
1455 var->transp.length = 0;
1456 var->transp.offset = 0;
1457 break;
1458 case 24:
1459 var->red.offset = 16;
1460 var->green.offset = 8;
1461 var->blue.offset = 0;
1462 var->red.length = 8;
1463 var->green.length = 8;
1464 var->blue.length = 8;
1465 var->transp.length = 0;
1466 var->transp.offset = 0;
1467 break;
1468 case 32:
1469 var->red.offset = 16;
1470 var->green.offset = 8;
1471 var->blue.offset = 0;
1472 var->red.length = 8;
1473 var->green.length = 8;
1474 var->blue.length = 8;
1475 var->transp.length = 8;
1476 var->transp.offset = 24;
1477 break;
1478 default:
1479 return -EINVAL;
1480 }
1481 return 0;
1482}
1483EXPORT_SYMBOL(drm_fb_helper_check_var);
1484
Daniel Vetter207fd322013-01-20 22:13:14 +01001485/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001486 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001487 * @info: fbdev registered by the helper
1488 *
1489 * This will let fbcon do the mode init and is called at initialization time by
1490 * the fbdev core when registering the driver, and later on through the hotplug
1491 * callback.
1492 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001493int drm_fb_helper_set_par(struct fb_info *info)
1494{
1495 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001496 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001497
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001498 if (oops_in_progress)
1499 return -EBUSY;
1500
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001501 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001502 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001503 return -EINVAL;
1504 }
1505
Rob Clark5ea1f752014-05-30 12:29:48 -04001506 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001507
Dave Airlie785b93e2009-08-28 15:46:53 +10001508 return 0;
1509}
1510EXPORT_SYMBOL(drm_fb_helper_set_par);
1511
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001512static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
Rob Clark1edf0262015-08-25 15:35:59 -04001513{
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001514 int i;
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001515
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001516 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001517 struct drm_mode_set *mode_set;
1518
1519 mode_set = &fb_helper->crtc_info[i].mode_set;
1520
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001521 mode_set->x = x;
1522 mode_set->y = y;
Rob Clark1edf0262015-08-25 15:35:59 -04001523 }
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001524}
Rob Clark1edf0262015-08-25 15:35:59 -04001525
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001526static int pan_display_atomic(struct fb_var_screeninfo *var,
1527 struct fb_info *info)
1528{
1529 struct drm_fb_helper *fb_helper = info->par;
1530 int ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001531
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001532 pan_set(fb_helper, var->xoffset, var->yoffset);
Rob Clark1edf0262015-08-25 15:35:59 -04001533
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001534 ret = restore_fbdev_mode_atomic(fb_helper, true);
1535 if (!ret) {
1536 info->var.xoffset = var->xoffset;
1537 info->var.yoffset = var->yoffset;
1538 } else
1539 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001540
Rob Clark1edf0262015-08-25 15:35:59 -04001541 return ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001542}
1543
Daniel Vetter71286452017-04-03 10:33:04 +02001544static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001545 struct fb_info *info)
1546{
1547 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001548 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001549 int ret = 0;
1550 int i;
1551
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001552 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001553 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001554 modeset = &fb_helper->crtc_info[i].mode_set;
1555
1556 modeset->x = var->xoffset;
1557 modeset->y = var->yoffset;
1558
1559 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001560 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001561 if (!ret) {
1562 info->var.xoffset = var->xoffset;
1563 info->var.yoffset = var->yoffset;
1564 }
1565 }
1566 }
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001567 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001568
1569 return ret;
1570}
1571
1572/**
1573 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1574 * @var: updated screen information
1575 * @info: fbdev registered by the helper
1576 */
1577int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1578 struct fb_info *info)
1579{
1580 struct drm_fb_helper *fb_helper = info->par;
1581 struct drm_device *dev = fb_helper->dev;
1582 int ret;
1583
1584 if (oops_in_progress)
1585 return -EBUSY;
1586
Thierry Redinge9827d82017-07-04 17:18:23 +02001587 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001588 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001589 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001590 return -EBUSY;
1591 }
1592
1593 if (drm_drv_uses_atomic_modeset(dev))
1594 ret = pan_display_atomic(var, info);
1595 else
1596 ret = pan_display_legacy(var, info);
Thierry Redinge9827d82017-07-04 17:18:23 +02001597 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001598
Dave Airlie785b93e2009-08-28 15:46:53 +10001599 return ret;
1600}
1601EXPORT_SYMBOL(drm_fb_helper_pan_display);
1602
Daniel Vetter8acf6582013-01-21 23:38:37 +01001603/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001604 * Allocates the backing storage and sets up the fbdev info structure through
1605 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1606 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001607 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001608static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1609 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001610{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001611 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001612 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001613 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001614 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001615 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001616
1617 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1618 sizes.surface_depth = 24;
1619 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001620 sizes.fb_width = (u32)-1;
1621 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001622
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001623 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001624 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001625 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001626
Dave Airlie785b93e2009-08-28 15:46:53 +10001627 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001628 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001629 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001630 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001631
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001632 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001633
1634 if (cmdline_mode->bpp_specified) {
1635 switch (cmdline_mode->bpp) {
1636 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001637 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001638 break;
1639 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001640 sizes.surface_depth = 15;
1641 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001642 break;
1643 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001644 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001645 break;
1646 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001647 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001648 break;
1649 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001650 sizes.surface_depth = 24;
1651 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001652 break;
1653 }
1654 break;
1655 }
1656 }
1657
Dave Airlie8be48d92010-03-30 05:34:14 +00001658 crtc_count = 0;
1659 for (i = 0; i < fb_helper->crtc_count; i++) {
1660 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001661 struct drm_mode_set *mode_set;
1662 int x, y, j;
1663 /* in case of tile group, are we the last tile vert or horiz?
1664 * If no tile group you are always the last one both vertically
1665 * and horizontally
1666 */
1667 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001668
Dave Airlie8be48d92010-03-30 05:34:14 +00001669 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001670 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001671
1672 if (!desired_mode)
1673 continue;
1674
1675 crtc_count++;
1676
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001677 x = fb_helper->crtc_info[i].x;
1678 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001679
1680 if (gamma_size == 0)
1681 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1682
1683 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1684 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001685
1686 for (j = 0; j < mode_set->num_connectors; j++) {
1687 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001688
Rob Clark0e3704c2015-03-11 10:23:14 -04001689 if (connector->has_tile) {
1690 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1691 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1692 /* cloning to multiple tiles is just crazy-talk, so: */
1693 break;
1694 }
1695 }
1696
1697 if (lasth)
1698 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1699 if (lastv)
1700 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001701 }
1702
Dave Airlie38651672010-03-30 05:34:13 +00001703 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001704 /*
1705 * hmm everyone went away - assume VGA cable just fell out
1706 * and will come back later.
1707 */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001708 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001709 sizes.fb_width = sizes.surface_width = 1024;
1710 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001711 }
1712
Xinliang Liu5f152572017-02-15 17:19:08 +01001713 /* Handle our overallocation */
1714 sizes.surface_height *= drm_fbdev_overalloc;
1715 sizes.surface_height /= 100;
1716
Dave Airlie38651672010-03-30 05:34:13 +00001717 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001718 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1719 if (ret < 0)
1720 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001721
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001722 /*
1723 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1724 * events, but at init time drm_setup_crtcs needs to be called before
1725 * the fb is allocated (since we need to figure out the desired size of
1726 * the fb before we can allocate it ...). Hence we need to fix things up
1727 * here again.
1728 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001729 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001730 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1731 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1732
Dave Airlie785b93e2009-08-28 15:46:53 +10001733 return 0;
1734}
Dave Airlie785b93e2009-08-28 15:46:53 +10001735
Daniel Vetter207fd322013-01-20 22:13:14 +01001736/**
1737 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1738 * @info: fbdev registered by the helper
1739 * @pitch: desired pitch
1740 * @depth: desired depth
1741 *
1742 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1743 * fbdev emulations. Drivers which support acceleration methods which impose
1744 * additional constraints need to set up their own limits.
1745 *
1746 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001747 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001748 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001749void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1750 uint32_t depth)
1751{
1752 info->fix.type = FB_TYPE_PACKED_PIXELS;
1753 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1754 FB_VISUAL_TRUECOLOR;
1755 info->fix.mmio_start = 0;
1756 info->fix.mmio_len = 0;
1757 info->fix.type_aux = 0;
1758 info->fix.xpanstep = 1; /* doing it in hw */
1759 info->fix.ypanstep = 1; /* doing it in hw */
1760 info->fix.ywrapstep = 0;
1761 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001762
1763 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001764}
1765EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1766
Daniel Vetter207fd322013-01-20 22:13:14 +01001767/**
1768 * drm_fb_helper_fill_var - initalizes variable fbdev information
1769 * @info: fbdev instance to set up
1770 * @fb_helper: fb helper instance to use as template
1771 * @fb_width: desired fb width
1772 * @fb_height: desired fb height
1773 *
1774 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001775 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001776 *
1777 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001778 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1779 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001780 */
Dave Airlie38651672010-03-30 05:34:13 +00001781void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001782 uint32_t fb_width, uint32_t fb_height)
1783{
Dave Airlie38651672010-03-30 05:34:13 +00001784 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001785
Dave Airlie38651672010-03-30 05:34:13 +00001786 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001787 info->var.xres_virtual = fb->width;
1788 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001789 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001790 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001791 info->var.xoffset = 0;
1792 info->var.yoffset = 0;
1793 info->var.activate = FB_ACTIVATE_NOW;
1794 info->var.height = -1;
1795 info->var.width = -1;
1796
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001797 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001798 case 8:
1799 info->var.red.offset = 0;
1800 info->var.green.offset = 0;
1801 info->var.blue.offset = 0;
1802 info->var.red.length = 8; /* 8bit DAC */
1803 info->var.green.length = 8;
1804 info->var.blue.length = 8;
1805 info->var.transp.offset = 0;
1806 info->var.transp.length = 0;
1807 break;
1808 case 15:
1809 info->var.red.offset = 10;
1810 info->var.green.offset = 5;
1811 info->var.blue.offset = 0;
1812 info->var.red.length = 5;
1813 info->var.green.length = 5;
1814 info->var.blue.length = 5;
1815 info->var.transp.offset = 15;
1816 info->var.transp.length = 1;
1817 break;
1818 case 16:
1819 info->var.red.offset = 11;
1820 info->var.green.offset = 5;
1821 info->var.blue.offset = 0;
1822 info->var.red.length = 5;
1823 info->var.green.length = 6;
1824 info->var.blue.length = 5;
1825 info->var.transp.offset = 0;
1826 break;
1827 case 24:
1828 info->var.red.offset = 16;
1829 info->var.green.offset = 8;
1830 info->var.blue.offset = 0;
1831 info->var.red.length = 8;
1832 info->var.green.length = 8;
1833 info->var.blue.length = 8;
1834 info->var.transp.offset = 0;
1835 info->var.transp.length = 0;
1836 break;
1837 case 32:
1838 info->var.red.offset = 16;
1839 info->var.green.offset = 8;
1840 info->var.blue.offset = 0;
1841 info->var.red.length = 8;
1842 info->var.green.length = 8;
1843 info->var.blue.length = 8;
1844 info->var.transp.offset = 24;
1845 info->var.transp.length = 8;
1846 break;
1847 default:
1848 break;
1849 }
1850
1851 info->var.xres = fb_width;
1852 info->var.yres = fb_height;
1853}
1854EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001855
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001856static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
Daniel Vettere13a0582017-07-05 06:56:29 +02001857 uint32_t maxX,
1858 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001859{
1860 struct drm_connector *connector;
Daniel Vettere13a0582017-07-05 06:56:29 +02001861 int i, count = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001862
Chris Wilson966a6a12016-11-29 12:02:15 +00001863 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001864 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001865 count += connector->funcs->fill_modes(connector, maxX, maxY);
1866 }
1867
1868 return count;
1869}
1870
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001871struct 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 +00001872{
1873 struct drm_display_mode *mode;
1874
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001875 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001876 if (mode->hdisplay > width ||
1877 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001878 continue;
1879 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1880 return mode;
1881 }
1882 return NULL;
1883}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001884EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001885
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001886static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001887{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001888 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001889}
1890
Vincent Abrioua09759e2017-01-06 17:44:43 +01001891struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00001892{
Chris Wilson1794d252011-04-17 07:43:32 +01001893 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001894 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001895 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001896
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001897 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001898 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001899 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001900
1901 /* attempt to find a matching mode in the list of modes
1902 * we have gotten so far, if not add a CVT mode that conforms
1903 */
1904 if (cmdline_mode->rb || cmdline_mode->margins)
1905 goto create_mode;
1906
Takashi Iwaic683f422014-03-19 14:53:13 +01001907 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001908again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001909 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001910 /* check width/height */
1911 if (mode->hdisplay != cmdline_mode->xres ||
1912 mode->vdisplay != cmdline_mode->yres)
1913 continue;
1914
1915 if (cmdline_mode->refresh_specified) {
1916 if (mode->vrefresh != cmdline_mode->refresh)
1917 continue;
1918 }
1919
1920 if (cmdline_mode->interlace) {
1921 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1922 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001923 } else if (prefer_non_interlace) {
1924 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1925 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001926 }
1927 return mode;
1928 }
1929
Takashi Iwaic683f422014-03-19 14:53:13 +01001930 if (prefer_non_interlace) {
1931 prefer_non_interlace = false;
1932 goto again;
1933 }
1934
Dave Airlie38651672010-03-30 05:34:13 +00001935create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001936 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1937 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001938 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001939 return mode;
1940}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001941EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001942
1943static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1944{
1945 bool enable;
1946
Sachin Kamat96081cd2012-11-15 03:43:30 +00001947 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001948 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001949 else
Dave Airlie38651672010-03-30 05:34:13 +00001950 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001951
Dave Airlie38651672010-03-30 05:34:13 +00001952 return enable;
1953}
1954
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001955static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1956 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001957{
1958 bool any_enabled = false;
1959 struct drm_connector *connector;
1960 int i = 0;
1961
Chris Wilson966a6a12016-11-29 12:02:15 +00001962 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001963 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001964 enabled[i] = drm_connector_enabled(connector, true);
1965 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1966 enabled[i] ? "yes" : "no");
1967 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001968 }
1969
1970 if (any_enabled)
1971 return;
1972
Chris Wilson966a6a12016-11-29 12:02:15 +00001973 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001974 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001975 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001976 }
1977}
1978
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001979static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1980 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001981 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001982 bool *enabled, int width, int height)
1983{
1984 int count, i, j;
1985 bool can_clone = false;
1986 struct drm_fb_helper_connector *fb_helper_conn;
1987 struct drm_display_mode *dmt_mode, *mode;
1988
1989 /* only contemplate cloning in the single crtc case */
1990 if (fb_helper->crtc_count > 1)
1991 return false;
1992
1993 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00001994 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001995 if (enabled[i])
1996 count++;
1997 }
1998
1999 /* only contemplate cloning if more than one connector is enabled */
2000 if (count <= 1)
2001 return false;
2002
2003 /* check the command line or if nothing common pick 1024x768 */
2004 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002005 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002006 if (!enabled[i])
2007 continue;
2008 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002009 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002010 if (!modes[i]) {
2011 can_clone = false;
2012 break;
2013 }
2014 for (j = 0; j < i; j++) {
2015 if (!enabled[j])
2016 continue;
2017 if (!drm_mode_equal(modes[j], modes[i]))
2018 can_clone = false;
2019 }
2020 }
2021
2022 if (can_clone) {
2023 DRM_DEBUG_KMS("can clone using command line\n");
2024 return true;
2025 }
2026
2027 /* try and find a 1024x768 mode on each connector */
2028 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002029 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002030
Chris Wilson966a6a12016-11-29 12:02:15 +00002031 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002032 if (!enabled[i])
2033 continue;
2034
2035 fb_helper_conn = fb_helper->connector_info[i];
2036 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2037 if (drm_mode_equal(mode, dmt_mode))
2038 modes[i] = mode;
2039 }
2040 if (!modes[i])
2041 can_clone = false;
2042 }
2043
2044 if (can_clone) {
2045 DRM_DEBUG_KMS("can clone using 1024x768\n");
2046 return true;
2047 }
2048 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2049 return false;
2050}
2051
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002052static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2053 struct drm_display_mode **modes,
2054 struct drm_fb_offset *offsets,
2055 int idx,
2056 int h_idx, int v_idx)
2057{
2058 struct drm_fb_helper_connector *fb_helper_conn;
2059 int i;
2060 int hoffset = 0, voffset = 0;
2061
Chris Wilson966a6a12016-11-29 12:02:15 +00002062 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002063 fb_helper_conn = fb_helper->connector_info[i];
2064 if (!fb_helper_conn->connector->has_tile)
2065 continue;
2066
2067 if (!modes[i] && (h_idx || v_idx)) {
2068 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2069 fb_helper_conn->connector->base.id);
2070 continue;
2071 }
2072 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2073 hoffset += modes[i]->hdisplay;
2074
2075 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2076 voffset += modes[i]->vdisplay;
2077 }
2078 offsets[idx].x = hoffset;
2079 offsets[idx].y = voffset;
2080 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2081 return 0;
2082}
2083
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002084static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002085 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002086 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002087 bool *enabled, int width, int height)
2088{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002089 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002090 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2091 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002092 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002093 int i;
2094
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002095retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002096 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002097 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002098
Chris Wilsonc96521e2016-11-27 17:09:10 +00002099 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002100 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002101
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002102 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002103 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002104 continue;
2105 }
2106
2107 /* first pass over all the untiled connectors */
2108 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2109 continue;
2110
2111 if (tile_pass == 1) {
2112 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2113 fb_helper_conn->connector->tile_v_loc != 0)
2114 continue;
2115
2116 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002117 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002118 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2119 /* if this tile_pass doesn't cover any of the tiles - keep going */
2120 continue;
2121
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002122 /*
2123 * find the tile offsets for this pass - need to find
2124 * all tiles left and above
2125 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002126 drm_get_tile_offsets(fb_helper, modes, offsets,
2127 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2128 }
Dave Airlie38651672010-03-30 05:34:13 +00002129 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002130 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002131
2132 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002133 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002134 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002135 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2136 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 +00002137 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002138 }
2139 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002140 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2141 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002142 break;
2143 }
2144 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2145 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002146 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002147 }
2148
2149 if ((conn_configured & mask) != mask) {
2150 tile_pass++;
2151 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002152 }
2153 return true;
2154}
2155
Dave Airlie8be48d92010-03-30 05:34:14 +00002156static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2157 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002158 struct drm_display_mode **modes,
2159 int n, int width, int height)
2160{
2161 int c, o;
2162 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002163 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002164 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002165 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002166 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002167 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002168
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002169 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002170 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002171
2172 fb_helper_conn = fb_helper->connector_info[n];
2173 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002174
2175 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002176 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002177 if (modes[n] == NULL)
2178 return best_score;
2179
Lyude255f0e72016-05-12 10:56:59 -04002180 crtcs = kzalloc(fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002181 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002182 if (!crtcs)
2183 return best_score;
2184
2185 my_score = 1;
2186 if (connector->status == connector_status_connected)
2187 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002188 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002189 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002190 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002191 my_score++;
2192
2193 connector_funcs = connector->helper_private;
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002194
2195 /*
2196 * If the DRM device implements atomic hooks and ->best_encoder() is
2197 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2198 * helper.
2199 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002200 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002201 !connector_funcs->best_encoder)
2202 encoder = drm_atomic_helper_best_encoder(connector);
2203 else
2204 encoder = connector_funcs->best_encoder(connector);
2205
Dave Airlie38651672010-03-30 05:34:13 +00002206 if (!encoder)
2207 goto out;
2208
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002209 /*
2210 * select a crtc for this connector and then attempt to configure
2211 * remaining connectors
2212 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002213 for (c = 0; c < fb_helper->crtc_count; c++) {
2214 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002215
Sachin Kamat96081cd2012-11-15 03:43:30 +00002216 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002217 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002218
2219 for (o = 0; o < n; o++)
2220 if (best_crtcs[o] == crtc)
2221 break;
2222
2223 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002224 /* ignore cloning unless only a single crtc */
2225 if (fb_helper->crtc_count > 1)
2226 continue;
2227
2228 if (!drm_mode_equal(modes[o], modes[n]))
2229 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002230 }
2231
2232 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002233 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2234 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002235 width, height);
2236 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002237 best_score = score;
2238 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002239 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002240 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002241 }
Dave Airlie38651672010-03-30 05:34:13 +00002242 }
2243out:
2244 kfree(crtcs);
2245 return best_score;
2246}
2247
Chris Wilson64e94402016-11-29 12:02:16 +00002248static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2249 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002250{
Dave Airlie8be48d92010-03-30 05:34:14 +00002251 struct drm_device *dev = fb_helper->dev;
2252 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002253 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002254 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002255 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002256 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002257
2258 DRM_DEBUG_KMS("\n");
Chris Wilson966a6a12016-11-29 12:02:15 +00002259 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002260 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002261
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002262 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002263 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002264 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002265 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002266 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002267 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002268 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002269 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002270 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002271 DRM_ERROR("Memory allocation failed\n");
2272 goto out;
2273 }
2274
Daniel Vettere13a0582017-07-05 06:56:29 +02002275 mutex_lock(&fb_helper->dev->mode_config.mutex);
2276 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2277 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002278 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002279
Jesse Barnes11e17a02013-02-19 13:31:39 -08002280 if (!(fb_helper->funcs->initial_config &&
2281 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002282 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002283 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002284 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2285 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2286 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002287
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002288 if (!drm_target_cloned(fb_helper, modes, offsets,
2289 enabled, width, height) &&
2290 !drm_target_preferred(fb_helper, modes, offsets,
2291 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002292 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002293
2294 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2295 width, height);
2296
2297 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002298 }
Daniel Vettere13a0582017-07-05 06:56:29 +02002299 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002300
Dave Airlie8be48d92010-03-30 05:34:14 +00002301 /* need to set the modesets up here for use later */
2302 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002303 for (i = 0; i < fb_helper->crtc_count; i++)
2304 drm_fb_helper_modeset_release(fb_helper,
2305 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002306
Chris Wilson966a6a12016-11-29 12:02:15 +00002307 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002308 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002309 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002310 struct drm_fb_offset *offset = &offsets[i];
Ville Syrjäläa2889602016-10-26 17:41:18 +03002311 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00002312
Dave Airlie8be48d92010-03-30 05:34:14 +00002313 if (mode && fb_crtc) {
Ville Syrjäläa2889602016-10-26 17:41:18 +03002314 struct drm_connector *connector =
2315 fb_helper->connector_info[i]->connector;
2316
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002317 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2318 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002319
Dave Airlie8be48d92010-03-30 05:34:14 +00002320 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002321 fb_crtc->x = offset->x;
2322 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002323 modeset->mode = drm_mode_duplicate(dev,
2324 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002325 drm_connector_get(connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002326 modeset->connectors[modeset->num_connectors++] = connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002327 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002328 modeset->x = offset->x;
2329 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002330 }
Dave Airlie38651672010-03-30 05:34:13 +00002331 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002332out:
Dave Airlie38651672010-03-30 05:34:13 +00002333 kfree(crtcs);
2334 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002335 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002336 kfree(enabled);
2337}
2338
2339/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002340 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002341 * @fb_helper: fb_helper device struct
2342 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002343 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002344 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002345 * At the moment, this is a cloned configuration across all heads with
2346 * a new framebuffer object as the backing store.
2347 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002348 * Note that this also registers the fbdev and so allows userspace to call into
2349 * the driver through the fbdev interfaces.
2350 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002351 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2352 * to let the driver allocate and initialize the fbdev info structure and the
2353 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002354 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2355 * values for the fbdev info structure.
2356 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002357 * HANG DEBUGGING:
2358 *
2359 * When you have fbcon support built-in or already loaded, this function will do
2360 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2361 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2362 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2363 * to consoles, not even serial console. This means when your driver crashes,
2364 * you will see absolutely nothing else but a system stuck in this function,
2365 * with no further output. Any kind of printk() you place within your own driver
2366 * or in the drm core modeset code will also never show up.
2367 *
2368 * Standard debug practice is to run the fbcon setup without taking the
2369 * console_lock as a hack, to be able to see backtraces and crashes on the
2370 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2371 * cmdline option.
2372 *
2373 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002374 * first modeset from userspace will crash in the same way, and is even easier
2375 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002376 * kernel cmdline option.
2377 *
Dave Airlie38651672010-03-30 05:34:13 +00002378 * RETURNS:
2379 * Zero if everything went ok, nonzero otherwise.
2380 */
Thierry Reding01934c22014-12-19 11:21:32 +01002381int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002382{
Dave Airlie8be48d92010-03-30 05:34:14 +00002383 struct drm_device *dev = fb_helper->dev;
Chris Wilson966a6a12016-11-29 12:02:15 +00002384 struct fb_info *info;
Chris Wilson966a6a12016-11-29 12:02:15 +00002385 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002386
Daniel Vetterf64c5572015-08-25 15:45:13 +02002387 if (!drm_fbdev_emulation)
2388 return 0;
2389
Thierry Redinge9827d82017-07-04 17:18:23 +02002390 mutex_lock(&fb_helper->lock);
Chris Wilson64e94402016-11-29 12:02:16 +00002391 drm_setup_crtcs(fb_helper,
2392 dev->mode_config.max_width,
2393 dev->mode_config.max_height);
Chris Wilson966a6a12016-11-29 12:02:15 +00002394 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Thierry Redinge9827d82017-07-04 17:18:23 +02002395 mutex_unlock(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002396 if (ret)
2397 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002398
Chris Wilson966a6a12016-11-29 12:02:15 +00002399 info = fb_helper->fbdev;
2400 info->var.pixclock = 0;
2401 ret = register_framebuffer(info);
2402 if (ret < 0)
2403 return ret;
2404
2405 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2406 info->node, info->fix.id);
2407
Chris Wilsona53ca632016-11-29 12:02:17 +00002408 mutex_lock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002409 if (list_empty(&kernel_fb_helper_list))
2410 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2411
2412 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +00002413 mutex_unlock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002414
2415 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002416}
Dave Airlie8be48d92010-03-30 05:34:14 +00002417EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002418
Chris Wilson73943712011-04-22 11:03:57 +01002419/**
2420 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002421 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01002422 * @fb_helper: the drm_fb_helper
2423 *
Chris Wilson73943712011-04-22 11:03:57 +01002424 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002425 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002426 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002427 * Called at runtime, takes the mode config locks to be able to check/change the
2428 * modeset configuration. Must be run from process context (which usually means
2429 * either the output polling work or a work item launched from the driver's
2430 * hotplug interrupt).
2431 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002432 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002433 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002434 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2435 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002436 *
Chris Wilson73943712011-04-22 11:03:57 +01002437 * RETURNS:
2438 * 0 on success and a non-zero error code otherwise.
2439 */
2440int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002441{
Thierry Redinge9827d82017-07-04 17:18:23 +02002442 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002443
Daniel Vetterf64c5572015-08-25 15:45:13 +02002444 if (!drm_fbdev_emulation)
2445 return 0;
2446
Thierry Redinge9827d82017-07-04 17:18:23 +02002447 mutex_lock(&fb_helper->lock);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002448 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002449 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002450 mutex_unlock(&fb_helper->lock);
2451 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002452 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002453
Dave Airlie38651672010-03-30 05:34:13 +00002454 DRM_DEBUG_KMS("\n");
2455
Chris Wilson64e94402016-11-29 12:02:16 +00002456 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
Thierry Redinge9827d82017-07-04 17:18:23 +02002457 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002458
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002459 drm_fb_helper_set_par(fb_helper->fbdev);
2460
2461 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002462}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002463EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002464
David Rientjes6a108a12011-01-20 14:44:16 -08002465/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002466 * but the module doesn't depend on any fb console symbols. At least
2467 * attempt to load fbcon to avoid leaving the system without a usable console.
2468 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002469int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002470{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002471#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07002472 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06002473 struct module *fbcon;
2474
2475 mutex_lock(&module_mutex);
2476 fbcon = find_module(name);
2477 mutex_unlock(&module_mutex);
2478
2479 if (!fbcon)
2480 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002481#endif
David Fries3ce05162010-12-12 12:39:22 -06002482 return 0;
2483}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002484EXPORT_SYMBOL(drm_fb_helper_modinit);