blob: e3eb3c9a98e36b18c41b5477778135bf2b993531 [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
Hans de Goede8f0cb412017-11-25 20:35:50 +010044#include "drm_crtc_internal.h"
Ville Syrjälä699fbee2016-09-19 16:33:44 +030045#include "drm_crtc_helper_internal.h"
46
Daniel Vetterf64c5572015-08-25 15:45:13 +020047static bool drm_fbdev_emulation = true;
48module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
49MODULE_PARM_DESC(fbdev_emulation,
50 "Enable legacy fbdev emulation [default=true]");
51
Xinliang Liu5f152572017-02-15 17:19:08 +010052static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
53module_param(drm_fbdev_overalloc, int, 0444);
54MODULE_PARM_DESC(drm_fbdev_overalloc,
55 "Overallocation of the fbdev buffer (%) [default="
56 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
57
Dave Airlie785b93e2009-08-28 15:46:53 +100058static LIST_HEAD(kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +000059static DEFINE_MUTEX(kernel_fb_helper_lock);
Dave Airlie785b93e2009-08-28 15:46:53 +100060
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010061/**
62 * DOC: fbdev helpers
63 *
64 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020065 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010066 * helper functions used by many drivers to implement the kernel mode setting
67 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010068 *
Noralf Trønnes95b01372017-12-15 18:51:16 +010069 * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it
70 * down by calling drm_fb_helper_fbdev_teardown().
Daniel Vetter207fd322013-01-20 22:13:14 +010071 *
Noralf Trønnes95b01372017-12-15 18:51:16 +010072 * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use
73 * the setup helper and will need to do the whole four-step setup process with
74 * drm_fb_helper_prepare(), drm_fb_helper_init(),
75 * drm_fb_helper_single_add_all_connectors(), enable hotplugging and
76 * drm_fb_helper_initial_config() to avoid a possible race window.
77 *
78 * At runtime drivers should restore the fbdev console by using
79 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
80 * They should also notify the fb helper code from updates to the output
81 * configuration by using drm_fb_helper_output_poll_changed() as their
82 * &drm_mode_config_funcs.output_poll_changed callback.
83 *
84 * For suspend/resume consider using drm_mode_config_helper_suspend() and
85 * drm_mode_config_helper_resume() which takes care of fbdev as well.
Daniel Vetter207fd322013-01-20 22:13:14 +010086 *
87 * All other functions exported by the fb helper library can be used to
88 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020089 *
90 * It is possible, though perhaps somewhat tricky, to implement race-free
91 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
92 * helper must be called first to initialize the minimum required to make
93 * hotplug detection work. Drivers also need to make sure to properly set up
Daniel Vetter6806cdf2017-01-25 07:26:43 +010094 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
Thierry Reding10a23102014-06-27 17:19:24 +020095 * it is safe to enable interrupts and start processing hotplug events. At the
96 * same time, drivers should initialize all modeset objects such as CRTCs,
97 * encoders and connectors. To finish up the fbdev helper initialization, the
98 * drm_fb_helper_init() function is called. To probe for all attached displays
99 * and set up an initial configuration using the detected hardware, drivers
100 * should call drm_fb_helper_single_add_all_connectors() followed by
101 * drm_fb_helper_initial_config().
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200102 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100103 * If &drm_framebuffer_funcs.dirty is set, the
Noralf Trønnes2dad5512016-05-11 18:09:17 +0200104 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100105 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
Noralf Trønnes2dad5512016-05-11 18:09:17 +0200106 * away. This worker then calls the dirty() function ensuring that it will
107 * always run in process context since the fb_*() function could be running in
108 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
109 * callback it will also schedule dirty_work with the damage collected from the
110 * mmap page writes.
Daniel Vetterd0ddc0332012-11-01 14:45:17 +0100111 */
112
Chris Wilson966a6a12016-11-29 12:02:15 +0000113#define drm_fb_helper_for_each_connector(fbh, i__) \
Daniel Vettere13a0582017-07-05 06:56:29 +0200114 for (({ lockdep_assert_held(&(fbh)->lock); }), \
Chris Wilson966a6a12016-11-29 12:02:15 +0000115 i__ = 0; i__ < (fbh)->connector_count; i__++)
116
Thierry Redingaf2405a2017-07-04 17:18:21 +0200117static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
118 struct drm_connector *connector)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200119{
Thierry Reding50021ff2017-03-29 16:43:53 +0200120 struct drm_fb_helper_connector *fb_conn;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200121 struct drm_fb_helper_connector **temp;
Thierry Reding50021ff2017-03-29 16:43:53 +0200122 unsigned int count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200123
124 if (!drm_fbdev_emulation)
125 return 0;
126
Thierry Redinge9827d82017-07-04 17:18:23 +0200127 lockdep_assert_held(&fb_helper->lock);
Thierry Reding50021ff2017-03-29 16:43:53 +0200128
129 count = fb_helper->connector_count + 1;
130
131 if (count > fb_helper->connector_info_alloc_count) {
132 size_t size = count * sizeof(fb_conn);
133
134 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200135 if (!temp)
136 return -ENOMEM;
137
Thierry Reding50021ff2017-03-29 16:43:53 +0200138 fb_helper->connector_info_alloc_count = count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200139 fb_helper->connector_info = temp;
140 }
141
Thierry Reding50021ff2017-03-29 16:43:53 +0200142 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
143 if (!fb_conn)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200144 return -ENOMEM;
145
146 drm_connector_get(connector);
Thierry Reding50021ff2017-03-29 16:43:53 +0200147 fb_conn->connector = connector;
148 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
Thierry Redingaf2405a2017-07-04 17:18:21 +0200149
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200150 return 0;
151}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200152
153int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
154 struct drm_connector *connector)
155{
156 int err;
157
Noralf Trønnesc7779902017-10-30 16:39:37 +0100158 if (!fb_helper)
159 return 0;
160
Thierry Redinge9827d82017-07-04 17:18:23 +0200161 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200162 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200163 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200164
165 return err;
166}
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200167EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
168
Daniel Vetter207fd322013-01-20 22:13:14 +0100169/**
170 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
171 * emulation helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100172 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
Daniel Vetter207fd322013-01-20 22:13:14 +0100173 *
174 * This functions adds all the available connectors for use with the given
175 * fb_helper. This is a separate step to allow drivers to freely assign
176 * connectors to the fbdev, e.g. if some are reserved for special purposes or
177 * not adequate to be used for the fbcon.
178 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200179 * This function is protected against concurrent connector hotadds/removals
180 * using drm_fb_helper_add_one_connector() and
181 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100182 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000183int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000184{
Gustavo A. R. Silva89f3f352017-12-05 11:46:28 -0600185 struct drm_device *dev;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000186 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100187 struct drm_connector_list_iter conn_iter;
188 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000189
Noralf Trønnesc7779902017-10-30 16:39:37 +0100190 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200191 return 0;
192
Gustavo A. R. Silva89f3f352017-12-05 11:46:28 -0600193 dev = fb_helper->dev;
194
Thierry Redinge9827d82017-07-04 17:18:23 +0200195 mutex_lock(&fb_helper->lock);
Thierry Redingb982dab2017-02-28 15:46:43 +0100196 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100197 drm_for_each_connector_iter(connector, &conn_iter) {
Thierry Redingaf2405a2017-07-04 17:18:21 +0200198 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100199 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000200 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000201 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100202 goto out;
203
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000204fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000205 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300206 struct drm_fb_helper_connector *fb_helper_connector =
207 fb_helper->connector_info[i];
208
Thierry Redingad093602017-02-28 15:46:39 +0100209 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300210
211 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000212 fb_helper->connector_info[i] = NULL;
213 }
214 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100215out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100216 drm_connector_list_iter_end(&conn_iter);
Thierry Redinge9827d82017-07-04 17:18:23 +0200217 mutex_unlock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200218
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100219 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000220}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000221EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000222
Thierry Redingaf2405a2017-07-04 17:18:21 +0200223static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
224 struct drm_connector *connector)
Dave Airlie65c2a892014-06-05 14:01:30 +1000225{
226 struct drm_fb_helper_connector *fb_helper_connector;
227 int i, j;
228
Daniel Vetterf64c5572015-08-25 15:45:13 +0200229 if (!drm_fbdev_emulation)
230 return 0;
231
Thierry Redinge9827d82017-07-04 17:18:23 +0200232 lockdep_assert_held(&fb_helper->lock);
Dave Airlie65c2a892014-06-05 14:01:30 +1000233
Thierry Redinge9827d82017-07-04 17:18:23 +0200234 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie65c2a892014-06-05 14:01:30 +1000235 if (fb_helper->connector_info[i]->connector == connector)
236 break;
237 }
238
239 if (i == fb_helper->connector_count)
240 return -EINVAL;
241 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100242 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000243
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200244 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000245 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200246
Dave Airlie65c2a892014-06-05 14:01:30 +1000247 fb_helper->connector_count--;
248 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500249
Dave Airlie65c2a892014-06-05 14:01:30 +1000250 return 0;
251}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200252
253int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
254 struct drm_connector *connector)
255{
256 int err;
257
Noralf Trønnesc7779902017-10-30 16:39:37 +0100258 if (!fb_helper)
259 return 0;
260
Thierry Redinge9827d82017-07-04 17:18:23 +0200261 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200262 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200263 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200264
265 return err;
266}
Dave Airlie65c2a892014-06-05 14:01:30 +1000267EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
268
Jason Wessel99231022010-10-13 14:09:43 -0500269static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
270{
271 uint16_t *r_base, *g_base, *b_base;
272
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200273 if (crtc->funcs->gamma_set == NULL)
274 return;
275
Jason Wessel99231022010-10-13 14:09:43 -0500276 r_base = crtc->gamma_store;
277 g_base = r_base + crtc->gamma_size;
278 b_base = g_base + crtc->gamma_size;
279
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200280 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
281 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500282}
283
Daniel Vetter207fd322013-01-20 22:13:14 +0100284/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100285 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100286 * @info: fbdev registered by the helper
287 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500288int drm_fb_helper_debug_enter(struct fb_info *info)
289{
290 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200291 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500292 int i;
293
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500294 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
295 for (i = 0; i < helper->crtc_count; i++) {
296 struct drm_mode_set *mode_set =
297 &helper->crtc_info[i].mode_set;
298
299 if (!mode_set->crtc->enabled)
300 continue;
301
302 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100303 if (funcs->mode_set_base_atomic == NULL)
304 continue;
305
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200306 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
307 continue;
308
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500309 funcs->mode_set_base_atomic(mode_set->crtc,
310 mode_set->fb,
311 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500312 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500313 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500314 }
315 }
316
317 return 0;
318}
319EXPORT_SYMBOL(drm_fb_helper_debug_enter);
320
Daniel Vetter207fd322013-01-20 22:13:14 +0100321/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100322 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100323 * @info: fbdev registered by the helper
324 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500325int drm_fb_helper_debug_leave(struct fb_info *info)
326{
327 struct drm_fb_helper *helper = info->par;
328 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200329 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500330 struct drm_framebuffer *fb;
331 int i;
332
333 for (i = 0; i < helper->crtc_count; i++) {
334 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200335
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500336 crtc = mode_set->crtc;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200337 if (drm_drv_uses_atomic_modeset(crtc->dev))
338 continue;
339
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500340 funcs = crtc->helper_private;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200341 fb = crtc->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500342
343 if (!crtc->enabled)
344 continue;
345
346 if (!fb) {
347 DRM_ERROR("no fb to restore??\n");
348 continue;
349 }
350
Stefan Christ1b99b722016-11-14 00:03:11 +0100351 if (funcs->mode_set_base_atomic == NULL)
352 continue;
353
Jason Wessel99231022010-10-13 14:09:43 -0500354 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500355 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500356 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500357 }
358
359 return 0;
360}
361EXPORT_SYMBOL(drm_fb_helper_debug_leave);
362
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200363static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
Rob Clarkbbb1e522015-08-25 15:35:58 -0400364{
365 struct drm_device *dev = fb_helper->dev;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100366 struct drm_plane_state *plane_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400367 struct drm_plane *plane;
368 struct drm_atomic_state *state;
369 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200370 unsigned int plane_mask;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200371 struct drm_modeset_acquire_ctx ctx;
372
373 drm_modeset_acquire_init(&ctx, 0);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400374
375 state = drm_atomic_state_alloc(dev);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200376 if (!state) {
377 ret = -ENOMEM;
378 goto out_ctx;
379 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400380
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200381 state->acquire_ctx = &ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400382retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100383 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400384 drm_for_each_plane(plane, dev) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400385 plane_state = drm_atomic_get_plane_state(state, plane);
386 if (IS_ERR(plane_state)) {
387 ret = PTR_ERR(plane_state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200388 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400389 }
390
Robert Fossc2c446a2017-05-19 16:50:17 -0400391 plane_state->rotation = DRM_MODE_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400392
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100393 plane->old_fb = plane->fb;
394 plane_mask |= 1 << drm_plane_index(plane);
395
Rob Clarkbbb1e522015-08-25 15:35:58 -0400396 /* disable non-primary: */
397 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
398 continue;
399
400 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
401 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200402 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400403 }
404
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200405 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400406 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100407 struct drm_plane *primary = mode_set->crtc->primary;
408
409 /* Cannot fail as we've already gotten the plane state above */
410 plane_state = drm_atomic_get_new_plane_state(state, primary);
411 plane_state->rotation = fb_helper->crtc_info[i].rotation;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400412
413 ret = __drm_atomic_helper_set_config(mode_set, state);
414 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200415 goto out_state;
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200416
417 /*
418 * __drm_atomic_helper_set_config() sets active when a
419 * mode is set, unconditionally clear it if we force DPMS off
420 */
421 if (!active) {
422 struct drm_crtc *crtc = mode_set->crtc;
423 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
424
425 crtc_state->active = false;
426 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400427 }
428
429 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400430
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200431out_state:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100432 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700433
Rob Clarkbbb1e522015-08-25 15:35:58 -0400434 if (ret == -EDEADLK)
435 goto backoff;
436
Chris Wilson08536952016-10-14 13:18:18 +0100437 drm_atomic_state_put(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200438out_ctx:
439 drm_modeset_drop_locks(&ctx);
440 drm_modeset_acquire_fini(&ctx);
441
Rob Clarkbbb1e522015-08-25 15:35:58 -0400442 return ret;
443
444backoff:
445 drm_atomic_state_clear(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200446 drm_modeset_backoff(&ctx);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400447
448 goto retry;
449}
450
Daniel Vetter71286452017-04-03 10:33:04 +0200451static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100452{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300453 struct drm_device *dev = fb_helper->dev;
454 struct drm_plane *plane;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200455 int i, ret = 0;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100456
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200457 drm_modeset_lock_all(fb_helper->dev);
Daniel Vetter6295d602015-07-09 23:44:25 +0200458 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700459 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
460 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100461
Ville Syrjälä6686df82016-10-21 22:22:45 +0300462 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300463 drm_mode_plane_set_obj_prop(plane,
464 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400465 DRM_MODE_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530466 }
467
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100468 for (i = 0; i < fb_helper->crtc_count; i++) {
469 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300470 struct drm_crtc *crtc = mode_set->crtc;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300471
Alex Deucher03f9abb2015-09-30 14:47:37 -0400472 if (crtc->funcs->cursor_set2) {
473 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
474 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200475 goto out;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400476 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300477 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
478 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200479 goto out;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300480 }
481
Daniel Vetter2d13b672012-12-11 13:47:23 +0100482 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100483 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200484 goto out;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100485 }
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200486out:
487 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200488
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200489 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100490}
Rob Clark5ea1f752014-05-30 12:29:48 -0400491
Daniel Vetter71286452017-04-03 10:33:04 +0200492static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
493{
494 struct drm_device *dev = fb_helper->dev;
495
Daniel Vetter71286452017-04-03 10:33:04 +0200496 if (drm_drv_uses_atomic_modeset(dev))
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200497 return restore_fbdev_mode_atomic(fb_helper, true);
Daniel Vetter71286452017-04-03 10:33:04 +0200498 else
499 return restore_fbdev_mode_legacy(fb_helper);
500}
501
Rob Clark5ea1f752014-05-30 12:29:48 -0400502/**
503 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
Noralf Trønnesc7779902017-10-30 16:39:37 +0100504 * @fb_helper: driver-allocated fbdev helper, can be NULL
Rob Clark5ea1f752014-05-30 12:29:48 -0400505 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100506 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400507 * when implementing an fbcon on top of kms using this helper. This ensures that
508 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200509 *
510 * RETURNS:
511 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400512 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200513int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400514{
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200515 bool do_delayed;
516 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000517
Noralf Trønnesc7779902017-10-30 16:39:37 +0100518 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200519 return -ENODEV;
520
Daniel Vetterca91a272017-07-06 15:00:21 +0200521 if (READ_ONCE(fb_helper->deferred_setup))
522 return 0;
523
Thierry Redinge9827d82017-07-04 17:18:23 +0200524 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400525 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000526
527 do_delayed = fb_helper->delayed_hotplug;
528 if (do_delayed)
529 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200530 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000531
532 if (do_delayed)
533 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200534
Rob Clark5ea1f752014-05-30 12:29:48 -0400535 return ret;
536}
537EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100538
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200539static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
540{
541 struct drm_device *dev = fb_helper->dev;
542 struct drm_crtc *crtc;
543 int bound = 0, crtcs_bound = 0;
544
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200545 /*
546 * Sometimes user space wants everything disabled, so don't steal the
547 * display if there's a master.
548 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200549 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200550 return false;
551
552 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200553 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200554 if (crtc->primary->fb)
555 crtcs_bound++;
556 if (crtc->primary->fb == fb_helper->fb)
557 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200558 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200559 }
560
561 if (bound < crtcs_bound)
562 return false;
563
564 return true;
565}
566
567#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100568/*
569 * restore fbcon display for all kms driver's using this helper, used for sysrq
570 * and panic handling.
571 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530572static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000573{
Dave Airlie785b93e2009-08-28 15:46:53 +1000574 bool ret, error = false;
575 struct drm_fb_helper *helper;
576
577 if (list_empty(&kernel_fb_helper_list))
578 return false;
579
580 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200581 struct drm_device *dev = helper->dev;
582
583 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100584 continue;
585
Thierry Redinge9827d82017-07-04 17:18:23 +0200586 mutex_lock(&helper->lock);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200587 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100588 if (ret)
589 error = true;
Thierry Redinge9827d82017-07-04 17:18:23 +0200590 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000591 }
592 return error;
593}
594
Dave Airlie785b93e2009-08-28 15:46:53 +1000595static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
596{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100597 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200598
Daniel Vetterd21bf462013-01-20 18:09:52 +0100599 ret = drm_fb_helper_force_kernel_mode();
600 if (ret == true)
601 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000602}
603static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
604
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700605static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000606{
607 schedule_work(&drm_fb_helper_restore_work);
608}
609
610static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
611 .handler = drm_fb_helper_sysrq,
612 .help_msg = "force-fb(V)",
613 .action_msg = "Restore framebuffer console",
614};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000615#else
616static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200617#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000618
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200619static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000620{
Dave Airlie785b93e2009-08-28 15:46:53 +1000621 struct drm_device *dev = fb_helper->dev;
622 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700623 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700624 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000625
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200626 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700627 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000628 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000629
Dave Airlie8be48d92010-03-30 05:34:14 +0000630 if (!crtc->enabled)
631 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000632
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100633 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000634 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700635 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200636 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500637 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100638 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700639 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000640 }
Daniel Vetter84849902012-12-02 00:28:11 +0100641 drm_modeset_unlock_all(dev);
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200642}
643
644static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
645{
646 struct drm_fb_helper *fb_helper = info->par;
647
648 /*
649 * For each CRTC in this fb, turn the connectors on/off.
650 */
651 mutex_lock(&fb_helper->lock);
652 if (!drm_fb_helper_is_bound(fb_helper)) {
653 mutex_unlock(&fb_helper->lock);
654 return;
655 }
656
657 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
658 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
659 else
660 dpms_legacy(fb_helper, dpms_mode);
Thierry Redinge9827d82017-07-04 17:18:23 +0200661 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000662}
663
Daniel Vetter207fd322013-01-20 22:13:14 +0100664/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100665 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100666 * @blank: desired blanking state
667 * @info: fbdev registered by the helper
668 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000669int drm_fb_helper_blank(int blank, struct fb_info *info)
670{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200671 if (oops_in_progress)
672 return -EBUSY;
673
Dave Airlie785b93e2009-08-28 15:46:53 +1000674 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000675 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000676 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100677 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000678 break;
James Simmons731b5a12009-10-29 20:39:07 +0000679 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000680 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100681 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000682 break;
James Simmons731b5a12009-10-29 20:39:07 +0000683 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000684 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100685 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000686 break;
James Simmons731b5a12009-10-29 20:39:07 +0000687 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000688 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100689 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000690 break;
James Simmons731b5a12009-10-29 20:39:07 +0000691 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000692 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100693 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000694 break;
695 }
696 return 0;
697}
698EXPORT_SYMBOL(drm_fb_helper_blank);
699
Ville Syrjäläa2889602016-10-26 17:41:18 +0300700static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
701 struct drm_mode_set *modeset)
702{
703 int i;
704
705 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100706 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300707 modeset->connectors[i] = NULL;
708 }
709 modeset->num_connectors = 0;
710
711 drm_mode_destroy(helper->dev, modeset->mode);
712 modeset->mode = NULL;
713
714 /* FIXME should hold a ref? */
715 modeset->fb = NULL;
716}
717
Dave Airlie785b93e2009-08-28 15:46:53 +1000718static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
719{
720 int i;
721
Dave Airlie6e86d582016-04-27 11:24:51 +1000722 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100723 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000724 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000725 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000726 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300727
Sascha Hauera1b77362012-02-01 11:38:22 +0100728 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300729 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
730
731 drm_fb_helper_modeset_release(helper, modeset);
732 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100733 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000734 kfree(helper->crtc_info);
735}
736
Noralf Trønnescfe63422016-08-23 13:54:06 +0200737static void drm_fb_helper_resume_worker(struct work_struct *work)
738{
739 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
740 resume_work);
741
742 console_lock();
743 fb_set_suspend(helper->fbdev, 0);
744 console_unlock();
745}
746
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200747static void drm_fb_helper_dirty_work(struct work_struct *work)
748{
749 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
750 dirty_work);
751 struct drm_clip_rect *clip = &helper->dirty_clip;
752 struct drm_clip_rect clip_copy;
753 unsigned long flags;
754
755 spin_lock_irqsave(&helper->dirty_lock, flags);
756 clip_copy = *clip;
757 clip->x1 = clip->y1 = ~0;
758 clip->x2 = clip->y2 = 0;
759 spin_unlock_irqrestore(&helper->dirty_lock, flags);
760
Takashi Iwai87d3b652016-10-20 17:05:30 +0200761 /* call dirty callback only when it has been really touched */
762 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
763 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200764}
765
Daniel Vetter207fd322013-01-20 22:13:14 +0100766/**
Thierry Reding10a23102014-06-27 17:19:24 +0200767 * drm_fb_helper_prepare - setup a drm_fb_helper structure
768 * @dev: DRM device
769 * @helper: driver-allocated fbdev helper structure to set up
770 * @funcs: pointer to structure of functions associate with this helper
771 *
772 * Sets up the bare minimum to make the framebuffer helper usable. This is
773 * useful to implement race-free initialization of the polling helpers.
774 */
775void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
776 const struct drm_fb_helper_funcs *funcs)
777{
778 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200779 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200780 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200781 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
782 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200783 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200784 helper->funcs = funcs;
785 helper->dev = dev;
786}
787EXPORT_SYMBOL(drm_fb_helper_prepare);
788
789/**
Daniel Vettered84e252017-02-07 15:10:49 +0100790 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100791 * @dev: drm device
792 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100793 * @max_conn_count: max connector count
794 *
795 * This allocates the structures for the fbdev helper with the given limits.
796 * Note that this won't yet touch the hardware (through the driver interfaces)
797 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
798 * to allow driver writes more control over the exact init sequence.
799 *
Thierry Reding10a23102014-06-27 17:19:24 +0200800 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100801 *
802 * RETURNS:
803 * Zero if everything went ok, nonzero otherwise.
804 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000805int drm_fb_helper_init(struct drm_device *dev,
806 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200807 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000808{
Dave Airlie785b93e2009-08-28 15:46:53 +1000809 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200810 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000811 int i;
812
Noralf Trønnes29ad20b2017-10-30 16:39:38 +0100813 if (!drm_fbdev_emulation) {
814 dev->fb_helper = fb_helper;
Daniel Vetterf64c5572015-08-25 15:45:13 +0200815 return 0;
Noralf Trønnes29ad20b2017-10-30 16:39:38 +0100816 }
Daniel Vetterf64c5572015-08-25 15:45:13 +0200817
Xiubo Li04cfe972014-03-10 09:33:58 +0800818 if (!max_conn_count)
819 return -EINVAL;
820
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200821 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000822 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000823 return -ENOMEM;
824
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200825 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000826 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
827 if (!fb_helper->connector_info) {
828 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000829 return -ENOMEM;
830 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000831 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000832 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000833
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200834 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000835 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000836 kcalloc(max_conn_count,
837 sizeof(struct drm_connector *),
838 GFP_KERNEL);
839
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200840 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000841 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000842 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100843 fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000844 }
845
846 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200847 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000848 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000849 i++;
850 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100851
Noralf Trønnes29ad20b2017-10-30 16:39:38 +0100852 dev->fb_helper = fb_helper;
853
Dave Airlie785b93e2009-08-28 15:46:53 +1000854 return 0;
855out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000856 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000857 return -ENOMEM;
858}
Dave Airlie4abe3522010-03-30 05:34:18 +0000859EXPORT_SYMBOL(drm_fb_helper_init);
860
Archit Tanejab8017d62015-07-22 14:57:56 +0530861/**
862 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
863 * @fb_helper: driver-allocated fbdev helper
864 *
865 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100866 * by the driver within the fb_probe fb_helper callback function. Drivers do not
867 * need to release the allocated fb_info structure themselves, this is
868 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530869 *
870 * RETURNS:
871 * fb_info pointer if things went okay, pointer containing error code
872 * otherwise
873 */
874struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
875{
876 struct device *dev = fb_helper->dev->dev;
877 struct fb_info *info;
878 int ret;
879
880 info = framebuffer_alloc(0, dev);
881 if (!info)
882 return ERR_PTR(-ENOMEM);
883
884 ret = fb_alloc_cmap(&info->cmap, 256, 0);
885 if (ret)
886 goto err_release;
887
888 info->apertures = alloc_apertures(1);
889 if (!info->apertures) {
890 ret = -ENOMEM;
891 goto err_free_cmap;
892 }
893
894 fb_helper->fbdev = info;
895
896 return info;
897
898err_free_cmap:
899 fb_dealloc_cmap(&info->cmap);
900err_release:
901 framebuffer_release(info);
902 return ERR_PTR(ret);
903}
904EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
905
906/**
907 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
Noralf Trønnesc7779902017-10-30 16:39:37 +0100908 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Tanejab8017d62015-07-22 14:57:56 +0530909 *
910 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100911 * framebuffer device. This must be called before releasing all resources for
912 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530913 */
914void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
915{
916 if (fb_helper && fb_helper->fbdev)
917 unregister_framebuffer(fb_helper->fbdev);
918}
919EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
920
921/**
Daniel Vettered84e252017-02-07 15:10:49 +0100922 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100923 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vettered84e252017-02-07 15:10:49 +0100924 *
925 * This cleans up all remaining resources associated with @fb_helper. Must be
926 * called after drm_fb_helper_unlink_fbi() was called.
927 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000928void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
929{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100930 struct fb_info *info;
931
Noralf Trønnes29ad20b2017-10-30 16:39:38 +0100932 if (!fb_helper)
933 return;
934
935 fb_helper->dev->fb_helper = NULL;
936
937 if (!drm_fbdev_emulation)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200938 return;
939
Noralf Trønnesb52f09c2017-08-28 19:17:43 +0200940 cancel_work_sync(&fb_helper->resume_work);
941 cancel_work_sync(&fb_helper->dirty_work);
942
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100943 info = fb_helper->fbdev;
944 if (info) {
945 if (info->cmap.len)
946 fb_dealloc_cmap(&info->cmap);
947 framebuffer_release(info);
948 }
949 fb_helper->fbdev = NULL;
950
Chris Wilsona53ca632016-11-29 12:02:17 +0000951 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000952 if (!list_empty(&fb_helper->kernel_fb_list)) {
953 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200954 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000955 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000956 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000957 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000958
Thierry Redinge9827d82017-07-04 17:18:23 +0200959 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000960 drm_fb_helper_crtc_free(fb_helper);
961
Dave Airlie4abe3522010-03-30 05:34:18 +0000962}
963EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000964
Archit Taneja47074ab2015-07-22 14:57:57 +0530965/**
966 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
Noralf Trønnesc7779902017-10-30 16:39:37 +0100967 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Taneja47074ab2015-07-22 14:57:57 +0530968 *
969 * A wrapper around unlink_framebuffer implemented by fbdev core
970 */
971void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
972{
973 if (fb_helper && fb_helper->fbdev)
974 unlink_framebuffer(fb_helper->fbdev);
975}
976EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
977
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200978static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
979 u32 width, u32 height)
980{
981 struct drm_fb_helper *helper = info->par;
982 struct drm_clip_rect *clip = &helper->dirty_clip;
983 unsigned long flags;
984
985 if (!helper->fb->funcs->dirty)
986 return;
987
988 spin_lock_irqsave(&helper->dirty_lock, flags);
989 clip->x1 = min_t(u32, clip->x1, x);
990 clip->y1 = min_t(u32, clip->y1, y);
991 clip->x2 = max_t(u32, clip->x2, x + width);
992 clip->y2 = max_t(u32, clip->y2, y + height);
993 spin_unlock_irqrestore(&helper->dirty_lock, flags);
994
995 schedule_work(&helper->dirty_work);
996}
997
998/**
999 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
1000 * @info: fb_info struct pointer
1001 * @pagelist: list of dirty mmap framebuffer pages
1002 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001003 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001004 * callback function for flushing the fbdev mmap writes.
1005 */
1006void drm_fb_helper_deferred_io(struct fb_info *info,
1007 struct list_head *pagelist)
1008{
1009 unsigned long start, end, min, max;
1010 struct page *page;
1011 u32 y1, y2;
1012
1013 min = ULONG_MAX;
1014 max = 0;
1015 list_for_each_entry(page, pagelist, lru) {
1016 start = page->index << PAGE_SHIFT;
1017 end = start + PAGE_SIZE - 1;
1018 min = min(min, start);
1019 max = max(max, end);
1020 }
1021
1022 if (min < max) {
1023 y1 = min / info->fix.line_length;
1024 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1025 info->var.yres);
1026 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1027 }
1028}
1029EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1030
Archit Tanejacbb1a822015-07-31 16:21:41 +05301031/**
1032 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1033 * @info: fb_info struct pointer
1034 * @buf: userspace buffer to read from framebuffer memory
1035 * @count: number of bytes to read from framebuffer memory
1036 * @ppos: read offset within framebuffer memory
1037 *
1038 * A wrapper around fb_sys_read implemented by fbdev core
1039 */
1040ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1041 size_t count, loff_t *ppos)
1042{
1043 return fb_sys_read(info, buf, count, ppos);
1044}
1045EXPORT_SYMBOL(drm_fb_helper_sys_read);
1046
1047/**
1048 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1049 * @info: fb_info struct pointer
1050 * @buf: userspace buffer to write to framebuffer memory
1051 * @count: number of bytes to write to framebuffer memory
1052 * @ppos: write offset within framebuffer memory
1053 *
1054 * A wrapper around fb_sys_write implemented by fbdev core
1055 */
1056ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1057 size_t count, loff_t *ppos)
1058{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001059 ssize_t ret;
1060
1061 ret = fb_sys_write(info, buf, count, ppos);
1062 if (ret > 0)
1063 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1064 info->var.yres);
1065
1066 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301067}
1068EXPORT_SYMBOL(drm_fb_helper_sys_write);
1069
Archit Taneja742547b2015-07-31 16:21:42 +05301070/**
1071 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1072 * @info: fbdev registered by the helper
1073 * @rect: info about rectangle to fill
1074 *
1075 * A wrapper around sys_fillrect implemented by fbdev core
1076 */
1077void drm_fb_helper_sys_fillrect(struct fb_info *info,
1078 const struct fb_fillrect *rect)
1079{
1080 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001081 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1082 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301083}
1084EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1085
1086/**
1087 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1088 * @info: fbdev registered by the helper
1089 * @area: info about area to copy
1090 *
1091 * A wrapper around sys_copyarea implemented by fbdev core
1092 */
1093void drm_fb_helper_sys_copyarea(struct fb_info *info,
1094 const struct fb_copyarea *area)
1095{
1096 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001097 drm_fb_helper_dirty(info, area->dx, area->dy,
1098 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301099}
1100EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1101
1102/**
1103 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1104 * @info: fbdev registered by the helper
1105 * @image: info about image to blit
1106 *
1107 * A wrapper around sys_imageblit implemented by fbdev core
1108 */
1109void drm_fb_helper_sys_imageblit(struct fb_info *info,
1110 const struct fb_image *image)
1111{
1112 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001113 drm_fb_helper_dirty(info, image->dx, image->dy,
1114 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301115}
1116EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1117
1118/**
1119 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1120 * @info: fbdev registered by the helper
1121 * @rect: info about rectangle to fill
1122 *
1123 * A wrapper around cfb_imageblit implemented by fbdev core
1124 */
1125void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1126 const struct fb_fillrect *rect)
1127{
1128 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001129 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1130 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301131}
1132EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1133
1134/**
1135 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1136 * @info: fbdev registered by the helper
1137 * @area: info about area to copy
1138 *
1139 * A wrapper around cfb_copyarea implemented by fbdev core
1140 */
1141void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1142 const struct fb_copyarea *area)
1143{
1144 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001145 drm_fb_helper_dirty(info, area->dx, area->dy,
1146 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301147}
1148EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1149
1150/**
1151 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1152 * @info: fbdev registered by the helper
1153 * @image: info about image to blit
1154 *
1155 * A wrapper around cfb_imageblit implemented by fbdev core
1156 */
1157void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1158 const struct fb_image *image)
1159{
1160 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001161 drm_fb_helper_dirty(info, image->dx, image->dy,
1162 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301163}
1164EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1165
Archit Tanejafdefa582015-07-31 16:21:43 +05301166/**
1167 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
Noralf Trønnesc7779902017-10-30 16:39:37 +01001168 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001169 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301170 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001171 * A wrapper around fb_set_suspend implemented by fbdev core.
1172 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1173 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301174 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001175void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301176{
1177 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001178 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301179}
1180EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1181
Noralf Trønnescfe63422016-08-23 13:54:06 +02001182/**
1183 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1184 * takes the console lock
Noralf Trønnesc7779902017-10-30 16:39:37 +01001185 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001186 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001187 *
1188 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1189 * isn't available on resume, a worker is tasked with waiting for the lock
1190 * to become available. The console lock can be pretty contented on resume
1191 * due to all the printk activity.
1192 *
1193 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001194 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001195 *
1196 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1197 */
1198void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001199 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001200{
1201 if (!fb_helper || !fb_helper->fbdev)
1202 return;
1203
1204 /* make sure there's no pending/ongoing resume */
1205 flush_work(&fb_helper->resume_work);
1206
1207 if (suspend) {
1208 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1209 return;
1210
1211 console_lock();
1212
1213 } else {
1214 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1215 return;
1216
1217 if (!console_trylock()) {
1218 schedule_work(&fb_helper->resume_work);
1219 return;
1220 }
1221 }
1222
1223 fb_set_suspend(fb_helper->fbdev, suspend);
1224 console_unlock();
1225}
1226EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1227
Peter Rosinb8e2b012017-07-04 12:36:57 +02001228static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1229{
1230 u32 *palette = (u32 *)info->pseudo_palette;
1231 int i;
1232
1233 if (cmap->start + cmap->len > 16)
1234 return -EINVAL;
1235
1236 for (i = 0; i < cmap->len; ++i) {
1237 u16 red = cmap->red[i];
1238 u16 green = cmap->green[i];
1239 u16 blue = cmap->blue[i];
1240 u32 value;
1241
1242 red >>= 16 - info->var.red.length;
1243 green >>= 16 - info->var.green.length;
1244 blue >>= 16 - info->var.blue.length;
1245 value = (red << info->var.red.offset) |
1246 (green << info->var.green.offset) |
1247 (blue << info->var.blue.offset);
1248 if (info->var.transp.length > 0) {
1249 u32 mask = (1 << info->var.transp.length) - 1;
1250
1251 mask <<= info->var.transp.offset;
1252 value |= mask;
1253 }
1254 palette[cmap->start + i] = value;
1255 }
1256
1257 return 0;
1258}
1259
Peter Rosin964c6002017-07-13 18:25:27 +02001260static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
Dave Airlie068143d2009-10-05 09:58:02 +10001261{
1262 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie068143d2009-10-05 09:58:02 +10001263 struct drm_crtc *crtc;
Peter Rosina3562a02017-07-04 12:36:58 +02001264 u16 *r, *g, *b;
Peter Rosin964c6002017-07-13 18:25:27 +02001265 int i, ret = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001266
Peter Rosin964c6002017-07-13 18:25:27 +02001267 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001268 for (i = 0; i < fb_helper->crtc_count; i++) {
1269 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Peter Rosin964c6002017-07-13 18:25:27 +02001270 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1271 return -EINVAL;
Dave Airlie068143d2009-10-05 09:58:02 +10001272
Peter Rosin964c6002017-07-13 18:25:27 +02001273 if (cmap->start + cmap->len > crtc->gamma_size)
1274 return -EINVAL;
Peter Rosina3562a02017-07-04 12:36:58 +02001275
1276 r = crtc->gamma_store;
1277 g = r + crtc->gamma_size;
1278 b = g + crtc->gamma_size;
1279
1280 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1281 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1282 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1283
Peter Rosin964c6002017-07-13 18:25:27 +02001284 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1285 crtc->gamma_size, NULL);
1286 if (ret)
1287 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001288 }
Peter Rosin964c6002017-07-13 18:25:27 +02001289 drm_modeset_unlock_all(fb_helper->dev);
1290
1291 return ret;
1292}
1293
1294static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1295 struct fb_cmap *cmap)
1296{
1297 struct drm_device *dev = crtc->dev;
1298 struct drm_property_blob *gamma_lut;
1299 struct drm_color_lut *lut;
1300 int size = crtc->gamma_size;
1301 int i;
1302
1303 if (!size || cmap->start + cmap->len > size)
1304 return ERR_PTR(-EINVAL);
1305
1306 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1307 if (IS_ERR(gamma_lut))
1308 return gamma_lut;
1309
1310 lut = (struct drm_color_lut *)gamma_lut->data;
1311 if (cmap->start || cmap->len != size) {
1312 u16 *r = crtc->gamma_store;
1313 u16 *g = r + crtc->gamma_size;
1314 u16 *b = g + crtc->gamma_size;
1315
1316 for (i = 0; i < cmap->start; i++) {
1317 lut[i].red = r[i];
1318 lut[i].green = g[i];
1319 lut[i].blue = b[i];
1320 }
1321 for (i = cmap->start + cmap->len; i < size; i++) {
1322 lut[i].red = r[i];
1323 lut[i].green = g[i];
1324 lut[i].blue = b[i];
1325 }
1326 }
1327
1328 for (i = 0; i < cmap->len; i++) {
1329 lut[cmap->start + i].red = cmap->red[i];
1330 lut[cmap->start + i].green = cmap->green[i];
1331 lut[cmap->start + i].blue = cmap->blue[i];
1332 }
1333
1334 return gamma_lut;
1335}
1336
1337static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1338{
1339 struct drm_fb_helper *fb_helper = info->par;
1340 struct drm_device *dev = fb_helper->dev;
1341 struct drm_property_blob *gamma_lut = NULL;
1342 struct drm_modeset_acquire_ctx ctx;
1343 struct drm_crtc_state *crtc_state;
1344 struct drm_atomic_state *state;
1345 struct drm_crtc *crtc;
1346 u16 *r, *g, *b;
1347 int i, ret = 0;
1348 bool replaced;
1349
1350 drm_modeset_acquire_init(&ctx, 0);
1351
1352 state = drm_atomic_state_alloc(dev);
1353 if (!state) {
1354 ret = -ENOMEM;
1355 goto out_ctx;
1356 }
1357
1358 state->acquire_ctx = &ctx;
1359retry:
1360 for (i = 0; i < fb_helper->crtc_count; i++) {
1361 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1362
1363 if (!gamma_lut)
1364 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1365 if (IS_ERR(gamma_lut)) {
1366 ret = PTR_ERR(gamma_lut);
1367 gamma_lut = NULL;
1368 goto out_state;
1369 }
1370
1371 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1372 if (IS_ERR(crtc_state)) {
1373 ret = PTR_ERR(crtc_state);
1374 goto out_state;
1375 }
1376
1377 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1378 NULL);
1379 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1380 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1381 gamma_lut);
1382 crtc_state->color_mgmt_changed |= replaced;
1383 }
1384
1385 ret = drm_atomic_commit(state);
1386 if (ret)
1387 goto out_state;
1388
1389 for (i = 0; i < fb_helper->crtc_count; i++) {
1390 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1391
1392 r = crtc->gamma_store;
1393 g = r + crtc->gamma_size;
1394 b = g + crtc->gamma_size;
1395
1396 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1397 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1398 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1399 }
1400
1401out_state:
1402 if (ret == -EDEADLK)
1403 goto backoff;
1404
1405 drm_property_blob_put(gamma_lut);
1406 drm_atomic_state_put(state);
1407out_ctx:
1408 drm_modeset_drop_locks(&ctx);
1409 drm_modeset_acquire_fini(&ctx);
1410
1411 return ret;
1412
1413backoff:
1414 drm_atomic_state_clear(state);
1415 drm_modeset_backoff(&ctx);
1416 goto retry;
1417}
1418
1419/**
1420 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1421 * @cmap: cmap to set
1422 * @info: fbdev registered by the helper
1423 */
1424int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1425{
1426 struct drm_fb_helper *fb_helper = info->par;
1427 int ret;
1428
1429 if (oops_in_progress)
1430 return -EBUSY;
1431
1432 mutex_lock(&fb_helper->lock);
1433
1434 if (!drm_fb_helper_is_bound(fb_helper)) {
1435 ret = -EBUSY;
1436 goto out;
1437 }
1438
1439 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1440 ret = setcmap_pseudo_palette(cmap, info);
1441 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1442 ret = setcmap_atomic(cmap, info);
1443 else
1444 ret = setcmap_legacy(cmap, info);
1445
1446out:
Thierry Redinge9827d82017-07-04 17:18:23 +02001447 mutex_unlock(&fb_helper->lock);
Peter Rosin964c6002017-07-13 18:25:27 +02001448
1449 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001450}
1451EXPORT_SYMBOL(drm_fb_helper_setcmap);
1452
Daniel Vetter207fd322013-01-20 22:13:14 +01001453/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001454 * drm_fb_helper_ioctl - legacy ioctl implementation
1455 * @info: fbdev registered by the helper
1456 * @cmd: ioctl command
1457 * @arg: ioctl argument
1458 *
1459 * A helper to implement the standard fbdev ioctl. Only
1460 * FBIO_WAITFORVSYNC is implemented for now.
1461 */
1462int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1463 unsigned long arg)
1464{
1465 struct drm_fb_helper *fb_helper = info->par;
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001466 struct drm_mode_set *mode_set;
1467 struct drm_crtc *crtc;
1468 int ret = 0;
1469
Thierry Redinge9827d82017-07-04 17:18:23 +02001470 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001471 if (!drm_fb_helper_is_bound(fb_helper)) {
1472 ret = -EBUSY;
1473 goto unlock;
1474 }
1475
1476 switch (cmd) {
1477 case FBIO_WAITFORVSYNC:
1478 /*
1479 * Only consider the first CRTC.
1480 *
1481 * This ioctl is supposed to take the CRTC number as
1482 * an argument, but in fbdev times, what that number
1483 * was supposed to be was quite unclear, different
1484 * drivers were passing that argument differently
1485 * (some by reference, some by value), and most of the
1486 * userspace applications were just hardcoding 0 as an
1487 * argument.
1488 *
1489 * The first CRTC should be the integrated panel on
1490 * most drivers, so this is the best choice we can
1491 * make. If we're not smart enough here, one should
1492 * just consider switch the userspace to KMS.
1493 */
1494 mode_set = &fb_helper->crtc_info[0].mode_set;
1495 crtc = mode_set->crtc;
1496
1497 /*
1498 * Only wait for a vblank event if the CRTC is
1499 * enabled, otherwise just don't do anythintg,
1500 * not even report an error.
1501 */
1502 ret = drm_crtc_vblank_get(crtc);
1503 if (!ret) {
1504 drm_crtc_wait_one_vblank(crtc);
1505 drm_crtc_vblank_put(crtc);
1506 }
1507
1508 ret = 0;
1509 goto unlock;
1510 default:
1511 ret = -ENOTTY;
1512 }
1513
1514unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001515 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001516 return ret;
1517}
1518EXPORT_SYMBOL(drm_fb_helper_ioctl);
1519
1520/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001521 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001522 * @var: screeninfo to check
1523 * @info: fbdev registered by the helper
1524 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001525int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1526 struct fb_info *info)
1527{
1528 struct drm_fb_helper *fb_helper = info->par;
1529 struct drm_framebuffer *fb = fb_helper->fb;
1530 int depth;
1531
Jason Wesself90ebd92010-08-05 09:22:32 -05001532 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001533 return -EINVAL;
1534
Stefan Agner865afb12016-10-11 16:15:04 -07001535 /*
1536 * Changes struct fb_var_screeninfo are currently not pushed back
1537 * to KMS, hence fail if different settings are requested.
1538 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001539 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001540 var->xres > fb->width || var->yres > fb->height ||
1541 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1542 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001543 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1544 var->xres, var->yres, var->bits_per_pixel,
1545 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001546 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001547 return -EINVAL;
1548 }
1549
1550 switch (var->bits_per_pixel) {
1551 case 16:
1552 depth = (var->green.length == 6) ? 16 : 15;
1553 break;
1554 case 32:
1555 depth = (var->transp.length > 0) ? 32 : 24;
1556 break;
1557 default:
1558 depth = var->bits_per_pixel;
1559 break;
1560 }
1561
1562 switch (depth) {
1563 case 8:
1564 var->red.offset = 0;
1565 var->green.offset = 0;
1566 var->blue.offset = 0;
1567 var->red.length = 8;
1568 var->green.length = 8;
1569 var->blue.length = 8;
1570 var->transp.length = 0;
1571 var->transp.offset = 0;
1572 break;
1573 case 15:
1574 var->red.offset = 10;
1575 var->green.offset = 5;
1576 var->blue.offset = 0;
1577 var->red.length = 5;
1578 var->green.length = 5;
1579 var->blue.length = 5;
1580 var->transp.length = 1;
1581 var->transp.offset = 15;
1582 break;
1583 case 16:
1584 var->red.offset = 11;
1585 var->green.offset = 5;
1586 var->blue.offset = 0;
1587 var->red.length = 5;
1588 var->green.length = 6;
1589 var->blue.length = 5;
1590 var->transp.length = 0;
1591 var->transp.offset = 0;
1592 break;
1593 case 24:
1594 var->red.offset = 16;
1595 var->green.offset = 8;
1596 var->blue.offset = 0;
1597 var->red.length = 8;
1598 var->green.length = 8;
1599 var->blue.length = 8;
1600 var->transp.length = 0;
1601 var->transp.offset = 0;
1602 break;
1603 case 32:
1604 var->red.offset = 16;
1605 var->green.offset = 8;
1606 var->blue.offset = 0;
1607 var->red.length = 8;
1608 var->green.length = 8;
1609 var->blue.length = 8;
1610 var->transp.length = 8;
1611 var->transp.offset = 24;
1612 break;
1613 default:
1614 return -EINVAL;
1615 }
1616 return 0;
1617}
1618EXPORT_SYMBOL(drm_fb_helper_check_var);
1619
Daniel Vetter207fd322013-01-20 22:13:14 +01001620/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001621 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001622 * @info: fbdev registered by the helper
1623 *
1624 * This will let fbcon do the mode init and is called at initialization time by
1625 * the fbdev core when registering the driver, and later on through the hotplug
1626 * callback.
1627 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001628int drm_fb_helper_set_par(struct fb_info *info)
1629{
1630 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001631 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001632
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001633 if (oops_in_progress)
1634 return -EBUSY;
1635
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001636 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001637 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001638 return -EINVAL;
1639 }
1640
Rob Clark5ea1f752014-05-30 12:29:48 -04001641 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001642
Dave Airlie785b93e2009-08-28 15:46:53 +10001643 return 0;
1644}
1645EXPORT_SYMBOL(drm_fb_helper_set_par);
1646
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001647static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
Rob Clark1edf0262015-08-25 15:35:59 -04001648{
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001649 int i;
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001650
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001651 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001652 struct drm_mode_set *mode_set;
1653
1654 mode_set = &fb_helper->crtc_info[i].mode_set;
1655
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001656 mode_set->x = x;
1657 mode_set->y = y;
Rob Clark1edf0262015-08-25 15:35:59 -04001658 }
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001659}
Rob Clark1edf0262015-08-25 15:35:59 -04001660
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001661static int pan_display_atomic(struct fb_var_screeninfo *var,
1662 struct fb_info *info)
1663{
1664 struct drm_fb_helper *fb_helper = info->par;
1665 int ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001666
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001667 pan_set(fb_helper, var->xoffset, var->yoffset);
Rob Clark1edf0262015-08-25 15:35:59 -04001668
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001669 ret = restore_fbdev_mode_atomic(fb_helper, true);
1670 if (!ret) {
1671 info->var.xoffset = var->xoffset;
1672 info->var.yoffset = var->yoffset;
1673 } else
1674 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001675
Rob Clark1edf0262015-08-25 15:35:59 -04001676 return ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001677}
1678
Daniel Vetter71286452017-04-03 10:33:04 +02001679static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001680 struct fb_info *info)
1681{
1682 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001683 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001684 int ret = 0;
1685 int i;
1686
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001687 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001688 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001689 modeset = &fb_helper->crtc_info[i].mode_set;
1690
1691 modeset->x = var->xoffset;
1692 modeset->y = var->yoffset;
1693
1694 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001695 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001696 if (!ret) {
1697 info->var.xoffset = var->xoffset;
1698 info->var.yoffset = var->yoffset;
1699 }
1700 }
1701 }
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001702 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001703
1704 return ret;
1705}
1706
1707/**
1708 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1709 * @var: updated screen information
1710 * @info: fbdev registered by the helper
1711 */
1712int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1713 struct fb_info *info)
1714{
1715 struct drm_fb_helper *fb_helper = info->par;
1716 struct drm_device *dev = fb_helper->dev;
1717 int ret;
1718
1719 if (oops_in_progress)
1720 return -EBUSY;
1721
Thierry Redinge9827d82017-07-04 17:18:23 +02001722 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001723 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001724 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001725 return -EBUSY;
1726 }
1727
1728 if (drm_drv_uses_atomic_modeset(dev))
1729 ret = pan_display_atomic(var, info);
1730 else
1731 ret = pan_display_legacy(var, info);
Thierry Redinge9827d82017-07-04 17:18:23 +02001732 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001733
Dave Airlie785b93e2009-08-28 15:46:53 +10001734 return ret;
1735}
1736EXPORT_SYMBOL(drm_fb_helper_pan_display);
1737
Daniel Vetter8acf6582013-01-21 23:38:37 +01001738/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001739 * Allocates the backing storage and sets up the fbdev info structure through
Daniel Vetterca91a272017-07-06 15:00:21 +02001740 * the ->fb_probe callback.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001741 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001742static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1743 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001744{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001745 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001746 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001747 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001748 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001749 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001750
1751 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1752 sizes.surface_depth = 24;
1753 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001754 sizes.fb_width = (u32)-1;
1755 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001756
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001757 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001758 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001759 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001760
Dave Airlie785b93e2009-08-28 15:46:53 +10001761 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001762 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001763 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001764 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001765
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001766 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001767
1768 if (cmdline_mode->bpp_specified) {
1769 switch (cmdline_mode->bpp) {
1770 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001771 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001772 break;
1773 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001774 sizes.surface_depth = 15;
1775 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001776 break;
1777 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001778 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001779 break;
1780 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001781 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001782 break;
1783 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001784 sizes.surface_depth = 24;
1785 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001786 break;
1787 }
1788 break;
1789 }
1790 }
1791
Dave Airlie8be48d92010-03-30 05:34:14 +00001792 crtc_count = 0;
1793 for (i = 0; i < fb_helper->crtc_count; i++) {
1794 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001795 struct drm_mode_set *mode_set;
1796 int x, y, j;
1797 /* in case of tile group, are we the last tile vert or horiz?
1798 * If no tile group you are always the last one both vertically
1799 * and horizontally
1800 */
1801 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001802
Dave Airlie8be48d92010-03-30 05:34:14 +00001803 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001804 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001805
1806 if (!desired_mode)
1807 continue;
1808
1809 crtc_count++;
1810
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001811 x = fb_helper->crtc_info[i].x;
1812 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001813
1814 if (gamma_size == 0)
1815 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1816
1817 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1818 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001819
1820 for (j = 0; j < mode_set->num_connectors; j++) {
1821 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001822
Rob Clark0e3704c2015-03-11 10:23:14 -04001823 if (connector->has_tile) {
1824 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1825 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1826 /* cloning to multiple tiles is just crazy-talk, so: */
1827 break;
1828 }
1829 }
1830
1831 if (lasth)
1832 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1833 if (lastv)
1834 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001835 }
1836
Dave Airlie38651672010-03-30 05:34:13 +00001837 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Daniel Vetterca91a272017-07-06 15:00:21 +02001838 DRM_INFO("Cannot find any crtc or sizes\n");
Maarten Lankhorst52dd0652017-11-28 12:16:03 +01001839
1840 /* First time: disable all crtc's.. */
1841 if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
1842 restore_fbdev_mode(fb_helper);
Daniel Vetterca91a272017-07-06 15:00:21 +02001843 return -EAGAIN;
Dave Airlie785b93e2009-08-28 15:46:53 +10001844 }
1845
Xinliang Liu5f152572017-02-15 17:19:08 +01001846 /* Handle our overallocation */
1847 sizes.surface_height *= drm_fbdev_overalloc;
1848 sizes.surface_height /= 100;
1849
Dave Airlie38651672010-03-30 05:34:13 +00001850 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001851 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1852 if (ret < 0)
1853 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001854
Dave Airlie785b93e2009-08-28 15:46:53 +10001855 return 0;
1856}
Dave Airlie785b93e2009-08-28 15:46:53 +10001857
Daniel Vetter207fd322013-01-20 22:13:14 +01001858/**
1859 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1860 * @info: fbdev registered by the helper
1861 * @pitch: desired pitch
1862 * @depth: desired depth
1863 *
1864 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1865 * fbdev emulations. Drivers which support acceleration methods which impose
1866 * additional constraints need to set up their own limits.
1867 *
1868 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001869 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001870 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001871void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1872 uint32_t depth)
1873{
1874 info->fix.type = FB_TYPE_PACKED_PIXELS;
1875 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1876 FB_VISUAL_TRUECOLOR;
1877 info->fix.mmio_start = 0;
1878 info->fix.mmio_len = 0;
1879 info->fix.type_aux = 0;
1880 info->fix.xpanstep = 1; /* doing it in hw */
1881 info->fix.ypanstep = 1; /* doing it in hw */
1882 info->fix.ywrapstep = 0;
1883 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001884
1885 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001886}
1887EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1888
Daniel Vetter207fd322013-01-20 22:13:14 +01001889/**
1890 * drm_fb_helper_fill_var - initalizes variable fbdev information
1891 * @info: fbdev instance to set up
1892 * @fb_helper: fb helper instance to use as template
1893 * @fb_width: desired fb width
1894 * @fb_height: desired fb height
1895 *
1896 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001897 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001898 *
1899 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001900 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1901 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001902 */
Dave Airlie38651672010-03-30 05:34:13 +00001903void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001904 uint32_t fb_width, uint32_t fb_height)
1905{
Dave Airlie38651672010-03-30 05:34:13 +00001906 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001907
Dave Airlie38651672010-03-30 05:34:13 +00001908 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001909 info->var.xres_virtual = fb->width;
1910 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001911 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001912 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001913 info->var.xoffset = 0;
1914 info->var.yoffset = 0;
1915 info->var.activate = FB_ACTIVATE_NOW;
Dave Airlie785b93e2009-08-28 15:46:53 +10001916
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001917 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001918 case 8:
1919 info->var.red.offset = 0;
1920 info->var.green.offset = 0;
1921 info->var.blue.offset = 0;
1922 info->var.red.length = 8; /* 8bit DAC */
1923 info->var.green.length = 8;
1924 info->var.blue.length = 8;
1925 info->var.transp.offset = 0;
1926 info->var.transp.length = 0;
1927 break;
1928 case 15:
1929 info->var.red.offset = 10;
1930 info->var.green.offset = 5;
1931 info->var.blue.offset = 0;
1932 info->var.red.length = 5;
1933 info->var.green.length = 5;
1934 info->var.blue.length = 5;
1935 info->var.transp.offset = 15;
1936 info->var.transp.length = 1;
1937 break;
1938 case 16:
1939 info->var.red.offset = 11;
1940 info->var.green.offset = 5;
1941 info->var.blue.offset = 0;
1942 info->var.red.length = 5;
1943 info->var.green.length = 6;
1944 info->var.blue.length = 5;
1945 info->var.transp.offset = 0;
1946 break;
1947 case 24:
1948 info->var.red.offset = 16;
1949 info->var.green.offset = 8;
1950 info->var.blue.offset = 0;
1951 info->var.red.length = 8;
1952 info->var.green.length = 8;
1953 info->var.blue.length = 8;
1954 info->var.transp.offset = 0;
1955 info->var.transp.length = 0;
1956 break;
1957 case 32:
1958 info->var.red.offset = 16;
1959 info->var.green.offset = 8;
1960 info->var.blue.offset = 0;
1961 info->var.red.length = 8;
1962 info->var.green.length = 8;
1963 info->var.blue.length = 8;
1964 info->var.transp.offset = 24;
1965 info->var.transp.length = 8;
1966 break;
1967 default:
1968 break;
1969 }
1970
1971 info->var.xres = fb_width;
1972 info->var.yres = fb_height;
1973}
1974EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001975
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001976static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
Daniel Vettere13a0582017-07-05 06:56:29 +02001977 uint32_t maxX,
1978 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001979{
1980 struct drm_connector *connector;
Daniel Vettere13a0582017-07-05 06:56:29 +02001981 int i, count = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001982
Chris Wilson966a6a12016-11-29 12:02:15 +00001983 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001984 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001985 count += connector->funcs->fill_modes(connector, maxX, maxY);
1986 }
1987
1988 return count;
1989}
1990
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001991struct 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 +00001992{
1993 struct drm_display_mode *mode;
1994
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001995 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001996 if (mode->hdisplay > width ||
1997 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001998 continue;
1999 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2000 return mode;
2001 }
2002 return NULL;
2003}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002004EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002005
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002006static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00002007{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02002008 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00002009}
2010
Vincent Abrioua09759e2017-01-06 17:44:43 +01002011struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00002012{
Chris Wilson1794d252011-04-17 07:43:32 +01002013 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002014 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01002015 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00002016
Chris Wilsoneaf99c72014-08-06 10:08:32 +02002017 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00002018 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00002019 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00002020
2021 /* attempt to find a matching mode in the list of modes
2022 * we have gotten so far, if not add a CVT mode that conforms
2023 */
2024 if (cmdline_mode->rb || cmdline_mode->margins)
2025 goto create_mode;
2026
Takashi Iwaic683f422014-03-19 14:53:13 +01002027 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002028again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002029 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00002030 /* check width/height */
2031 if (mode->hdisplay != cmdline_mode->xres ||
2032 mode->vdisplay != cmdline_mode->yres)
2033 continue;
2034
2035 if (cmdline_mode->refresh_specified) {
2036 if (mode->vrefresh != cmdline_mode->refresh)
2037 continue;
2038 }
2039
2040 if (cmdline_mode->interlace) {
2041 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2042 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01002043 } else if (prefer_non_interlace) {
2044 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2045 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002046 }
2047 return mode;
2048 }
2049
Takashi Iwaic683f422014-03-19 14:53:13 +01002050 if (prefer_non_interlace) {
2051 prefer_non_interlace = false;
2052 goto again;
2053 }
2054
Dave Airlie38651672010-03-30 05:34:13 +00002055create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01002056 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2057 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002058 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00002059 return mode;
2060}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002061EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002062
2063static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2064{
2065 bool enable;
2066
Dave Airlieb5f05382017-10-16 05:08:39 +01002067 if (connector->display_info.non_desktop)
2068 return false;
2069
Sachin Kamat96081cd2012-11-15 03:43:30 +00002070 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00002071 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002072 else
Dave Airlie38651672010-03-30 05:34:13 +00002073 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002074
Dave Airlie38651672010-03-30 05:34:13 +00002075 return enable;
2076}
2077
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002078static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2079 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00002080{
2081 bool any_enabled = false;
2082 struct drm_connector *connector;
2083 int i = 0;
2084
Chris Wilson966a6a12016-11-29 12:02:15 +00002085 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002086 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002087 enabled[i] = drm_connector_enabled(connector, true);
2088 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
Dave Airlieb5f05382017-10-16 05:08:39 +01002089 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
2090
Dave Airlie38651672010-03-30 05:34:13 +00002091 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00002092 }
2093
2094 if (any_enabled)
2095 return;
2096
Chris Wilson966a6a12016-11-29 12:02:15 +00002097 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002098 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002099 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00002100 }
2101}
2102
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002103static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2104 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002105 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002106 bool *enabled, int width, int height)
2107{
2108 int count, i, j;
2109 bool can_clone = false;
2110 struct drm_fb_helper_connector *fb_helper_conn;
2111 struct drm_display_mode *dmt_mode, *mode;
2112
2113 /* only contemplate cloning in the single crtc case */
2114 if (fb_helper->crtc_count > 1)
2115 return false;
2116
2117 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002118 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002119 if (enabled[i])
2120 count++;
2121 }
2122
2123 /* only contemplate cloning if more than one connector is enabled */
2124 if (count <= 1)
2125 return false;
2126
2127 /* check the command line or if nothing common pick 1024x768 */
2128 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002129 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002130 if (!enabled[i])
2131 continue;
2132 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002133 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002134 if (!modes[i]) {
2135 can_clone = false;
2136 break;
2137 }
2138 for (j = 0; j < i; j++) {
2139 if (!enabled[j])
2140 continue;
2141 if (!drm_mode_equal(modes[j], modes[i]))
2142 can_clone = false;
2143 }
2144 }
2145
2146 if (can_clone) {
2147 DRM_DEBUG_KMS("can clone using command line\n");
2148 return true;
2149 }
2150
2151 /* try and find a 1024x768 mode on each connector */
2152 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002153 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002154
Chris Wilson966a6a12016-11-29 12:02:15 +00002155 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002156 if (!enabled[i])
2157 continue;
2158
2159 fb_helper_conn = fb_helper->connector_info[i];
2160 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2161 if (drm_mode_equal(mode, dmt_mode))
2162 modes[i] = mode;
2163 }
2164 if (!modes[i])
2165 can_clone = false;
2166 }
2167
2168 if (can_clone) {
2169 DRM_DEBUG_KMS("can clone using 1024x768\n");
2170 return true;
2171 }
2172 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2173 return false;
2174}
2175
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002176static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2177 struct drm_display_mode **modes,
2178 struct drm_fb_offset *offsets,
2179 int idx,
2180 int h_idx, int v_idx)
2181{
2182 struct drm_fb_helper_connector *fb_helper_conn;
2183 int i;
2184 int hoffset = 0, voffset = 0;
2185
Chris Wilson966a6a12016-11-29 12:02:15 +00002186 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002187 fb_helper_conn = fb_helper->connector_info[i];
2188 if (!fb_helper_conn->connector->has_tile)
2189 continue;
2190
2191 if (!modes[i] && (h_idx || v_idx)) {
2192 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2193 fb_helper_conn->connector->base.id);
2194 continue;
2195 }
2196 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2197 hoffset += modes[i]->hdisplay;
2198
2199 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2200 voffset += modes[i]->vdisplay;
2201 }
2202 offsets[idx].x = hoffset;
2203 offsets[idx].y = voffset;
2204 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2205 return 0;
2206}
2207
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002208static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002209 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002210 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002211 bool *enabled, int width, int height)
2212{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002213 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002214 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2215 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002216 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002217 int i;
2218
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002219retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002220 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002221 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002222
Chris Wilsonc96521e2016-11-27 17:09:10 +00002223 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002224 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002225
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002226 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002227 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002228 continue;
2229 }
2230
2231 /* first pass over all the untiled connectors */
2232 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2233 continue;
2234
2235 if (tile_pass == 1) {
2236 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2237 fb_helper_conn->connector->tile_v_loc != 0)
2238 continue;
2239
2240 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002241 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002242 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2243 /* if this tile_pass doesn't cover any of the tiles - keep going */
2244 continue;
2245
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002246 /*
2247 * find the tile offsets for this pass - need to find
2248 * all tiles left and above
2249 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002250 drm_get_tile_offsets(fb_helper, modes, offsets,
2251 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2252 }
Dave Airlie38651672010-03-30 05:34:13 +00002253 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002254 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002255
2256 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002257 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002258 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002259 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2260 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 +00002261 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002262 }
2263 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002264 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2265 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002266 break;
2267 }
2268 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2269 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002270 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002271 }
2272
2273 if ((conn_configured & mask) != mask) {
2274 tile_pass++;
2275 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002276 }
2277 return true;
2278}
2279
Dave Airlie8be48d92010-03-30 05:34:14 +00002280static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2281 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002282 struct drm_display_mode **modes,
2283 int n, int width, int height)
2284{
2285 int c, o;
2286 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002287 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002288 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002289 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002290 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002291 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002292
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002293 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002294 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002295
2296 fb_helper_conn = fb_helper->connector_info[n];
2297 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002298
2299 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002300 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002301 if (modes[n] == NULL)
2302 return best_score;
2303
Harsha Sharma4b947b1c2017-10-13 13:07:47 +05302304 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002305 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002306 if (!crtcs)
2307 return best_score;
2308
2309 my_score = 1;
2310 if (connector->status == connector_status_connected)
2311 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002312 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002313 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002314 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002315 my_score++;
2316
2317 connector_funcs = connector->helper_private;
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002318
2319 /*
2320 * If the DRM device implements atomic hooks and ->best_encoder() is
2321 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2322 * helper.
2323 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002324 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002325 !connector_funcs->best_encoder)
2326 encoder = drm_atomic_helper_best_encoder(connector);
2327 else
2328 encoder = connector_funcs->best_encoder(connector);
2329
Dave Airlie38651672010-03-30 05:34:13 +00002330 if (!encoder)
2331 goto out;
2332
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002333 /*
2334 * select a crtc for this connector and then attempt to configure
2335 * remaining connectors
2336 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002337 for (c = 0; c < fb_helper->crtc_count; c++) {
2338 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002339
Sachin Kamat96081cd2012-11-15 03:43:30 +00002340 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002341 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002342
2343 for (o = 0; o < n; o++)
2344 if (best_crtcs[o] == crtc)
2345 break;
2346
2347 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002348 /* ignore cloning unless only a single crtc */
2349 if (fb_helper->crtc_count > 1)
2350 continue;
2351
2352 if (!drm_mode_equal(modes[o], modes[n]))
2353 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002354 }
2355
2356 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002357 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2358 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002359 width, height);
2360 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002361 best_score = score;
2362 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002363 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002364 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002365 }
Dave Airlie38651672010-03-30 05:34:13 +00002366 }
2367out:
2368 kfree(crtcs);
2369 return best_score;
2370}
2371
Hans de Goede8f0cb412017-11-25 20:35:50 +01002372/*
2373 * This function checks if rotation is necessary because of panel orientation
2374 * and if it is, if it is supported.
2375 * If rotation is necessary and supported, its gets set in fb_crtc.rotation.
2376 * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets
2377 * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only
2378 * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do
2379 * the unsupported rotation.
2380 */
2381static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper,
2382 struct drm_fb_helper_crtc *fb_crtc,
2383 struct drm_connector *connector)
2384{
2385 struct drm_plane *plane = fb_crtc->mode_set.crtc->primary;
2386 uint64_t valid_mask = 0;
2387 int i, rotation;
2388
2389 fb_crtc->rotation = DRM_MODE_ROTATE_0;
2390
2391 switch (connector->display_info.panel_orientation) {
2392 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
2393 rotation = DRM_MODE_ROTATE_180;
2394 break;
2395 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
2396 rotation = DRM_MODE_ROTATE_90;
2397 break;
2398 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
2399 rotation = DRM_MODE_ROTATE_270;
2400 break;
2401 default:
2402 rotation = DRM_MODE_ROTATE_0;
2403 }
2404
2405 /*
2406 * TODO: support 90 / 270 degree hardware rotation,
2407 * depending on the hardware this may require the framebuffer
2408 * to be in a specific tiling format.
2409 */
2410 if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) {
2411 fb_helper->sw_rotations |= rotation;
2412 return;
2413 }
2414
2415 for (i = 0; i < plane->rotation_property->num_values; i++)
2416 valid_mask |= (1ULL << plane->rotation_property->values[i]);
2417
2418 if (!(rotation & valid_mask)) {
2419 fb_helper->sw_rotations |= rotation;
2420 return;
2421 }
2422
2423 fb_crtc->rotation = rotation;
2424 /* Rotating in hardware, fbcon should not rotate */
2425 fb_helper->sw_rotations |= DRM_MODE_ROTATE_0;
2426}
2427
Chris Wilson64e94402016-11-29 12:02:16 +00002428static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2429 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002430{
Dave Airlie8be48d92010-03-30 05:34:14 +00002431 struct drm_device *dev = fb_helper->dev;
2432 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002433 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002434 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002435 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002436 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002437
2438 DRM_DEBUG_KMS("\n");
Chris Wilson966a6a12016-11-29 12:02:15 +00002439 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002440 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002441
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002442 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002443 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002444 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002445 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002446 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002447 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002448 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002449 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002450 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002451 DRM_ERROR("Memory allocation failed\n");
2452 goto out;
2453 }
2454
Daniel Vettere13a0582017-07-05 06:56:29 +02002455 mutex_lock(&fb_helper->dev->mode_config.mutex);
2456 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2457 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002458 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002459
Jesse Barnes11e17a02013-02-19 13:31:39 -08002460 if (!(fb_helper->funcs->initial_config &&
2461 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002462 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002463 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002464 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2465 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2466 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002467
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002468 if (!drm_target_cloned(fb_helper, modes, offsets,
2469 enabled, width, height) &&
2470 !drm_target_preferred(fb_helper, modes, offsets,
2471 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002472 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002473
2474 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2475 width, height);
2476
2477 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002478 }
Daniel Vettere13a0582017-07-05 06:56:29 +02002479 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002480
Dave Airlie8be48d92010-03-30 05:34:14 +00002481 /* need to set the modesets up here for use later */
2482 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002483 for (i = 0; i < fb_helper->crtc_count; i++)
2484 drm_fb_helper_modeset_release(fb_helper,
2485 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002486
Hans de Goede8f0cb412017-11-25 20:35:50 +01002487 fb_helper->sw_rotations = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002488 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002489 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002490 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002491 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie38651672010-03-30 05:34:13 +00002492
Dave Airlie8be48d92010-03-30 05:34:14 +00002493 if (mode && fb_crtc) {
David Lechner27a061f2017-08-02 13:00:13 -05002494 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Ville Syrjäläa2889602016-10-26 17:41:18 +03002495 struct drm_connector *connector =
2496 fb_helper->connector_info[i]->connector;
2497
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002498 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2499 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002500
Dave Airlie8be48d92010-03-30 05:34:14 +00002501 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002502 fb_crtc->x = offset->x;
2503 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002504 modeset->mode = drm_mode_duplicate(dev,
2505 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002506 drm_connector_get(connector);
Hans de Goede8f0cb412017-11-25 20:35:50 +01002507 drm_setup_crtc_rotation(fb_helper, fb_crtc, connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002508 modeset->connectors[modeset->num_connectors++] = connector;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002509 modeset->x = offset->x;
2510 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002511 }
Dave Airlie38651672010-03-30 05:34:13 +00002512 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002513out:
Dave Airlie38651672010-03-30 05:34:13 +00002514 kfree(crtcs);
2515 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002516 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002517 kfree(enabled);
2518}
2519
David Lechnerf461bd22017-08-03 11:19:08 -05002520/*
2521 * This is a continuation of drm_setup_crtcs() that sets up anything related
2522 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2523 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2524 * So, any setup that touches those fields needs to be done here instead of in
2525 * drm_setup_crtcs().
2526 */
2527static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2528{
David Lechner991a3992017-08-04 11:25:24 -05002529 struct fb_info *info = fb_helper->fbdev;
David Lechnerf461bd22017-08-03 11:19:08 -05002530 int i;
2531
2532 for (i = 0; i < fb_helper->crtc_count; i++)
2533 if (fb_helper->crtc_info[i].mode_set.num_connectors)
2534 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
David Lechner991a3992017-08-04 11:25:24 -05002535
2536 mutex_lock(&fb_helper->dev->mode_config.mutex);
2537 drm_fb_helper_for_each_connector(fb_helper, i) {
2538 struct drm_connector *connector =
2539 fb_helper->connector_info[i]->connector;
2540
2541 /* use first connected connector for the physical dimensions */
2542 if (connector->status == connector_status_connected) {
2543 info->var.width = connector->display_info.width_mm;
2544 info->var.height = connector->display_info.height_mm;
2545 break;
2546 }
2547 }
2548 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Hans de Goede8f0cb412017-11-25 20:35:50 +01002549
2550 switch (fb_helper->sw_rotations) {
2551 case DRM_MODE_ROTATE_0:
2552 info->fbcon_rotate_hint = FB_ROTATE_UR;
2553 break;
2554 case DRM_MODE_ROTATE_90:
2555 info->fbcon_rotate_hint = FB_ROTATE_CCW;
2556 break;
2557 case DRM_MODE_ROTATE_180:
2558 info->fbcon_rotate_hint = FB_ROTATE_UD;
2559 break;
2560 case DRM_MODE_ROTATE_270:
2561 info->fbcon_rotate_hint = FB_ROTATE_CW;
2562 break;
2563 default:
2564 /*
2565 * Multiple bits are set / multiple rotations requested
2566 * fbcon cannot handle separate rotation settings per
2567 * output, so fallback to unrotated.
2568 */
2569 info->fbcon_rotate_hint = FB_ROTATE_UR;
2570 }
David Lechnerf461bd22017-08-03 11:19:08 -05002571}
2572
Daniel Vetterca91a272017-07-06 15:00:21 +02002573/* Note: Drops fb_helper->lock before returning. */
2574static int
2575__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2576 int bpp_sel)
2577{
2578 struct drm_device *dev = fb_helper->dev;
2579 struct fb_info *info;
2580 unsigned int width, height;
2581 int ret;
2582
2583 width = dev->mode_config.max_width;
2584 height = dev->mode_config.max_height;
2585
2586 drm_setup_crtcs(fb_helper, width, height);
2587 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2588 if (ret < 0) {
2589 if (ret == -EAGAIN) {
2590 fb_helper->preferred_bpp = bpp_sel;
2591 fb_helper->deferred_setup = true;
2592 ret = 0;
2593 }
2594 mutex_unlock(&fb_helper->lock);
2595
2596 return ret;
2597 }
David Lechnerf461bd22017-08-03 11:19:08 -05002598 drm_setup_crtcs_fb(fb_helper);
Daniel Vetterca91a272017-07-06 15:00:21 +02002599
2600 fb_helper->deferred_setup = false;
2601
2602 info = fb_helper->fbdev;
2603 info->var.pixclock = 0;
2604
2605 /* Need to drop locks to avoid recursive deadlock in
2606 * register_framebuffer. This is ok because the only thing left to do is
2607 * register the fbdev emulation instance in kernel_fb_helper_list. */
2608 mutex_unlock(&fb_helper->lock);
2609
2610 ret = register_framebuffer(info);
2611 if (ret < 0)
2612 return ret;
2613
2614 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2615 info->node, info->fix.id);
2616
2617 mutex_lock(&kernel_fb_helper_lock);
2618 if (list_empty(&kernel_fb_helper_list))
2619 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2620
2621 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2622 mutex_unlock(&kernel_fb_helper_lock);
2623
2624 return 0;
2625}
2626
Dave Airlie38651672010-03-30 05:34:13 +00002627/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002628 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002629 * @fb_helper: fb_helper device struct
2630 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002631 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002632 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002633 * At the moment, this is a cloned configuration across all heads with
2634 * a new framebuffer object as the backing store.
2635 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002636 * Note that this also registers the fbdev and so allows userspace to call into
2637 * the driver through the fbdev interfaces.
2638 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002639 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2640 * to let the driver allocate and initialize the fbdev info structure and the
2641 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002642 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2643 * values for the fbdev info structure.
2644 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002645 * HANG DEBUGGING:
2646 *
2647 * When you have fbcon support built-in or already loaded, this function will do
2648 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2649 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2650 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2651 * to consoles, not even serial console. This means when your driver crashes,
2652 * you will see absolutely nothing else but a system stuck in this function,
2653 * with no further output. Any kind of printk() you place within your own driver
2654 * or in the drm core modeset code will also never show up.
2655 *
2656 * Standard debug practice is to run the fbcon setup without taking the
2657 * console_lock as a hack, to be able to see backtraces and crashes on the
2658 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2659 * cmdline option.
2660 *
2661 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002662 * first modeset from userspace will crash in the same way, and is even easier
2663 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002664 * kernel cmdline option.
2665 *
Dave Airlie38651672010-03-30 05:34:13 +00002666 * RETURNS:
2667 * Zero if everything went ok, nonzero otherwise.
2668 */
Thierry Reding01934c22014-12-19 11:21:32 +01002669int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002670{
Chris Wilson966a6a12016-11-29 12:02:15 +00002671 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002672
Daniel Vetterf64c5572015-08-25 15:45:13 +02002673 if (!drm_fbdev_emulation)
2674 return 0;
2675
Thierry Redinge9827d82017-07-04 17:18:23 +02002676 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002677 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00002678
Daniel Vetterca91a272017-07-06 15:00:21 +02002679 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002680}
Dave Airlie8be48d92010-03-30 05:34:14 +00002681EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002682
Chris Wilson73943712011-04-22 11:03:57 +01002683/**
2684 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002685 * probing all the outputs attached to the fb
Noralf Trønnesc7779902017-10-30 16:39:37 +01002686 * @fb_helper: driver-allocated fbdev helper, can be NULL
Chris Wilson73943712011-04-22 11:03:57 +01002687 *
Chris Wilson73943712011-04-22 11:03:57 +01002688 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002689 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002690 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002691 * Called at runtime, takes the mode config locks to be able to check/change the
2692 * modeset configuration. Must be run from process context (which usually means
2693 * either the output polling work or a work item launched from the driver's
2694 * hotplug interrupt).
2695 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002696 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002697 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002698 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2699 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002700 *
Chris Wilson73943712011-04-22 11:03:57 +01002701 * RETURNS:
2702 * 0 on success and a non-zero error code otherwise.
2703 */
2704int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002705{
Thierry Redinge9827d82017-07-04 17:18:23 +02002706 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002707
Noralf Trønnesc7779902017-10-30 16:39:37 +01002708 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +02002709 return 0;
2710
Thierry Redinge9827d82017-07-04 17:18:23 +02002711 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002712 if (fb_helper->deferred_setup) {
2713 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
2714 fb_helper->preferred_bpp);
2715 return err;
2716 }
2717
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002718 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002719 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002720 mutex_unlock(&fb_helper->lock);
2721 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002722 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002723
Dave Airlie38651672010-03-30 05:34:13 +00002724 DRM_DEBUG_KMS("\n");
2725
Chris Wilson64e94402016-11-29 12:02:16 +00002726 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
David Lechnerf461bd22017-08-03 11:19:08 -05002727 drm_setup_crtcs_fb(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +02002728 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002729
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002730 drm_fb_helper_set_par(fb_helper->fbdev);
2731
2732 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002733}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002734EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002735
Noralf Trønnes304a4f62017-10-30 16:39:39 +01002736/**
Noralf Trønnes87412162017-12-15 18:51:14 +01002737 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
2738 * @dev: DRM device
2739 * @fb_helper: fbdev helper structure to set up
2740 * @funcs: fbdev helper functions
2741 * @preferred_bpp: Preferred bits per pixel for the device.
2742 * @dev->mode_config.preferred_depth is used if this is zero.
2743 * @max_conn_count: Maximum number of connectors.
2744 * @dev->mode_config.num_connector is used if this is zero.
2745 *
2746 * This function sets up fbdev emulation and registers fbdev for access by
2747 * userspace. If all connectors are disconnected, setup is deferred to the next
2748 * time drm_fb_helper_hotplug_event() is called.
2749 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
2750 * function.
2751 *
2752 * See also: drm_fb_helper_initial_config()
2753 *
2754 * Returns:
2755 * Zero on success or negative error code on failure.
2756 */
2757int drm_fb_helper_fbdev_setup(struct drm_device *dev,
2758 struct drm_fb_helper *fb_helper,
2759 const struct drm_fb_helper_funcs *funcs,
2760 unsigned int preferred_bpp,
2761 unsigned int max_conn_count)
2762{
2763 int ret;
2764
2765 if (!preferred_bpp)
2766 preferred_bpp = dev->mode_config.preferred_depth;
2767 if (!preferred_bpp)
2768 preferred_bpp = 32;
2769
2770 if (!max_conn_count)
2771 max_conn_count = dev->mode_config.num_connector;
2772 if (!max_conn_count) {
2773 DRM_DEV_ERROR(dev->dev, "No connectors\n");
2774 return -EINVAL;
2775 }
2776
2777 drm_fb_helper_prepare(dev, fb_helper, funcs);
2778
2779 ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
2780 if (ret < 0) {
2781 DRM_DEV_ERROR(dev->dev, "Failed to initialize fbdev helper\n");
2782 return ret;
2783 }
2784
2785 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
2786 if (ret < 0) {
2787 DRM_DEV_ERROR(dev->dev, "Failed to add connectors\n");
2788 goto err_drm_fb_helper_fini;
2789 }
2790
2791 if (!drm_drv_uses_atomic_modeset(dev))
2792 drm_helper_disable_unused_functions(dev);
2793
2794 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
2795 if (ret < 0) {
2796 DRM_DEV_ERROR(dev->dev, "Failed to set fbdev configuration\n");
2797 goto err_drm_fb_helper_fini;
2798 }
2799
2800 return 0;
2801
2802err_drm_fb_helper_fini:
2803 drm_fb_helper_fini(fb_helper);
2804
2805 return ret;
2806}
2807EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
2808
2809/**
2810 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
2811 * @dev: DRM device
2812 *
2813 * This function unregisters fbdev if not already done and cleans up the
2814 * associated resources including the &drm_framebuffer.
2815 * The driver is responsible for freeing the &drm_fb_helper structure which is
2816 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
2817 * when this function returns.
2818 *
2819 * In order to support device removal/unplug while file handles are still open,
2820 * drm_fb_helper_unregister_fbi() should be called on device removal and
2821 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
2822 * file handles are closed.
2823 */
2824void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
2825{
2826 struct drm_fb_helper *fb_helper = dev->fb_helper;
2827
2828 if (!fb_helper)
2829 return;
2830
2831 /* Unregister if it hasn't been done already */
2832 if (fb_helper->fbdev && fb_helper->fbdev->dev)
2833 drm_fb_helper_unregister_fbi(fb_helper);
2834
2835 drm_fb_helper_fini(fb_helper);
2836
2837 if (fb_helper->fb)
2838 drm_framebuffer_remove(fb_helper->fb);
2839}
2840EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
2841
2842/**
Noralf Trønnes304a4f62017-10-30 16:39:39 +01002843 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
2844 * @dev: DRM device
2845 *
2846 * This function can be used as the &drm_driver->lastclose callback for drivers
2847 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
2848 */
2849void drm_fb_helper_lastclose(struct drm_device *dev)
2850{
2851 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
2852}
2853EXPORT_SYMBOL(drm_fb_helper_lastclose);
2854
2855/**
2856 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2857 * helper for fbdev emulation
2858 * @dev: DRM device
2859 *
2860 * This function can be used as the
2861 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2862 * need to call drm_fb_helper_hotplug_event().
2863 */
2864void drm_fb_helper_output_poll_changed(struct drm_device *dev)
2865{
2866 drm_fb_helper_hotplug_event(dev->fb_helper);
2867}
2868EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
2869
David Rientjes6a108a12011-01-20 14:44:16 -08002870/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002871 * but the module doesn't depend on any fb console symbols. At least
2872 * attempt to load fbcon to avoid leaving the system without a usable console.
2873 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002874int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002875{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002876#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07002877 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06002878 struct module *fbcon;
2879
2880 mutex_lock(&module_mutex);
2881 fbcon = find_module(name);
2882 mutex_unlock(&module_mutex);
2883
2884 if (!fbcon)
2885 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002886#endif
David Fries3ce05162010-12-12 12:39:22 -06002887 return 0;
2888}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002889EXPORT_SYMBOL(drm_fb_helper_modinit);