blob: 954cdd48de92efb3f88b3236d612b695ade3ce8a [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
Noralf Trønnesc7779902017-10-30 16:39:37 +0100153 if (!fb_helper)
154 return 0;
155
Thierry Redinge9827d82017-07-04 17:18:23 +0200156 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200157 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200158 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200159
160 return err;
161}
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200162EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
163
Daniel Vetter207fd322013-01-20 22:13:14 +0100164/**
165 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
166 * emulation helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100167 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
Daniel Vetter207fd322013-01-20 22:13:14 +0100168 *
169 * This functions adds all the available connectors for use with the given
170 * fb_helper. This is a separate step to allow drivers to freely assign
171 * connectors to the fbdev, e.g. if some are reserved for special purposes or
172 * not adequate to be used for the fbcon.
173 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200174 * This function is protected against concurrent connector hotadds/removals
175 * using drm_fb_helper_add_one_connector() and
176 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100177 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000178int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000179{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000180 struct drm_device *dev = fb_helper->dev;
181 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100182 struct drm_connector_list_iter conn_iter;
183 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000184
Noralf Trønnesc7779902017-10-30 16:39:37 +0100185 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200186 return 0;
187
Thierry Redinge9827d82017-07-04 17:18:23 +0200188 mutex_lock(&fb_helper->lock);
Thierry Redingb982dab2017-02-28 15:46:43 +0100189 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100190 drm_for_each_connector_iter(connector, &conn_iter) {
Thierry Redingaf2405a2017-07-04 17:18:21 +0200191 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100192 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000193 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000194 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100195 goto out;
196
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000197fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000198 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300199 struct drm_fb_helper_connector *fb_helper_connector =
200 fb_helper->connector_info[i];
201
Thierry Redingad093602017-02-28 15:46:39 +0100202 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300203
204 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000205 fb_helper->connector_info[i] = NULL;
206 }
207 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100208out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100209 drm_connector_list_iter_end(&conn_iter);
Thierry Redinge9827d82017-07-04 17:18:23 +0200210 mutex_unlock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200211
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100212 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000213}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000214EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000215
Thierry Redingaf2405a2017-07-04 17:18:21 +0200216static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
217 struct drm_connector *connector)
Dave Airlie65c2a892014-06-05 14:01:30 +1000218{
219 struct drm_fb_helper_connector *fb_helper_connector;
220 int i, j;
221
Daniel Vetterf64c5572015-08-25 15:45:13 +0200222 if (!drm_fbdev_emulation)
223 return 0;
224
Thierry Redinge9827d82017-07-04 17:18:23 +0200225 lockdep_assert_held(&fb_helper->lock);
Dave Airlie65c2a892014-06-05 14:01:30 +1000226
Thierry Redinge9827d82017-07-04 17:18:23 +0200227 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie65c2a892014-06-05 14:01:30 +1000228 if (fb_helper->connector_info[i]->connector == connector)
229 break;
230 }
231
232 if (i == fb_helper->connector_count)
233 return -EINVAL;
234 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100235 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000236
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200237 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000238 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200239
Dave Airlie65c2a892014-06-05 14:01:30 +1000240 fb_helper->connector_count--;
241 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500242
Dave Airlie65c2a892014-06-05 14:01:30 +1000243 return 0;
244}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200245
246int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
247 struct drm_connector *connector)
248{
249 int err;
250
Noralf Trønnesc7779902017-10-30 16:39:37 +0100251 if (!fb_helper)
252 return 0;
253
Thierry Redinge9827d82017-07-04 17:18:23 +0200254 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200255 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200256 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200257
258 return err;
259}
Dave Airlie65c2a892014-06-05 14:01:30 +1000260EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
261
Jason Wessel99231022010-10-13 14:09:43 -0500262static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
263{
264 uint16_t *r_base, *g_base, *b_base;
265
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200266 if (crtc->funcs->gamma_set == NULL)
267 return;
268
Jason Wessel99231022010-10-13 14:09:43 -0500269 r_base = crtc->gamma_store;
270 g_base = r_base + crtc->gamma_size;
271 b_base = g_base + crtc->gamma_size;
272
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200273 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
274 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500275}
276
Daniel Vetter207fd322013-01-20 22:13:14 +0100277/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100278 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100279 * @info: fbdev registered by the helper
280 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500281int drm_fb_helper_debug_enter(struct fb_info *info)
282{
283 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200284 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500285 int i;
286
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500287 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
288 for (i = 0; i < helper->crtc_count; i++) {
289 struct drm_mode_set *mode_set =
290 &helper->crtc_info[i].mode_set;
291
292 if (!mode_set->crtc->enabled)
293 continue;
294
295 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100296 if (funcs->mode_set_base_atomic == NULL)
297 continue;
298
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200299 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
300 continue;
301
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500302 funcs->mode_set_base_atomic(mode_set->crtc,
303 mode_set->fb,
304 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500305 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500306 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500307 }
308 }
309
310 return 0;
311}
312EXPORT_SYMBOL(drm_fb_helper_debug_enter);
313
Daniel Vetter207fd322013-01-20 22:13:14 +0100314/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100315 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100316 * @info: fbdev registered by the helper
317 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500318int drm_fb_helper_debug_leave(struct fb_info *info)
319{
320 struct drm_fb_helper *helper = info->par;
321 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200322 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500323 struct drm_framebuffer *fb;
324 int i;
325
326 for (i = 0; i < helper->crtc_count; i++) {
327 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200328
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500329 crtc = mode_set->crtc;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200330 if (drm_drv_uses_atomic_modeset(crtc->dev))
331 continue;
332
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500333 funcs = crtc->helper_private;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200334 fb = crtc->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500335
336 if (!crtc->enabled)
337 continue;
338
339 if (!fb) {
340 DRM_ERROR("no fb to restore??\n");
341 continue;
342 }
343
Stefan Christ1b99b722016-11-14 00:03:11 +0100344 if (funcs->mode_set_base_atomic == NULL)
345 continue;
346
Jason Wessel99231022010-10-13 14:09:43 -0500347 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500348 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500349 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500350 }
351
352 return 0;
353}
354EXPORT_SYMBOL(drm_fb_helper_debug_leave);
355
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200356static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
Rob Clarkbbb1e522015-08-25 15:35:58 -0400357{
358 struct drm_device *dev = fb_helper->dev;
359 struct drm_plane *plane;
360 struct drm_atomic_state *state;
361 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200362 unsigned int plane_mask;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200363 struct drm_modeset_acquire_ctx ctx;
364
365 drm_modeset_acquire_init(&ctx, 0);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400366
367 state = drm_atomic_state_alloc(dev);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200368 if (!state) {
369 ret = -ENOMEM;
370 goto out_ctx;
371 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400372
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200373 state->acquire_ctx = &ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400374retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100375 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400376 drm_for_each_plane(plane, dev) {
377 struct drm_plane_state *plane_state;
378
379 plane_state = drm_atomic_get_plane_state(state, plane);
380 if (IS_ERR(plane_state)) {
381 ret = PTR_ERR(plane_state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200382 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400383 }
384
Robert Fossc2c446a2017-05-19 16:50:17 -0400385 plane_state->rotation = DRM_MODE_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400386
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100387 plane->old_fb = plane->fb;
388 plane_mask |= 1 << drm_plane_index(plane);
389
Rob Clarkbbb1e522015-08-25 15:35:58 -0400390 /* disable non-primary: */
391 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
392 continue;
393
394 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
395 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200396 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400397 }
398
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200399 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400400 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
401
402 ret = __drm_atomic_helper_set_config(mode_set, state);
403 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200404 goto out_state;
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200405
406 /*
407 * __drm_atomic_helper_set_config() sets active when a
408 * mode is set, unconditionally clear it if we force DPMS off
409 */
410 if (!active) {
411 struct drm_crtc *crtc = mode_set->crtc;
412 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
413
414 crtc_state->active = false;
415 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400416 }
417
418 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400419
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200420out_state:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100421 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700422
Rob Clarkbbb1e522015-08-25 15:35:58 -0400423 if (ret == -EDEADLK)
424 goto backoff;
425
Chris Wilson08536952016-10-14 13:18:18 +0100426 drm_atomic_state_put(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200427out_ctx:
428 drm_modeset_drop_locks(&ctx);
429 drm_modeset_acquire_fini(&ctx);
430
Rob Clarkbbb1e522015-08-25 15:35:58 -0400431 return ret;
432
433backoff:
434 drm_atomic_state_clear(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200435 drm_modeset_backoff(&ctx);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400436
437 goto retry;
438}
439
Daniel Vetter71286452017-04-03 10:33:04 +0200440static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100441{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300442 struct drm_device *dev = fb_helper->dev;
443 struct drm_plane *plane;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200444 int i, ret = 0;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100445
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200446 drm_modeset_lock_all(fb_helper->dev);
Daniel Vetter6295d602015-07-09 23:44:25 +0200447 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700448 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
449 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100450
Ville Syrjälä6686df82016-10-21 22:22:45 +0300451 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300452 drm_mode_plane_set_obj_prop(plane,
453 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400454 DRM_MODE_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530455 }
456
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100457 for (i = 0; i < fb_helper->crtc_count; i++) {
458 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300459 struct drm_crtc *crtc = mode_set->crtc;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300460
Alex Deucher03f9abb2015-09-30 14:47:37 -0400461 if (crtc->funcs->cursor_set2) {
462 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
463 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200464 goto out;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400465 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300466 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
467 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200468 goto out;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300469 }
470
Daniel Vetter2d13b672012-12-11 13:47:23 +0100471 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100472 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200473 goto out;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100474 }
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200475out:
476 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200477
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200478 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100479}
Rob Clark5ea1f752014-05-30 12:29:48 -0400480
Daniel Vetter71286452017-04-03 10:33:04 +0200481static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
482{
483 struct drm_device *dev = fb_helper->dev;
484
Daniel Vetter71286452017-04-03 10:33:04 +0200485 if (drm_drv_uses_atomic_modeset(dev))
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200486 return restore_fbdev_mode_atomic(fb_helper, true);
Daniel Vetter71286452017-04-03 10:33:04 +0200487 else
488 return restore_fbdev_mode_legacy(fb_helper);
489}
490
Rob Clark5ea1f752014-05-30 12:29:48 -0400491/**
492 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
Noralf Trønnesc7779902017-10-30 16:39:37 +0100493 * @fb_helper: driver-allocated fbdev helper, can be NULL
Rob Clark5ea1f752014-05-30 12:29:48 -0400494 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100495 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400496 * when implementing an fbcon on top of kms using this helper. This ensures that
497 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200498 *
499 * RETURNS:
500 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400501 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200502int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400503{
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200504 bool do_delayed;
505 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000506
Noralf Trønnesc7779902017-10-30 16:39:37 +0100507 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200508 return -ENODEV;
509
Daniel Vetterca91a272017-07-06 15:00:21 +0200510 if (READ_ONCE(fb_helper->deferred_setup))
511 return 0;
512
Thierry Redinge9827d82017-07-04 17:18:23 +0200513 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400514 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000515
516 do_delayed = fb_helper->delayed_hotplug;
517 if (do_delayed)
518 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200519 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000520
521 if (do_delayed)
522 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200523
Rob Clark5ea1f752014-05-30 12:29:48 -0400524 return ret;
525}
526EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100527
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200528static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
529{
530 struct drm_device *dev = fb_helper->dev;
531 struct drm_crtc *crtc;
532 int bound = 0, crtcs_bound = 0;
533
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200534 /*
535 * Sometimes user space wants everything disabled, so don't steal the
536 * display if there's a master.
537 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200538 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200539 return false;
540
541 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200542 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200543 if (crtc->primary->fb)
544 crtcs_bound++;
545 if (crtc->primary->fb == fb_helper->fb)
546 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200547 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200548 }
549
550 if (bound < crtcs_bound)
551 return false;
552
553 return true;
554}
555
556#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100557/*
558 * restore fbcon display for all kms driver's using this helper, used for sysrq
559 * and panic handling.
560 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530561static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000562{
Dave Airlie785b93e2009-08-28 15:46:53 +1000563 bool ret, error = false;
564 struct drm_fb_helper *helper;
565
566 if (list_empty(&kernel_fb_helper_list))
567 return false;
568
569 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200570 struct drm_device *dev = helper->dev;
571
572 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100573 continue;
574
Thierry Redinge9827d82017-07-04 17:18:23 +0200575 mutex_lock(&helper->lock);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200576 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100577 if (ret)
578 error = true;
Thierry Redinge9827d82017-07-04 17:18:23 +0200579 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000580 }
581 return error;
582}
583
Dave Airlie785b93e2009-08-28 15:46:53 +1000584static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
585{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100586 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200587
Daniel Vetterd21bf462013-01-20 18:09:52 +0100588 ret = drm_fb_helper_force_kernel_mode();
589 if (ret == true)
590 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000591}
592static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
593
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700594static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000595{
596 schedule_work(&drm_fb_helper_restore_work);
597}
598
599static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
600 .handler = drm_fb_helper_sysrq,
601 .help_msg = "force-fb(V)",
602 .action_msg = "Restore framebuffer console",
603};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000604#else
605static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200606#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000607
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200608static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000609{
Dave Airlie785b93e2009-08-28 15:46:53 +1000610 struct drm_device *dev = fb_helper->dev;
611 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700612 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700613 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000614
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200615 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700616 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000617 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000618
Dave Airlie8be48d92010-03-30 05:34:14 +0000619 if (!crtc->enabled)
620 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000621
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100622 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000623 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700624 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200625 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500626 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100627 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700628 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000629 }
Daniel Vetter84849902012-12-02 00:28:11 +0100630 drm_modeset_unlock_all(dev);
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200631}
632
633static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
634{
635 struct drm_fb_helper *fb_helper = info->par;
636
637 /*
638 * For each CRTC in this fb, turn the connectors on/off.
639 */
640 mutex_lock(&fb_helper->lock);
641 if (!drm_fb_helper_is_bound(fb_helper)) {
642 mutex_unlock(&fb_helper->lock);
643 return;
644 }
645
646 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
647 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
648 else
649 dpms_legacy(fb_helper, dpms_mode);
Thierry Redinge9827d82017-07-04 17:18:23 +0200650 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000651}
652
Daniel Vetter207fd322013-01-20 22:13:14 +0100653/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100654 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100655 * @blank: desired blanking state
656 * @info: fbdev registered by the helper
657 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000658int drm_fb_helper_blank(int blank, struct fb_info *info)
659{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200660 if (oops_in_progress)
661 return -EBUSY;
662
Dave Airlie785b93e2009-08-28 15:46:53 +1000663 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000664 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000665 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100666 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000667 break;
James Simmons731b5a12009-10-29 20:39:07 +0000668 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000669 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100670 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000671 break;
James Simmons731b5a12009-10-29 20:39:07 +0000672 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000673 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100674 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000675 break;
James Simmons731b5a12009-10-29 20:39:07 +0000676 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000677 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100678 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000679 break;
James Simmons731b5a12009-10-29 20:39:07 +0000680 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000681 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100682 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000683 break;
684 }
685 return 0;
686}
687EXPORT_SYMBOL(drm_fb_helper_blank);
688
Ville Syrjäläa2889602016-10-26 17:41:18 +0300689static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
690 struct drm_mode_set *modeset)
691{
692 int i;
693
694 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100695 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300696 modeset->connectors[i] = NULL;
697 }
698 modeset->num_connectors = 0;
699
700 drm_mode_destroy(helper->dev, modeset->mode);
701 modeset->mode = NULL;
702
703 /* FIXME should hold a ref? */
704 modeset->fb = NULL;
705}
706
Dave Airlie785b93e2009-08-28 15:46:53 +1000707static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
708{
709 int i;
710
Dave Airlie6e86d582016-04-27 11:24:51 +1000711 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100712 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000713 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000714 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000715 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300716
Sascha Hauera1b77362012-02-01 11:38:22 +0100717 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300718 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
719
720 drm_fb_helper_modeset_release(helper, modeset);
721 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100722 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000723 kfree(helper->crtc_info);
724}
725
Noralf Trønnescfe63422016-08-23 13:54:06 +0200726static void drm_fb_helper_resume_worker(struct work_struct *work)
727{
728 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
729 resume_work);
730
731 console_lock();
732 fb_set_suspend(helper->fbdev, 0);
733 console_unlock();
734}
735
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200736static void drm_fb_helper_dirty_work(struct work_struct *work)
737{
738 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
739 dirty_work);
740 struct drm_clip_rect *clip = &helper->dirty_clip;
741 struct drm_clip_rect clip_copy;
742 unsigned long flags;
743
744 spin_lock_irqsave(&helper->dirty_lock, flags);
745 clip_copy = *clip;
746 clip->x1 = clip->y1 = ~0;
747 clip->x2 = clip->y2 = 0;
748 spin_unlock_irqrestore(&helper->dirty_lock, flags);
749
Takashi Iwai87d3b652016-10-20 17:05:30 +0200750 /* call dirty callback only when it has been really touched */
751 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
752 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200753}
754
Daniel Vetter207fd322013-01-20 22:13:14 +0100755/**
Thierry Reding10a23102014-06-27 17:19:24 +0200756 * drm_fb_helper_prepare - setup a drm_fb_helper structure
757 * @dev: DRM device
758 * @helper: driver-allocated fbdev helper structure to set up
759 * @funcs: pointer to structure of functions associate with this helper
760 *
761 * Sets up the bare minimum to make the framebuffer helper usable. This is
762 * useful to implement race-free initialization of the polling helpers.
763 */
764void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
765 const struct drm_fb_helper_funcs *funcs)
766{
767 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200768 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200769 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200770 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
771 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200772 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200773 helper->funcs = funcs;
774 helper->dev = dev;
775}
776EXPORT_SYMBOL(drm_fb_helper_prepare);
777
778/**
Daniel Vettered84e252017-02-07 15:10:49 +0100779 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100780 * @dev: drm device
781 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100782 * @max_conn_count: max connector count
783 *
784 * This allocates the structures for the fbdev helper with the given limits.
785 * Note that this won't yet touch the hardware (through the driver interfaces)
786 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
787 * to allow driver writes more control over the exact init sequence.
788 *
Thierry Reding10a23102014-06-27 17:19:24 +0200789 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100790 *
791 * RETURNS:
792 * Zero if everything went ok, nonzero otherwise.
793 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000794int drm_fb_helper_init(struct drm_device *dev,
795 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200796 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000797{
Dave Airlie785b93e2009-08-28 15:46:53 +1000798 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200799 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000800 int i;
801
Daniel Vetterf64c5572015-08-25 15:45:13 +0200802 if (!drm_fbdev_emulation)
803 return 0;
804
Xiubo Li04cfe972014-03-10 09:33:58 +0800805 if (!max_conn_count)
806 return -EINVAL;
807
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200808 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000809 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000810 return -ENOMEM;
811
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200812 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000813 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
814 if (!fb_helper->connector_info) {
815 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000816 return -ENOMEM;
817 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000818 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000819 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000820
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200821 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000822 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000823 kcalloc(max_conn_count,
824 sizeof(struct drm_connector *),
825 GFP_KERNEL);
826
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200827 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000828 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000829 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000830 }
831
832 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200833 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000834 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000835 i++;
836 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100837
Dave Airlie785b93e2009-08-28 15:46:53 +1000838 return 0;
839out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000840 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000841 return -ENOMEM;
842}
Dave Airlie4abe3522010-03-30 05:34:18 +0000843EXPORT_SYMBOL(drm_fb_helper_init);
844
Archit Tanejab8017d62015-07-22 14:57:56 +0530845/**
846 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
847 * @fb_helper: driver-allocated fbdev helper
848 *
849 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100850 * by the driver within the fb_probe fb_helper callback function. Drivers do not
851 * need to release the allocated fb_info structure themselves, this is
852 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530853 *
854 * RETURNS:
855 * fb_info pointer if things went okay, pointer containing error code
856 * otherwise
857 */
858struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
859{
860 struct device *dev = fb_helper->dev->dev;
861 struct fb_info *info;
862 int ret;
863
864 info = framebuffer_alloc(0, dev);
865 if (!info)
866 return ERR_PTR(-ENOMEM);
867
868 ret = fb_alloc_cmap(&info->cmap, 256, 0);
869 if (ret)
870 goto err_release;
871
872 info->apertures = alloc_apertures(1);
873 if (!info->apertures) {
874 ret = -ENOMEM;
875 goto err_free_cmap;
876 }
877
878 fb_helper->fbdev = info;
879
880 return info;
881
882err_free_cmap:
883 fb_dealloc_cmap(&info->cmap);
884err_release:
885 framebuffer_release(info);
886 return ERR_PTR(ret);
887}
888EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
889
890/**
891 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
Noralf Trønnesc7779902017-10-30 16:39:37 +0100892 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Tanejab8017d62015-07-22 14:57:56 +0530893 *
894 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100895 * framebuffer device. This must be called before releasing all resources for
896 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530897 */
898void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
899{
900 if (fb_helper && fb_helper->fbdev)
901 unregister_framebuffer(fb_helper->fbdev);
902}
903EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
904
905/**
Daniel Vettered84e252017-02-07 15:10:49 +0100906 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100907 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vettered84e252017-02-07 15:10:49 +0100908 *
909 * This cleans up all remaining resources associated with @fb_helper. Must be
910 * called after drm_fb_helper_unlink_fbi() was called.
911 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000912void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
913{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100914 struct fb_info *info;
915
916 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200917 return;
918
Noralf Trønnesb52f09c2017-08-28 19:17:43 +0200919 cancel_work_sync(&fb_helper->resume_work);
920 cancel_work_sync(&fb_helper->dirty_work);
921
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100922 info = fb_helper->fbdev;
923 if (info) {
924 if (info->cmap.len)
925 fb_dealloc_cmap(&info->cmap);
926 framebuffer_release(info);
927 }
928 fb_helper->fbdev = NULL;
929
Chris Wilsona53ca632016-11-29 12:02:17 +0000930 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000931 if (!list_empty(&fb_helper->kernel_fb_list)) {
932 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200933 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000934 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000935 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000936 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000937
Thierry Redinge9827d82017-07-04 17:18:23 +0200938 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000939 drm_fb_helper_crtc_free(fb_helper);
940
Dave Airlie4abe3522010-03-30 05:34:18 +0000941}
942EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000943
Archit Taneja47074ab2015-07-22 14:57:57 +0530944/**
945 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
Noralf Trønnesc7779902017-10-30 16:39:37 +0100946 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Taneja47074ab2015-07-22 14:57:57 +0530947 *
948 * A wrapper around unlink_framebuffer implemented by fbdev core
949 */
950void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
951{
952 if (fb_helper && fb_helper->fbdev)
953 unlink_framebuffer(fb_helper->fbdev);
954}
955EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
956
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200957static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
958 u32 width, u32 height)
959{
960 struct drm_fb_helper *helper = info->par;
961 struct drm_clip_rect *clip = &helper->dirty_clip;
962 unsigned long flags;
963
964 if (!helper->fb->funcs->dirty)
965 return;
966
967 spin_lock_irqsave(&helper->dirty_lock, flags);
968 clip->x1 = min_t(u32, clip->x1, x);
969 clip->y1 = min_t(u32, clip->y1, y);
970 clip->x2 = max_t(u32, clip->x2, x + width);
971 clip->y2 = max_t(u32, clip->y2, y + height);
972 spin_unlock_irqrestore(&helper->dirty_lock, flags);
973
974 schedule_work(&helper->dirty_work);
975}
976
977/**
978 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
979 * @info: fb_info struct pointer
980 * @pagelist: list of dirty mmap framebuffer pages
981 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100982 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200983 * callback function for flushing the fbdev mmap writes.
984 */
985void drm_fb_helper_deferred_io(struct fb_info *info,
986 struct list_head *pagelist)
987{
988 unsigned long start, end, min, max;
989 struct page *page;
990 u32 y1, y2;
991
992 min = ULONG_MAX;
993 max = 0;
994 list_for_each_entry(page, pagelist, lru) {
995 start = page->index << PAGE_SHIFT;
996 end = start + PAGE_SIZE - 1;
997 min = min(min, start);
998 max = max(max, end);
999 }
1000
1001 if (min < max) {
1002 y1 = min / info->fix.line_length;
1003 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1004 info->var.yres);
1005 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1006 }
1007}
1008EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1009
Archit Tanejacbb1a822015-07-31 16:21:41 +05301010/**
1011 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1012 * @info: fb_info struct pointer
1013 * @buf: userspace buffer to read from framebuffer memory
1014 * @count: number of bytes to read from framebuffer memory
1015 * @ppos: read offset within framebuffer memory
1016 *
1017 * A wrapper around fb_sys_read implemented by fbdev core
1018 */
1019ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1020 size_t count, loff_t *ppos)
1021{
1022 return fb_sys_read(info, buf, count, ppos);
1023}
1024EXPORT_SYMBOL(drm_fb_helper_sys_read);
1025
1026/**
1027 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1028 * @info: fb_info struct pointer
1029 * @buf: userspace buffer to write to framebuffer memory
1030 * @count: number of bytes to write to framebuffer memory
1031 * @ppos: write offset within framebuffer memory
1032 *
1033 * A wrapper around fb_sys_write implemented by fbdev core
1034 */
1035ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1036 size_t count, loff_t *ppos)
1037{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001038 ssize_t ret;
1039
1040 ret = fb_sys_write(info, buf, count, ppos);
1041 if (ret > 0)
1042 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1043 info->var.yres);
1044
1045 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301046}
1047EXPORT_SYMBOL(drm_fb_helper_sys_write);
1048
Archit Taneja742547b2015-07-31 16:21:42 +05301049/**
1050 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1051 * @info: fbdev registered by the helper
1052 * @rect: info about rectangle to fill
1053 *
1054 * A wrapper around sys_fillrect implemented by fbdev core
1055 */
1056void drm_fb_helper_sys_fillrect(struct fb_info *info,
1057 const struct fb_fillrect *rect)
1058{
1059 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001060 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1061 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301062}
1063EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1064
1065/**
1066 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1067 * @info: fbdev registered by the helper
1068 * @area: info about area to copy
1069 *
1070 * A wrapper around sys_copyarea implemented by fbdev core
1071 */
1072void drm_fb_helper_sys_copyarea(struct fb_info *info,
1073 const struct fb_copyarea *area)
1074{
1075 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001076 drm_fb_helper_dirty(info, area->dx, area->dy,
1077 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301078}
1079EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1080
1081/**
1082 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1083 * @info: fbdev registered by the helper
1084 * @image: info about image to blit
1085 *
1086 * A wrapper around sys_imageblit implemented by fbdev core
1087 */
1088void drm_fb_helper_sys_imageblit(struct fb_info *info,
1089 const struct fb_image *image)
1090{
1091 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001092 drm_fb_helper_dirty(info, image->dx, image->dy,
1093 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301094}
1095EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1096
1097/**
1098 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1099 * @info: fbdev registered by the helper
1100 * @rect: info about rectangle to fill
1101 *
1102 * A wrapper around cfb_imageblit implemented by fbdev core
1103 */
1104void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1105 const struct fb_fillrect *rect)
1106{
1107 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001108 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1109 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301110}
1111EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1112
1113/**
1114 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1115 * @info: fbdev registered by the helper
1116 * @area: info about area to copy
1117 *
1118 * A wrapper around cfb_copyarea implemented by fbdev core
1119 */
1120void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1121 const struct fb_copyarea *area)
1122{
1123 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001124 drm_fb_helper_dirty(info, area->dx, area->dy,
1125 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301126}
1127EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1128
1129/**
1130 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1131 * @info: fbdev registered by the helper
1132 * @image: info about image to blit
1133 *
1134 * A wrapper around cfb_imageblit implemented by fbdev core
1135 */
1136void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1137 const struct fb_image *image)
1138{
1139 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001140 drm_fb_helper_dirty(info, image->dx, image->dy,
1141 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301142}
1143EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1144
Archit Tanejafdefa582015-07-31 16:21:43 +05301145/**
1146 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
Noralf Trønnesc7779902017-10-30 16:39:37 +01001147 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001148 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301149 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001150 * A wrapper around fb_set_suspend implemented by fbdev core.
1151 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1152 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301153 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001154void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301155{
1156 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001157 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301158}
1159EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1160
Noralf Trønnescfe63422016-08-23 13:54:06 +02001161/**
1162 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1163 * takes the console lock
Noralf Trønnesc7779902017-10-30 16:39:37 +01001164 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001165 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001166 *
1167 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1168 * isn't available on resume, a worker is tasked with waiting for the lock
1169 * to become available. The console lock can be pretty contented on resume
1170 * due to all the printk activity.
1171 *
1172 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001173 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001174 *
1175 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1176 */
1177void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001178 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001179{
1180 if (!fb_helper || !fb_helper->fbdev)
1181 return;
1182
1183 /* make sure there's no pending/ongoing resume */
1184 flush_work(&fb_helper->resume_work);
1185
1186 if (suspend) {
1187 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1188 return;
1189
1190 console_lock();
1191
1192 } else {
1193 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1194 return;
1195
1196 if (!console_trylock()) {
1197 schedule_work(&fb_helper->resume_work);
1198 return;
1199 }
1200 }
1201
1202 fb_set_suspend(fb_helper->fbdev, suspend);
1203 console_unlock();
1204}
1205EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1206
Peter Rosinb8e2b012017-07-04 12:36:57 +02001207static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1208{
1209 u32 *palette = (u32 *)info->pseudo_palette;
1210 int i;
1211
1212 if (cmap->start + cmap->len > 16)
1213 return -EINVAL;
1214
1215 for (i = 0; i < cmap->len; ++i) {
1216 u16 red = cmap->red[i];
1217 u16 green = cmap->green[i];
1218 u16 blue = cmap->blue[i];
1219 u32 value;
1220
1221 red >>= 16 - info->var.red.length;
1222 green >>= 16 - info->var.green.length;
1223 blue >>= 16 - info->var.blue.length;
1224 value = (red << info->var.red.offset) |
1225 (green << info->var.green.offset) |
1226 (blue << info->var.blue.offset);
1227 if (info->var.transp.length > 0) {
1228 u32 mask = (1 << info->var.transp.length) - 1;
1229
1230 mask <<= info->var.transp.offset;
1231 value |= mask;
1232 }
1233 palette[cmap->start + i] = value;
1234 }
1235
1236 return 0;
1237}
1238
Peter Rosin964c6002017-07-13 18:25:27 +02001239static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
Dave Airlie068143d2009-10-05 09:58:02 +10001240{
1241 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie068143d2009-10-05 09:58:02 +10001242 struct drm_crtc *crtc;
Peter Rosina3562a02017-07-04 12:36:58 +02001243 u16 *r, *g, *b;
Peter Rosin964c6002017-07-13 18:25:27 +02001244 int i, ret = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001245
Peter Rosin964c6002017-07-13 18:25:27 +02001246 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001247 for (i = 0; i < fb_helper->crtc_count; i++) {
1248 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Peter Rosin964c6002017-07-13 18:25:27 +02001249 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1250 return -EINVAL;
Dave Airlie068143d2009-10-05 09:58:02 +10001251
Peter Rosin964c6002017-07-13 18:25:27 +02001252 if (cmap->start + cmap->len > crtc->gamma_size)
1253 return -EINVAL;
Peter Rosina3562a02017-07-04 12:36:58 +02001254
1255 r = crtc->gamma_store;
1256 g = r + crtc->gamma_size;
1257 b = g + crtc->gamma_size;
1258
1259 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1260 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1261 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1262
Peter Rosin964c6002017-07-13 18:25:27 +02001263 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1264 crtc->gamma_size, NULL);
1265 if (ret)
1266 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001267 }
Peter Rosin964c6002017-07-13 18:25:27 +02001268 drm_modeset_unlock_all(fb_helper->dev);
1269
1270 return ret;
1271}
1272
1273static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1274 struct fb_cmap *cmap)
1275{
1276 struct drm_device *dev = crtc->dev;
1277 struct drm_property_blob *gamma_lut;
1278 struct drm_color_lut *lut;
1279 int size = crtc->gamma_size;
1280 int i;
1281
1282 if (!size || cmap->start + cmap->len > size)
1283 return ERR_PTR(-EINVAL);
1284
1285 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1286 if (IS_ERR(gamma_lut))
1287 return gamma_lut;
1288
1289 lut = (struct drm_color_lut *)gamma_lut->data;
1290 if (cmap->start || cmap->len != size) {
1291 u16 *r = crtc->gamma_store;
1292 u16 *g = r + crtc->gamma_size;
1293 u16 *b = g + crtc->gamma_size;
1294
1295 for (i = 0; i < cmap->start; i++) {
1296 lut[i].red = r[i];
1297 lut[i].green = g[i];
1298 lut[i].blue = b[i];
1299 }
1300 for (i = cmap->start + cmap->len; i < size; i++) {
1301 lut[i].red = r[i];
1302 lut[i].green = g[i];
1303 lut[i].blue = b[i];
1304 }
1305 }
1306
1307 for (i = 0; i < cmap->len; i++) {
1308 lut[cmap->start + i].red = cmap->red[i];
1309 lut[cmap->start + i].green = cmap->green[i];
1310 lut[cmap->start + i].blue = cmap->blue[i];
1311 }
1312
1313 return gamma_lut;
1314}
1315
1316static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1317{
1318 struct drm_fb_helper *fb_helper = info->par;
1319 struct drm_device *dev = fb_helper->dev;
1320 struct drm_property_blob *gamma_lut = NULL;
1321 struct drm_modeset_acquire_ctx ctx;
1322 struct drm_crtc_state *crtc_state;
1323 struct drm_atomic_state *state;
1324 struct drm_crtc *crtc;
1325 u16 *r, *g, *b;
1326 int i, ret = 0;
1327 bool replaced;
1328
1329 drm_modeset_acquire_init(&ctx, 0);
1330
1331 state = drm_atomic_state_alloc(dev);
1332 if (!state) {
1333 ret = -ENOMEM;
1334 goto out_ctx;
1335 }
1336
1337 state->acquire_ctx = &ctx;
1338retry:
1339 for (i = 0; i < fb_helper->crtc_count; i++) {
1340 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1341
1342 if (!gamma_lut)
1343 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1344 if (IS_ERR(gamma_lut)) {
1345 ret = PTR_ERR(gamma_lut);
1346 gamma_lut = NULL;
1347 goto out_state;
1348 }
1349
1350 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1351 if (IS_ERR(crtc_state)) {
1352 ret = PTR_ERR(crtc_state);
1353 goto out_state;
1354 }
1355
1356 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1357 NULL);
1358 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1359 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1360 gamma_lut);
1361 crtc_state->color_mgmt_changed |= replaced;
1362 }
1363
1364 ret = drm_atomic_commit(state);
1365 if (ret)
1366 goto out_state;
1367
1368 for (i = 0; i < fb_helper->crtc_count; i++) {
1369 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1370
1371 r = crtc->gamma_store;
1372 g = r + crtc->gamma_size;
1373 b = g + crtc->gamma_size;
1374
1375 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1376 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1377 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1378 }
1379
1380out_state:
1381 if (ret == -EDEADLK)
1382 goto backoff;
1383
1384 drm_property_blob_put(gamma_lut);
1385 drm_atomic_state_put(state);
1386out_ctx:
1387 drm_modeset_drop_locks(&ctx);
1388 drm_modeset_acquire_fini(&ctx);
1389
1390 return ret;
1391
1392backoff:
1393 drm_atomic_state_clear(state);
1394 drm_modeset_backoff(&ctx);
1395 goto retry;
1396}
1397
1398/**
1399 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1400 * @cmap: cmap to set
1401 * @info: fbdev registered by the helper
1402 */
1403int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1404{
1405 struct drm_fb_helper *fb_helper = info->par;
1406 int ret;
1407
1408 if (oops_in_progress)
1409 return -EBUSY;
1410
1411 mutex_lock(&fb_helper->lock);
1412
1413 if (!drm_fb_helper_is_bound(fb_helper)) {
1414 ret = -EBUSY;
1415 goto out;
1416 }
1417
1418 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1419 ret = setcmap_pseudo_palette(cmap, info);
1420 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1421 ret = setcmap_atomic(cmap, info);
1422 else
1423 ret = setcmap_legacy(cmap, info);
1424
1425out:
Thierry Redinge9827d82017-07-04 17:18:23 +02001426 mutex_unlock(&fb_helper->lock);
Peter Rosin964c6002017-07-13 18:25:27 +02001427
1428 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001429}
1430EXPORT_SYMBOL(drm_fb_helper_setcmap);
1431
Daniel Vetter207fd322013-01-20 22:13:14 +01001432/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001433 * drm_fb_helper_ioctl - legacy ioctl implementation
1434 * @info: fbdev registered by the helper
1435 * @cmd: ioctl command
1436 * @arg: ioctl argument
1437 *
1438 * A helper to implement the standard fbdev ioctl. Only
1439 * FBIO_WAITFORVSYNC is implemented for now.
1440 */
1441int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1442 unsigned long arg)
1443{
1444 struct drm_fb_helper *fb_helper = info->par;
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001445 struct drm_mode_set *mode_set;
1446 struct drm_crtc *crtc;
1447 int ret = 0;
1448
Thierry Redinge9827d82017-07-04 17:18:23 +02001449 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001450 if (!drm_fb_helper_is_bound(fb_helper)) {
1451 ret = -EBUSY;
1452 goto unlock;
1453 }
1454
1455 switch (cmd) {
1456 case FBIO_WAITFORVSYNC:
1457 /*
1458 * Only consider the first CRTC.
1459 *
1460 * This ioctl is supposed to take the CRTC number as
1461 * an argument, but in fbdev times, what that number
1462 * was supposed to be was quite unclear, different
1463 * drivers were passing that argument differently
1464 * (some by reference, some by value), and most of the
1465 * userspace applications were just hardcoding 0 as an
1466 * argument.
1467 *
1468 * The first CRTC should be the integrated panel on
1469 * most drivers, so this is the best choice we can
1470 * make. If we're not smart enough here, one should
1471 * just consider switch the userspace to KMS.
1472 */
1473 mode_set = &fb_helper->crtc_info[0].mode_set;
1474 crtc = mode_set->crtc;
1475
1476 /*
1477 * Only wait for a vblank event if the CRTC is
1478 * enabled, otherwise just don't do anythintg,
1479 * not even report an error.
1480 */
1481 ret = drm_crtc_vblank_get(crtc);
1482 if (!ret) {
1483 drm_crtc_wait_one_vblank(crtc);
1484 drm_crtc_vblank_put(crtc);
1485 }
1486
1487 ret = 0;
1488 goto unlock;
1489 default:
1490 ret = -ENOTTY;
1491 }
1492
1493unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001494 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001495 return ret;
1496}
1497EXPORT_SYMBOL(drm_fb_helper_ioctl);
1498
1499/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001500 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001501 * @var: screeninfo to check
1502 * @info: fbdev registered by the helper
1503 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001504int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1505 struct fb_info *info)
1506{
1507 struct drm_fb_helper *fb_helper = info->par;
1508 struct drm_framebuffer *fb = fb_helper->fb;
1509 int depth;
1510
Jason Wesself90ebd92010-08-05 09:22:32 -05001511 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001512 return -EINVAL;
1513
Stefan Agner865afb12016-10-11 16:15:04 -07001514 /*
1515 * Changes struct fb_var_screeninfo are currently not pushed back
1516 * to KMS, hence fail if different settings are requested.
1517 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001518 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001519 var->xres > fb->width || var->yres > fb->height ||
1520 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1521 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001522 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1523 var->xres, var->yres, var->bits_per_pixel,
1524 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001525 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001526 return -EINVAL;
1527 }
1528
1529 switch (var->bits_per_pixel) {
1530 case 16:
1531 depth = (var->green.length == 6) ? 16 : 15;
1532 break;
1533 case 32:
1534 depth = (var->transp.length > 0) ? 32 : 24;
1535 break;
1536 default:
1537 depth = var->bits_per_pixel;
1538 break;
1539 }
1540
1541 switch (depth) {
1542 case 8:
1543 var->red.offset = 0;
1544 var->green.offset = 0;
1545 var->blue.offset = 0;
1546 var->red.length = 8;
1547 var->green.length = 8;
1548 var->blue.length = 8;
1549 var->transp.length = 0;
1550 var->transp.offset = 0;
1551 break;
1552 case 15:
1553 var->red.offset = 10;
1554 var->green.offset = 5;
1555 var->blue.offset = 0;
1556 var->red.length = 5;
1557 var->green.length = 5;
1558 var->blue.length = 5;
1559 var->transp.length = 1;
1560 var->transp.offset = 15;
1561 break;
1562 case 16:
1563 var->red.offset = 11;
1564 var->green.offset = 5;
1565 var->blue.offset = 0;
1566 var->red.length = 5;
1567 var->green.length = 6;
1568 var->blue.length = 5;
1569 var->transp.length = 0;
1570 var->transp.offset = 0;
1571 break;
1572 case 24:
1573 var->red.offset = 16;
1574 var->green.offset = 8;
1575 var->blue.offset = 0;
1576 var->red.length = 8;
1577 var->green.length = 8;
1578 var->blue.length = 8;
1579 var->transp.length = 0;
1580 var->transp.offset = 0;
1581 break;
1582 case 32:
1583 var->red.offset = 16;
1584 var->green.offset = 8;
1585 var->blue.offset = 0;
1586 var->red.length = 8;
1587 var->green.length = 8;
1588 var->blue.length = 8;
1589 var->transp.length = 8;
1590 var->transp.offset = 24;
1591 break;
1592 default:
1593 return -EINVAL;
1594 }
1595 return 0;
1596}
1597EXPORT_SYMBOL(drm_fb_helper_check_var);
1598
Daniel Vetter207fd322013-01-20 22:13:14 +01001599/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001600 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001601 * @info: fbdev registered by the helper
1602 *
1603 * This will let fbcon do the mode init and is called at initialization time by
1604 * the fbdev core when registering the driver, and later on through the hotplug
1605 * callback.
1606 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001607int drm_fb_helper_set_par(struct fb_info *info)
1608{
1609 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001610 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001611
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001612 if (oops_in_progress)
1613 return -EBUSY;
1614
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001615 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001616 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001617 return -EINVAL;
1618 }
1619
Rob Clark5ea1f752014-05-30 12:29:48 -04001620 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001621
Dave Airlie785b93e2009-08-28 15:46:53 +10001622 return 0;
1623}
1624EXPORT_SYMBOL(drm_fb_helper_set_par);
1625
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001626static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
Rob Clark1edf0262015-08-25 15:35:59 -04001627{
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001628 int i;
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001629
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001630 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001631 struct drm_mode_set *mode_set;
1632
1633 mode_set = &fb_helper->crtc_info[i].mode_set;
1634
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001635 mode_set->x = x;
1636 mode_set->y = y;
Rob Clark1edf0262015-08-25 15:35:59 -04001637 }
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001638}
Rob Clark1edf0262015-08-25 15:35:59 -04001639
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001640static int pan_display_atomic(struct fb_var_screeninfo *var,
1641 struct fb_info *info)
1642{
1643 struct drm_fb_helper *fb_helper = info->par;
1644 int ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001645
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001646 pan_set(fb_helper, var->xoffset, var->yoffset);
Rob Clark1edf0262015-08-25 15:35:59 -04001647
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001648 ret = restore_fbdev_mode_atomic(fb_helper, true);
1649 if (!ret) {
1650 info->var.xoffset = var->xoffset;
1651 info->var.yoffset = var->yoffset;
1652 } else
1653 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001654
Rob Clark1edf0262015-08-25 15:35:59 -04001655 return ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001656}
1657
Daniel Vetter71286452017-04-03 10:33:04 +02001658static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001659 struct fb_info *info)
1660{
1661 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001662 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001663 int ret = 0;
1664 int i;
1665
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001666 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001667 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001668 modeset = &fb_helper->crtc_info[i].mode_set;
1669
1670 modeset->x = var->xoffset;
1671 modeset->y = var->yoffset;
1672
1673 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001674 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001675 if (!ret) {
1676 info->var.xoffset = var->xoffset;
1677 info->var.yoffset = var->yoffset;
1678 }
1679 }
1680 }
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001681 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001682
1683 return ret;
1684}
1685
1686/**
1687 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1688 * @var: updated screen information
1689 * @info: fbdev registered by the helper
1690 */
1691int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1692 struct fb_info *info)
1693{
1694 struct drm_fb_helper *fb_helper = info->par;
1695 struct drm_device *dev = fb_helper->dev;
1696 int ret;
1697
1698 if (oops_in_progress)
1699 return -EBUSY;
1700
Thierry Redinge9827d82017-07-04 17:18:23 +02001701 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001702 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001703 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001704 return -EBUSY;
1705 }
1706
1707 if (drm_drv_uses_atomic_modeset(dev))
1708 ret = pan_display_atomic(var, info);
1709 else
1710 ret = pan_display_legacy(var, info);
Thierry Redinge9827d82017-07-04 17:18:23 +02001711 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001712
Dave Airlie785b93e2009-08-28 15:46:53 +10001713 return ret;
1714}
1715EXPORT_SYMBOL(drm_fb_helper_pan_display);
1716
Daniel Vetter8acf6582013-01-21 23:38:37 +01001717/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001718 * Allocates the backing storage and sets up the fbdev info structure through
Daniel Vetterca91a272017-07-06 15:00:21 +02001719 * the ->fb_probe callback.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001720 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001721static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1722 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001723{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001724 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001725 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001726 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001727 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001728 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001729
1730 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1731 sizes.surface_depth = 24;
1732 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001733 sizes.fb_width = (u32)-1;
1734 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001735
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001736 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001737 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001738 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001739
Dave Airlie785b93e2009-08-28 15:46:53 +10001740 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001741 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001742 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001743 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001744
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001745 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001746
1747 if (cmdline_mode->bpp_specified) {
1748 switch (cmdline_mode->bpp) {
1749 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001750 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001751 break;
1752 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001753 sizes.surface_depth = 15;
1754 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001755 break;
1756 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001757 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001758 break;
1759 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001760 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001761 break;
1762 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001763 sizes.surface_depth = 24;
1764 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001765 break;
1766 }
1767 break;
1768 }
1769 }
1770
Dave Airlie8be48d92010-03-30 05:34:14 +00001771 crtc_count = 0;
1772 for (i = 0; i < fb_helper->crtc_count; i++) {
1773 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001774 struct drm_mode_set *mode_set;
1775 int x, y, j;
1776 /* in case of tile group, are we the last tile vert or horiz?
1777 * If no tile group you are always the last one both vertically
1778 * and horizontally
1779 */
1780 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001781
Dave Airlie8be48d92010-03-30 05:34:14 +00001782 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001783 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001784
1785 if (!desired_mode)
1786 continue;
1787
1788 crtc_count++;
1789
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001790 x = fb_helper->crtc_info[i].x;
1791 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001792
1793 if (gamma_size == 0)
1794 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1795
1796 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1797 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001798
1799 for (j = 0; j < mode_set->num_connectors; j++) {
1800 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001801
Rob Clark0e3704c2015-03-11 10:23:14 -04001802 if (connector->has_tile) {
1803 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1804 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1805 /* cloning to multiple tiles is just crazy-talk, so: */
1806 break;
1807 }
1808 }
1809
1810 if (lasth)
1811 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1812 if (lastv)
1813 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001814 }
1815
Dave Airlie38651672010-03-30 05:34:13 +00001816 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Daniel Vetterca91a272017-07-06 15:00:21 +02001817 DRM_INFO("Cannot find any crtc or sizes\n");
1818 return -EAGAIN;
Dave Airlie785b93e2009-08-28 15:46:53 +10001819 }
1820
Xinliang Liu5f152572017-02-15 17:19:08 +01001821 /* Handle our overallocation */
1822 sizes.surface_height *= drm_fbdev_overalloc;
1823 sizes.surface_height /= 100;
1824
Dave Airlie38651672010-03-30 05:34:13 +00001825 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001826 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1827 if (ret < 0)
1828 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001829
Dave Airlie785b93e2009-08-28 15:46:53 +10001830 return 0;
1831}
Dave Airlie785b93e2009-08-28 15:46:53 +10001832
Daniel Vetter207fd322013-01-20 22:13:14 +01001833/**
1834 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1835 * @info: fbdev registered by the helper
1836 * @pitch: desired pitch
1837 * @depth: desired depth
1838 *
1839 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1840 * fbdev emulations. Drivers which support acceleration methods which impose
1841 * additional constraints need to set up their own limits.
1842 *
1843 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001844 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001845 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001846void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1847 uint32_t depth)
1848{
1849 info->fix.type = FB_TYPE_PACKED_PIXELS;
1850 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1851 FB_VISUAL_TRUECOLOR;
1852 info->fix.mmio_start = 0;
1853 info->fix.mmio_len = 0;
1854 info->fix.type_aux = 0;
1855 info->fix.xpanstep = 1; /* doing it in hw */
1856 info->fix.ypanstep = 1; /* doing it in hw */
1857 info->fix.ywrapstep = 0;
1858 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001859
1860 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001861}
1862EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1863
Daniel Vetter207fd322013-01-20 22:13:14 +01001864/**
1865 * drm_fb_helper_fill_var - initalizes variable fbdev information
1866 * @info: fbdev instance to set up
1867 * @fb_helper: fb helper instance to use as template
1868 * @fb_width: desired fb width
1869 * @fb_height: desired fb height
1870 *
1871 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001872 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001873 *
1874 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001875 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1876 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001877 */
Dave Airlie38651672010-03-30 05:34:13 +00001878void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001879 uint32_t fb_width, uint32_t fb_height)
1880{
Dave Airlie38651672010-03-30 05:34:13 +00001881 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001882
Dave Airlie38651672010-03-30 05:34:13 +00001883 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001884 info->var.xres_virtual = fb->width;
1885 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001886 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001887 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001888 info->var.xoffset = 0;
1889 info->var.yoffset = 0;
1890 info->var.activate = FB_ACTIVATE_NOW;
Dave Airlie785b93e2009-08-28 15:46:53 +10001891
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001892 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001893 case 8:
1894 info->var.red.offset = 0;
1895 info->var.green.offset = 0;
1896 info->var.blue.offset = 0;
1897 info->var.red.length = 8; /* 8bit DAC */
1898 info->var.green.length = 8;
1899 info->var.blue.length = 8;
1900 info->var.transp.offset = 0;
1901 info->var.transp.length = 0;
1902 break;
1903 case 15:
1904 info->var.red.offset = 10;
1905 info->var.green.offset = 5;
1906 info->var.blue.offset = 0;
1907 info->var.red.length = 5;
1908 info->var.green.length = 5;
1909 info->var.blue.length = 5;
1910 info->var.transp.offset = 15;
1911 info->var.transp.length = 1;
1912 break;
1913 case 16:
1914 info->var.red.offset = 11;
1915 info->var.green.offset = 5;
1916 info->var.blue.offset = 0;
1917 info->var.red.length = 5;
1918 info->var.green.length = 6;
1919 info->var.blue.length = 5;
1920 info->var.transp.offset = 0;
1921 break;
1922 case 24:
1923 info->var.red.offset = 16;
1924 info->var.green.offset = 8;
1925 info->var.blue.offset = 0;
1926 info->var.red.length = 8;
1927 info->var.green.length = 8;
1928 info->var.blue.length = 8;
1929 info->var.transp.offset = 0;
1930 info->var.transp.length = 0;
1931 break;
1932 case 32:
1933 info->var.red.offset = 16;
1934 info->var.green.offset = 8;
1935 info->var.blue.offset = 0;
1936 info->var.red.length = 8;
1937 info->var.green.length = 8;
1938 info->var.blue.length = 8;
1939 info->var.transp.offset = 24;
1940 info->var.transp.length = 8;
1941 break;
1942 default:
1943 break;
1944 }
1945
1946 info->var.xres = fb_width;
1947 info->var.yres = fb_height;
1948}
1949EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001950
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001951static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
Daniel Vettere13a0582017-07-05 06:56:29 +02001952 uint32_t maxX,
1953 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001954{
1955 struct drm_connector *connector;
Daniel Vettere13a0582017-07-05 06:56:29 +02001956 int i, count = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001957
Chris Wilson966a6a12016-11-29 12:02:15 +00001958 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001959 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001960 count += connector->funcs->fill_modes(connector, maxX, maxY);
1961 }
1962
1963 return count;
1964}
1965
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001966struct 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 +00001967{
1968 struct drm_display_mode *mode;
1969
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001970 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001971 if (mode->hdisplay > width ||
1972 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001973 continue;
1974 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1975 return mode;
1976 }
1977 return NULL;
1978}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001979EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001980
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001981static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001982{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001983 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001984}
1985
Vincent Abrioua09759e2017-01-06 17:44:43 +01001986struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00001987{
Chris Wilson1794d252011-04-17 07:43:32 +01001988 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001989 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001990 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001991
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001992 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001993 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001994 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001995
1996 /* attempt to find a matching mode in the list of modes
1997 * we have gotten so far, if not add a CVT mode that conforms
1998 */
1999 if (cmdline_mode->rb || cmdline_mode->margins)
2000 goto create_mode;
2001
Takashi Iwaic683f422014-03-19 14:53:13 +01002002 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002003again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002004 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00002005 /* check width/height */
2006 if (mode->hdisplay != cmdline_mode->xres ||
2007 mode->vdisplay != cmdline_mode->yres)
2008 continue;
2009
2010 if (cmdline_mode->refresh_specified) {
2011 if (mode->vrefresh != cmdline_mode->refresh)
2012 continue;
2013 }
2014
2015 if (cmdline_mode->interlace) {
2016 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2017 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01002018 } else if (prefer_non_interlace) {
2019 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2020 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002021 }
2022 return mode;
2023 }
2024
Takashi Iwaic683f422014-03-19 14:53:13 +01002025 if (prefer_non_interlace) {
2026 prefer_non_interlace = false;
2027 goto again;
2028 }
2029
Dave Airlie38651672010-03-30 05:34:13 +00002030create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01002031 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2032 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002033 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00002034 return mode;
2035}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002036EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002037
2038static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2039{
2040 bool enable;
2041
Sachin Kamat96081cd2012-11-15 03:43:30 +00002042 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00002043 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002044 else
Dave Airlie38651672010-03-30 05:34:13 +00002045 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002046
Dave Airlie38651672010-03-30 05:34:13 +00002047 return enable;
2048}
2049
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002050static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2051 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00002052{
2053 bool any_enabled = false;
2054 struct drm_connector *connector;
2055 int i = 0;
2056
Chris Wilson966a6a12016-11-29 12:02:15 +00002057 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002058 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002059 enabled[i] = drm_connector_enabled(connector, true);
2060 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
2061 enabled[i] ? "yes" : "no");
2062 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00002063 }
2064
2065 if (any_enabled)
2066 return;
2067
Chris Wilson966a6a12016-11-29 12:02:15 +00002068 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002069 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002070 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00002071 }
2072}
2073
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002074static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2075 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002076 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002077 bool *enabled, int width, int height)
2078{
2079 int count, i, j;
2080 bool can_clone = false;
2081 struct drm_fb_helper_connector *fb_helper_conn;
2082 struct drm_display_mode *dmt_mode, *mode;
2083
2084 /* only contemplate cloning in the single crtc case */
2085 if (fb_helper->crtc_count > 1)
2086 return false;
2087
2088 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002089 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002090 if (enabled[i])
2091 count++;
2092 }
2093
2094 /* only contemplate cloning if more than one connector is enabled */
2095 if (count <= 1)
2096 return false;
2097
2098 /* check the command line or if nothing common pick 1024x768 */
2099 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002100 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002101 if (!enabled[i])
2102 continue;
2103 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002104 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002105 if (!modes[i]) {
2106 can_clone = false;
2107 break;
2108 }
2109 for (j = 0; j < i; j++) {
2110 if (!enabled[j])
2111 continue;
2112 if (!drm_mode_equal(modes[j], modes[i]))
2113 can_clone = false;
2114 }
2115 }
2116
2117 if (can_clone) {
2118 DRM_DEBUG_KMS("can clone using command line\n");
2119 return true;
2120 }
2121
2122 /* try and find a 1024x768 mode on each connector */
2123 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002124 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002125
Chris Wilson966a6a12016-11-29 12:02:15 +00002126 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002127 if (!enabled[i])
2128 continue;
2129
2130 fb_helper_conn = fb_helper->connector_info[i];
2131 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2132 if (drm_mode_equal(mode, dmt_mode))
2133 modes[i] = mode;
2134 }
2135 if (!modes[i])
2136 can_clone = false;
2137 }
2138
2139 if (can_clone) {
2140 DRM_DEBUG_KMS("can clone using 1024x768\n");
2141 return true;
2142 }
2143 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2144 return false;
2145}
2146
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002147static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2148 struct drm_display_mode **modes,
2149 struct drm_fb_offset *offsets,
2150 int idx,
2151 int h_idx, int v_idx)
2152{
2153 struct drm_fb_helper_connector *fb_helper_conn;
2154 int i;
2155 int hoffset = 0, voffset = 0;
2156
Chris Wilson966a6a12016-11-29 12:02:15 +00002157 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002158 fb_helper_conn = fb_helper->connector_info[i];
2159 if (!fb_helper_conn->connector->has_tile)
2160 continue;
2161
2162 if (!modes[i] && (h_idx || v_idx)) {
2163 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2164 fb_helper_conn->connector->base.id);
2165 continue;
2166 }
2167 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2168 hoffset += modes[i]->hdisplay;
2169
2170 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2171 voffset += modes[i]->vdisplay;
2172 }
2173 offsets[idx].x = hoffset;
2174 offsets[idx].y = voffset;
2175 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2176 return 0;
2177}
2178
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002179static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002180 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002181 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002182 bool *enabled, int width, int height)
2183{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002184 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002185 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2186 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002187 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002188 int i;
2189
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002190retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002191 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002192 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002193
Chris Wilsonc96521e2016-11-27 17:09:10 +00002194 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002195 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002196
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002197 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002198 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002199 continue;
2200 }
2201
2202 /* first pass over all the untiled connectors */
2203 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2204 continue;
2205
2206 if (tile_pass == 1) {
2207 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2208 fb_helper_conn->connector->tile_v_loc != 0)
2209 continue;
2210
2211 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002212 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002213 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2214 /* if this tile_pass doesn't cover any of the tiles - keep going */
2215 continue;
2216
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002217 /*
2218 * find the tile offsets for this pass - need to find
2219 * all tiles left and above
2220 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002221 drm_get_tile_offsets(fb_helper, modes, offsets,
2222 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2223 }
Dave Airlie38651672010-03-30 05:34:13 +00002224 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002225 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002226
2227 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002228 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002229 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002230 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2231 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 +00002232 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002233 }
2234 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002235 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2236 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002237 break;
2238 }
2239 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2240 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002241 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002242 }
2243
2244 if ((conn_configured & mask) != mask) {
2245 tile_pass++;
2246 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002247 }
2248 return true;
2249}
2250
Dave Airlie8be48d92010-03-30 05:34:14 +00002251static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2252 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002253 struct drm_display_mode **modes,
2254 int n, int width, int height)
2255{
2256 int c, o;
2257 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002258 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002259 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002260 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002261 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002262 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002263
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002264 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002265 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002266
2267 fb_helper_conn = fb_helper->connector_info[n];
2268 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002269
2270 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002271 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002272 if (modes[n] == NULL)
2273 return best_score;
2274
Harsha Sharma4b947b1c2017-10-13 13:07:47 +05302275 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002276 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002277 if (!crtcs)
2278 return best_score;
2279
2280 my_score = 1;
2281 if (connector->status == connector_status_connected)
2282 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002283 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002284 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002285 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002286 my_score++;
2287
2288 connector_funcs = connector->helper_private;
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002289
2290 /*
2291 * If the DRM device implements atomic hooks and ->best_encoder() is
2292 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2293 * helper.
2294 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002295 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002296 !connector_funcs->best_encoder)
2297 encoder = drm_atomic_helper_best_encoder(connector);
2298 else
2299 encoder = connector_funcs->best_encoder(connector);
2300
Dave Airlie38651672010-03-30 05:34:13 +00002301 if (!encoder)
2302 goto out;
2303
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002304 /*
2305 * select a crtc for this connector and then attempt to configure
2306 * remaining connectors
2307 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002308 for (c = 0; c < fb_helper->crtc_count; c++) {
2309 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002310
Sachin Kamat96081cd2012-11-15 03:43:30 +00002311 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002312 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002313
2314 for (o = 0; o < n; o++)
2315 if (best_crtcs[o] == crtc)
2316 break;
2317
2318 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002319 /* ignore cloning unless only a single crtc */
2320 if (fb_helper->crtc_count > 1)
2321 continue;
2322
2323 if (!drm_mode_equal(modes[o], modes[n]))
2324 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002325 }
2326
2327 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002328 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2329 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002330 width, height);
2331 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002332 best_score = score;
2333 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002334 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002335 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002336 }
Dave Airlie38651672010-03-30 05:34:13 +00002337 }
2338out:
2339 kfree(crtcs);
2340 return best_score;
2341}
2342
Chris Wilson64e94402016-11-29 12:02:16 +00002343static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2344 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002345{
Dave Airlie8be48d92010-03-30 05:34:14 +00002346 struct drm_device *dev = fb_helper->dev;
2347 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002348 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002349 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002350 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002351 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002352
2353 DRM_DEBUG_KMS("\n");
Chris Wilson966a6a12016-11-29 12:02:15 +00002354 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002355 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002356
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002357 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002358 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002359 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002360 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002361 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002362 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002363 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002364 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002365 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002366 DRM_ERROR("Memory allocation failed\n");
2367 goto out;
2368 }
2369
Daniel Vettere13a0582017-07-05 06:56:29 +02002370 mutex_lock(&fb_helper->dev->mode_config.mutex);
2371 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2372 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002373 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002374
Jesse Barnes11e17a02013-02-19 13:31:39 -08002375 if (!(fb_helper->funcs->initial_config &&
2376 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002377 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002378 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002379 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2380 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2381 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002382
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002383 if (!drm_target_cloned(fb_helper, modes, offsets,
2384 enabled, width, height) &&
2385 !drm_target_preferred(fb_helper, modes, offsets,
2386 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002387 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002388
2389 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2390 width, height);
2391
2392 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002393 }
Daniel Vettere13a0582017-07-05 06:56:29 +02002394 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002395
Dave Airlie8be48d92010-03-30 05:34:14 +00002396 /* need to set the modesets up here for use later */
2397 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002398 for (i = 0; i < fb_helper->crtc_count; i++)
2399 drm_fb_helper_modeset_release(fb_helper,
2400 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002401
Chris Wilson966a6a12016-11-29 12:02:15 +00002402 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002403 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002404 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002405 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie38651672010-03-30 05:34:13 +00002406
Dave Airlie8be48d92010-03-30 05:34:14 +00002407 if (mode && fb_crtc) {
David Lechner27a061f2017-08-02 13:00:13 -05002408 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Ville Syrjäläa2889602016-10-26 17:41:18 +03002409 struct drm_connector *connector =
2410 fb_helper->connector_info[i]->connector;
2411
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002412 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2413 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002414
Dave Airlie8be48d92010-03-30 05:34:14 +00002415 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002416 fb_crtc->x = offset->x;
2417 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002418 modeset->mode = drm_mode_duplicate(dev,
2419 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002420 drm_connector_get(connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002421 modeset->connectors[modeset->num_connectors++] = connector;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002422 modeset->x = offset->x;
2423 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002424 }
Dave Airlie38651672010-03-30 05:34:13 +00002425 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002426out:
Dave Airlie38651672010-03-30 05:34:13 +00002427 kfree(crtcs);
2428 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002429 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002430 kfree(enabled);
2431}
2432
David Lechnerf461bd22017-08-03 11:19:08 -05002433/*
2434 * This is a continuation of drm_setup_crtcs() that sets up anything related
2435 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2436 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2437 * So, any setup that touches those fields needs to be done here instead of in
2438 * drm_setup_crtcs().
2439 */
2440static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2441{
David Lechner991a3992017-08-04 11:25:24 -05002442 struct fb_info *info = fb_helper->fbdev;
David Lechnerf461bd22017-08-03 11:19:08 -05002443 int i;
2444
2445 for (i = 0; i < fb_helper->crtc_count; i++)
2446 if (fb_helper->crtc_info[i].mode_set.num_connectors)
2447 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
David Lechner991a3992017-08-04 11:25:24 -05002448
2449 mutex_lock(&fb_helper->dev->mode_config.mutex);
2450 drm_fb_helper_for_each_connector(fb_helper, i) {
2451 struct drm_connector *connector =
2452 fb_helper->connector_info[i]->connector;
2453
2454 /* use first connected connector for the physical dimensions */
2455 if (connector->status == connector_status_connected) {
2456 info->var.width = connector->display_info.width_mm;
2457 info->var.height = connector->display_info.height_mm;
2458 break;
2459 }
2460 }
2461 mutex_unlock(&fb_helper->dev->mode_config.mutex);
David Lechnerf461bd22017-08-03 11:19:08 -05002462}
2463
Daniel Vetterca91a272017-07-06 15:00:21 +02002464/* Note: Drops fb_helper->lock before returning. */
2465static int
2466__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2467 int bpp_sel)
2468{
2469 struct drm_device *dev = fb_helper->dev;
2470 struct fb_info *info;
2471 unsigned int width, height;
2472 int ret;
2473
2474 width = dev->mode_config.max_width;
2475 height = dev->mode_config.max_height;
2476
2477 drm_setup_crtcs(fb_helper, width, height);
2478 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2479 if (ret < 0) {
2480 if (ret == -EAGAIN) {
2481 fb_helper->preferred_bpp = bpp_sel;
2482 fb_helper->deferred_setup = true;
2483 ret = 0;
2484 }
2485 mutex_unlock(&fb_helper->lock);
2486
2487 return ret;
2488 }
David Lechnerf461bd22017-08-03 11:19:08 -05002489 drm_setup_crtcs_fb(fb_helper);
Daniel Vetterca91a272017-07-06 15:00:21 +02002490
2491 fb_helper->deferred_setup = false;
2492
2493 info = fb_helper->fbdev;
2494 info->var.pixclock = 0;
2495
2496 /* Need to drop locks to avoid recursive deadlock in
2497 * register_framebuffer. This is ok because the only thing left to do is
2498 * register the fbdev emulation instance in kernel_fb_helper_list. */
2499 mutex_unlock(&fb_helper->lock);
2500
2501 ret = register_framebuffer(info);
2502 if (ret < 0)
2503 return ret;
2504
2505 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2506 info->node, info->fix.id);
2507
2508 mutex_lock(&kernel_fb_helper_lock);
2509 if (list_empty(&kernel_fb_helper_list))
2510 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2511
2512 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2513 mutex_unlock(&kernel_fb_helper_lock);
2514
2515 return 0;
2516}
2517
Dave Airlie38651672010-03-30 05:34:13 +00002518/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002519 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002520 * @fb_helper: fb_helper device struct
2521 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002522 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002523 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002524 * At the moment, this is a cloned configuration across all heads with
2525 * a new framebuffer object as the backing store.
2526 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002527 * Note that this also registers the fbdev and so allows userspace to call into
2528 * the driver through the fbdev interfaces.
2529 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002530 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2531 * to let the driver allocate and initialize the fbdev info structure and the
2532 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002533 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2534 * values for the fbdev info structure.
2535 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002536 * HANG DEBUGGING:
2537 *
2538 * When you have fbcon support built-in or already loaded, this function will do
2539 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2540 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2541 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2542 * to consoles, not even serial console. This means when your driver crashes,
2543 * you will see absolutely nothing else but a system stuck in this function,
2544 * with no further output. Any kind of printk() you place within your own driver
2545 * or in the drm core modeset code will also never show up.
2546 *
2547 * Standard debug practice is to run the fbcon setup without taking the
2548 * console_lock as a hack, to be able to see backtraces and crashes on the
2549 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2550 * cmdline option.
2551 *
2552 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002553 * first modeset from userspace will crash in the same way, and is even easier
2554 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002555 * kernel cmdline option.
2556 *
Dave Airlie38651672010-03-30 05:34:13 +00002557 * RETURNS:
2558 * Zero if everything went ok, nonzero otherwise.
2559 */
Thierry Reding01934c22014-12-19 11:21:32 +01002560int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002561{
Chris Wilson966a6a12016-11-29 12:02:15 +00002562 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002563
Daniel Vetterf64c5572015-08-25 15:45:13 +02002564 if (!drm_fbdev_emulation)
2565 return 0;
2566
Thierry Redinge9827d82017-07-04 17:18:23 +02002567 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002568 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00002569
Daniel Vetterca91a272017-07-06 15:00:21 +02002570 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002571}
Dave Airlie8be48d92010-03-30 05:34:14 +00002572EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002573
Chris Wilson73943712011-04-22 11:03:57 +01002574/**
2575 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002576 * probing all the outputs attached to the fb
Noralf Trønnesc7779902017-10-30 16:39:37 +01002577 * @fb_helper: driver-allocated fbdev helper, can be NULL
Chris Wilson73943712011-04-22 11:03:57 +01002578 *
Chris Wilson73943712011-04-22 11:03:57 +01002579 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002580 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002581 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002582 * Called at runtime, takes the mode config locks to be able to check/change the
2583 * modeset configuration. Must be run from process context (which usually means
2584 * either the output polling work or a work item launched from the driver's
2585 * hotplug interrupt).
2586 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002587 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002588 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002589 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2590 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002591 *
Chris Wilson73943712011-04-22 11:03:57 +01002592 * RETURNS:
2593 * 0 on success and a non-zero error code otherwise.
2594 */
2595int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002596{
Thierry Redinge9827d82017-07-04 17:18:23 +02002597 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002598
Noralf Trønnesc7779902017-10-30 16:39:37 +01002599 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +02002600 return 0;
2601
Thierry Redinge9827d82017-07-04 17:18:23 +02002602 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002603 if (fb_helper->deferred_setup) {
2604 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
2605 fb_helper->preferred_bpp);
2606 return err;
2607 }
2608
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002609 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002610 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002611 mutex_unlock(&fb_helper->lock);
2612 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002613 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002614
Dave Airlie38651672010-03-30 05:34:13 +00002615 DRM_DEBUG_KMS("\n");
2616
Chris Wilson64e94402016-11-29 12:02:16 +00002617 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
David Lechnerf461bd22017-08-03 11:19:08 -05002618 drm_setup_crtcs_fb(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +02002619 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002620
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002621 drm_fb_helper_set_par(fb_helper->fbdev);
2622
2623 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002624}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002625EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002626
David Rientjes6a108a12011-01-20 14:44:16 -08002627/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002628 * but the module doesn't depend on any fb console symbols. At least
2629 * attempt to load fbcon to avoid leaving the system without a usable console.
2630 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002631int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002632{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002633#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07002634 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06002635 struct module *fbcon;
2636
2637 mutex_lock(&module_mutex);
2638 fbcon = find_module(name);
2639 mutex_unlock(&module_mutex);
2640
2641 if (!fbcon)
2642 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002643#endif
David Fries3ce05162010-12-12 12:39:22 -06002644 return 0;
2645}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002646EXPORT_SYMBOL(drm_fb_helper_modinit);