blob: 0646b108030ba5d1d41040af3fffa7386be7853d [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
Noralf Trønnes48c95712017-12-15 18:51:17 +0100110 * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup
111 * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()).
Daniel Vetterd0ddc0332012-11-01 14:45:17 +0100112 */
113
Chris Wilson966a6a12016-11-29 12:02:15 +0000114#define drm_fb_helper_for_each_connector(fbh, i__) \
Daniel Vettere13a0582017-07-05 06:56:29 +0200115 for (({ lockdep_assert_held(&(fbh)->lock); }), \
Chris Wilson966a6a12016-11-29 12:02:15 +0000116 i__ = 0; i__ < (fbh)->connector_count; i__++)
117
Thierry Redingaf2405a2017-07-04 17:18:21 +0200118static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
119 struct drm_connector *connector)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200120{
Thierry Reding50021ff2017-03-29 16:43:53 +0200121 struct drm_fb_helper_connector *fb_conn;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200122 struct drm_fb_helper_connector **temp;
Thierry Reding50021ff2017-03-29 16:43:53 +0200123 unsigned int count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200124
125 if (!drm_fbdev_emulation)
126 return 0;
127
Thierry Redinge9827d82017-07-04 17:18:23 +0200128 lockdep_assert_held(&fb_helper->lock);
Thierry Reding50021ff2017-03-29 16:43:53 +0200129
130 count = fb_helper->connector_count + 1;
131
132 if (count > fb_helper->connector_info_alloc_count) {
133 size_t size = count * sizeof(fb_conn);
134
135 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200136 if (!temp)
137 return -ENOMEM;
138
Thierry Reding50021ff2017-03-29 16:43:53 +0200139 fb_helper->connector_info_alloc_count = count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200140 fb_helper->connector_info = temp;
141 }
142
Thierry Reding50021ff2017-03-29 16:43:53 +0200143 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
144 if (!fb_conn)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200145 return -ENOMEM;
146
147 drm_connector_get(connector);
Thierry Reding50021ff2017-03-29 16:43:53 +0200148 fb_conn->connector = connector;
149 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
Thierry Redingaf2405a2017-07-04 17:18:21 +0200150
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200151 return 0;
152}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200153
154int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
155 struct drm_connector *connector)
156{
157 int err;
158
Noralf Trønnesc7779902017-10-30 16:39:37 +0100159 if (!fb_helper)
160 return 0;
161
Thierry Redinge9827d82017-07-04 17:18:23 +0200162 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200163 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200164 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200165
166 return err;
167}
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200168EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
169
Daniel Vetter207fd322013-01-20 22:13:14 +0100170/**
171 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
172 * emulation helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100173 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
Daniel Vetter207fd322013-01-20 22:13:14 +0100174 *
175 * This functions adds all the available connectors for use with the given
176 * fb_helper. This is a separate step to allow drivers to freely assign
177 * connectors to the fbdev, e.g. if some are reserved for special purposes or
178 * not adequate to be used for the fbcon.
179 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200180 * This function is protected against concurrent connector hotadds/removals
181 * using drm_fb_helper_add_one_connector() and
182 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100183 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000184int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000185{
Gustavo A. R. Silva89f3f352017-12-05 11:46:28 -0600186 struct drm_device *dev;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000187 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100188 struct drm_connector_list_iter conn_iter;
189 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000190
Noralf Trønnesc7779902017-10-30 16:39:37 +0100191 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200192 return 0;
193
Gustavo A. R. Silva89f3f352017-12-05 11:46:28 -0600194 dev = fb_helper->dev;
195
Thierry Redinge9827d82017-07-04 17:18:23 +0200196 mutex_lock(&fb_helper->lock);
Thierry Redingb982dab2017-02-28 15:46:43 +0100197 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100198 drm_for_each_connector_iter(connector, &conn_iter) {
Thierry Redingaf2405a2017-07-04 17:18:21 +0200199 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100200 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000201 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000202 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100203 goto out;
204
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000205fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000206 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300207 struct drm_fb_helper_connector *fb_helper_connector =
208 fb_helper->connector_info[i];
209
Thierry Redingad093602017-02-28 15:46:39 +0100210 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300211
212 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000213 fb_helper->connector_info[i] = NULL;
214 }
215 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100216out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100217 drm_connector_list_iter_end(&conn_iter);
Thierry Redinge9827d82017-07-04 17:18:23 +0200218 mutex_unlock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200219
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100220 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000221}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000222EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000223
Thierry Redingaf2405a2017-07-04 17:18:21 +0200224static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
225 struct drm_connector *connector)
Dave Airlie65c2a892014-06-05 14:01:30 +1000226{
227 struct drm_fb_helper_connector *fb_helper_connector;
228 int i, j;
229
Daniel Vetterf64c5572015-08-25 15:45:13 +0200230 if (!drm_fbdev_emulation)
231 return 0;
232
Thierry Redinge9827d82017-07-04 17:18:23 +0200233 lockdep_assert_held(&fb_helper->lock);
Dave Airlie65c2a892014-06-05 14:01:30 +1000234
Thierry Redinge9827d82017-07-04 17:18:23 +0200235 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie65c2a892014-06-05 14:01:30 +1000236 if (fb_helper->connector_info[i]->connector == connector)
237 break;
238 }
239
240 if (i == fb_helper->connector_count)
241 return -EINVAL;
242 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100243 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000244
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200245 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000246 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200247
Dave Airlie65c2a892014-06-05 14:01:30 +1000248 fb_helper->connector_count--;
249 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500250
Dave Airlie65c2a892014-06-05 14:01:30 +1000251 return 0;
252}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200253
254int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
255 struct drm_connector *connector)
256{
257 int err;
258
Noralf Trønnesc7779902017-10-30 16:39:37 +0100259 if (!fb_helper)
260 return 0;
261
Thierry Redinge9827d82017-07-04 17:18:23 +0200262 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200263 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200264 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200265
266 return err;
267}
Dave Airlie65c2a892014-06-05 14:01:30 +1000268EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
269
Jason Wessel99231022010-10-13 14:09:43 -0500270static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
271{
272 uint16_t *r_base, *g_base, *b_base;
273
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200274 if (crtc->funcs->gamma_set == NULL)
275 return;
276
Jason Wessel99231022010-10-13 14:09:43 -0500277 r_base = crtc->gamma_store;
278 g_base = r_base + crtc->gamma_size;
279 b_base = g_base + crtc->gamma_size;
280
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200281 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
282 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500283}
284
Daniel Vetter207fd322013-01-20 22:13:14 +0100285/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100286 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100287 * @info: fbdev registered by the helper
288 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500289int drm_fb_helper_debug_enter(struct fb_info *info)
290{
291 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200292 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500293 int i;
294
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500295 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
296 for (i = 0; i < helper->crtc_count; i++) {
297 struct drm_mode_set *mode_set =
298 &helper->crtc_info[i].mode_set;
299
300 if (!mode_set->crtc->enabled)
301 continue;
302
303 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100304 if (funcs->mode_set_base_atomic == NULL)
305 continue;
306
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200307 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
308 continue;
309
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500310 funcs->mode_set_base_atomic(mode_set->crtc,
311 mode_set->fb,
312 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500313 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500314 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500315 }
316 }
317
318 return 0;
319}
320EXPORT_SYMBOL(drm_fb_helper_debug_enter);
321
Daniel Vetter207fd322013-01-20 22:13:14 +0100322/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100323 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100324 * @info: fbdev registered by the helper
325 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500326int drm_fb_helper_debug_leave(struct fb_info *info)
327{
328 struct drm_fb_helper *helper = info->par;
329 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200330 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500331 struct drm_framebuffer *fb;
332 int i;
333
334 for (i = 0; i < helper->crtc_count; i++) {
335 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200336
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500337 crtc = mode_set->crtc;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200338 if (drm_drv_uses_atomic_modeset(crtc->dev))
339 continue;
340
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500341 funcs = crtc->helper_private;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200342 fb = crtc->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500343
344 if (!crtc->enabled)
345 continue;
346
347 if (!fb) {
348 DRM_ERROR("no fb to restore??\n");
349 continue;
350 }
351
Stefan Christ1b99b722016-11-14 00:03:11 +0100352 if (funcs->mode_set_base_atomic == NULL)
353 continue;
354
Jason Wessel99231022010-10-13 14:09:43 -0500355 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500356 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500357 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500358 }
359
360 return 0;
361}
362EXPORT_SYMBOL(drm_fb_helper_debug_leave);
363
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200364static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
Rob Clarkbbb1e522015-08-25 15:35:58 -0400365{
366 struct drm_device *dev = fb_helper->dev;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100367 struct drm_plane_state *plane_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400368 struct drm_plane *plane;
369 struct drm_atomic_state *state;
370 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200371 unsigned int plane_mask;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200372 struct drm_modeset_acquire_ctx ctx;
373
374 drm_modeset_acquire_init(&ctx, 0);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400375
376 state = drm_atomic_state_alloc(dev);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200377 if (!state) {
378 ret = -ENOMEM;
379 goto out_ctx;
380 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400381
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200382 state->acquire_ctx = &ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400383retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100384 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400385 drm_for_each_plane(plane, dev) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400386 plane_state = drm_atomic_get_plane_state(state, plane);
387 if (IS_ERR(plane_state)) {
388 ret = PTR_ERR(plane_state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200389 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400390 }
391
Robert Fossc2c446a2017-05-19 16:50:17 -0400392 plane_state->rotation = DRM_MODE_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400393
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100394 plane->old_fb = plane->fb;
395 plane_mask |= 1 << drm_plane_index(plane);
396
Rob Clarkbbb1e522015-08-25 15:35:58 -0400397 /* disable non-primary: */
398 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
399 continue;
400
401 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
402 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200403 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400404 }
405
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200406 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400407 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100408 struct drm_plane *primary = mode_set->crtc->primary;
409
410 /* Cannot fail as we've already gotten the plane state above */
411 plane_state = drm_atomic_get_new_plane_state(state, primary);
412 plane_state->rotation = fb_helper->crtc_info[i].rotation;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400413
414 ret = __drm_atomic_helper_set_config(mode_set, state);
415 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200416 goto out_state;
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200417
418 /*
419 * __drm_atomic_helper_set_config() sets active when a
420 * mode is set, unconditionally clear it if we force DPMS off
421 */
422 if (!active) {
423 struct drm_crtc *crtc = mode_set->crtc;
424 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
425
426 crtc_state->active = false;
427 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400428 }
429
430 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400431
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200432out_state:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100433 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700434
Rob Clarkbbb1e522015-08-25 15:35:58 -0400435 if (ret == -EDEADLK)
436 goto backoff;
437
Chris Wilson08536952016-10-14 13:18:18 +0100438 drm_atomic_state_put(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200439out_ctx:
440 drm_modeset_drop_locks(&ctx);
441 drm_modeset_acquire_fini(&ctx);
442
Rob Clarkbbb1e522015-08-25 15:35:58 -0400443 return ret;
444
445backoff:
446 drm_atomic_state_clear(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200447 drm_modeset_backoff(&ctx);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400448
449 goto retry;
450}
451
Daniel Vetter71286452017-04-03 10:33:04 +0200452static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100453{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300454 struct drm_device *dev = fb_helper->dev;
455 struct drm_plane *plane;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200456 int i, ret = 0;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100457
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200458 drm_modeset_lock_all(fb_helper->dev);
Daniel Vetter6295d602015-07-09 23:44:25 +0200459 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700460 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
461 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100462
Ville Syrjälä6686df82016-10-21 22:22:45 +0300463 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300464 drm_mode_plane_set_obj_prop(plane,
465 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400466 DRM_MODE_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530467 }
468
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100469 for (i = 0; i < fb_helper->crtc_count; i++) {
470 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300471 struct drm_crtc *crtc = mode_set->crtc;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300472
Alex Deucher03f9abb2015-09-30 14:47:37 -0400473 if (crtc->funcs->cursor_set2) {
474 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
475 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200476 goto out;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400477 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300478 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
479 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200480 goto out;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300481 }
482
Daniel Vetter2d13b672012-12-11 13:47:23 +0100483 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100484 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200485 goto out;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100486 }
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200487out:
488 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200489
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200490 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100491}
Rob Clark5ea1f752014-05-30 12:29:48 -0400492
Daniel Vetter71286452017-04-03 10:33:04 +0200493static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
494{
495 struct drm_device *dev = fb_helper->dev;
496
Daniel Vetter71286452017-04-03 10:33:04 +0200497 if (drm_drv_uses_atomic_modeset(dev))
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200498 return restore_fbdev_mode_atomic(fb_helper, true);
Daniel Vetter71286452017-04-03 10:33:04 +0200499 else
500 return restore_fbdev_mode_legacy(fb_helper);
501}
502
Rob Clark5ea1f752014-05-30 12:29:48 -0400503/**
504 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
Noralf Trønnesc7779902017-10-30 16:39:37 +0100505 * @fb_helper: driver-allocated fbdev helper, can be NULL
Rob Clark5ea1f752014-05-30 12:29:48 -0400506 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100507 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400508 * when implementing an fbcon on top of kms using this helper. This ensures that
509 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200510 *
511 * RETURNS:
512 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400513 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200514int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400515{
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200516 bool do_delayed;
517 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000518
Noralf Trønnesc7779902017-10-30 16:39:37 +0100519 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200520 return -ENODEV;
521
Daniel Vetterca91a272017-07-06 15:00:21 +0200522 if (READ_ONCE(fb_helper->deferred_setup))
523 return 0;
524
Thierry Redinge9827d82017-07-04 17:18:23 +0200525 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400526 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000527
528 do_delayed = fb_helper->delayed_hotplug;
529 if (do_delayed)
530 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200531 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000532
533 if (do_delayed)
534 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200535
Rob Clark5ea1f752014-05-30 12:29:48 -0400536 return ret;
537}
538EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100539
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200540static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
541{
542 struct drm_device *dev = fb_helper->dev;
543 struct drm_crtc *crtc;
544 int bound = 0, crtcs_bound = 0;
545
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200546 /*
547 * Sometimes user space wants everything disabled, so don't steal the
548 * display if there's a master.
549 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200550 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200551 return false;
552
553 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200554 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200555 if (crtc->primary->fb)
556 crtcs_bound++;
557 if (crtc->primary->fb == fb_helper->fb)
558 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200559 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200560 }
561
562 if (bound < crtcs_bound)
563 return false;
564
565 return true;
566}
567
568#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100569/*
570 * restore fbcon display for all kms driver's using this helper, used for sysrq
571 * and panic handling.
572 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530573static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000574{
Dave Airlie785b93e2009-08-28 15:46:53 +1000575 bool ret, error = false;
576 struct drm_fb_helper *helper;
577
578 if (list_empty(&kernel_fb_helper_list))
579 return false;
580
581 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200582 struct drm_device *dev = helper->dev;
583
584 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100585 continue;
586
Thierry Redinge9827d82017-07-04 17:18:23 +0200587 mutex_lock(&helper->lock);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200588 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100589 if (ret)
590 error = true;
Thierry Redinge9827d82017-07-04 17:18:23 +0200591 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000592 }
593 return error;
594}
595
Dave Airlie785b93e2009-08-28 15:46:53 +1000596static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
597{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100598 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200599
Daniel Vetterd21bf462013-01-20 18:09:52 +0100600 ret = drm_fb_helper_force_kernel_mode();
601 if (ret == true)
602 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000603}
604static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
605
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700606static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000607{
608 schedule_work(&drm_fb_helper_restore_work);
609}
610
611static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
612 .handler = drm_fb_helper_sysrq,
613 .help_msg = "force-fb(V)",
614 .action_msg = "Restore framebuffer console",
615};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000616#else
617static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200618#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000619
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200620static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000621{
Dave Airlie785b93e2009-08-28 15:46:53 +1000622 struct drm_device *dev = fb_helper->dev;
623 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700624 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700625 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000626
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200627 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700628 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000629 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000630
Dave Airlie8be48d92010-03-30 05:34:14 +0000631 if (!crtc->enabled)
632 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000633
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100634 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000635 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700636 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200637 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500638 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100639 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700640 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000641 }
Daniel Vetter84849902012-12-02 00:28:11 +0100642 drm_modeset_unlock_all(dev);
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200643}
644
645static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
646{
647 struct drm_fb_helper *fb_helper = info->par;
648
649 /*
650 * For each CRTC in this fb, turn the connectors on/off.
651 */
652 mutex_lock(&fb_helper->lock);
653 if (!drm_fb_helper_is_bound(fb_helper)) {
654 mutex_unlock(&fb_helper->lock);
655 return;
656 }
657
658 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
659 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
660 else
661 dpms_legacy(fb_helper, dpms_mode);
Thierry Redinge9827d82017-07-04 17:18:23 +0200662 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000663}
664
Daniel Vetter207fd322013-01-20 22:13:14 +0100665/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100666 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100667 * @blank: desired blanking state
668 * @info: fbdev registered by the helper
669 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000670int drm_fb_helper_blank(int blank, struct fb_info *info)
671{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200672 if (oops_in_progress)
673 return -EBUSY;
674
Dave Airlie785b93e2009-08-28 15:46:53 +1000675 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000676 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000677 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100678 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000679 break;
James Simmons731b5a12009-10-29 20:39:07 +0000680 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000681 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100682 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000683 break;
James Simmons731b5a12009-10-29 20:39:07 +0000684 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000685 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100686 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000687 break;
James Simmons731b5a12009-10-29 20:39:07 +0000688 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000689 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100690 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000691 break;
James Simmons731b5a12009-10-29 20:39:07 +0000692 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000693 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100694 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000695 break;
696 }
697 return 0;
698}
699EXPORT_SYMBOL(drm_fb_helper_blank);
700
Ville Syrjäläa2889602016-10-26 17:41:18 +0300701static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
702 struct drm_mode_set *modeset)
703{
704 int i;
705
706 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100707 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300708 modeset->connectors[i] = NULL;
709 }
710 modeset->num_connectors = 0;
711
712 drm_mode_destroy(helper->dev, modeset->mode);
713 modeset->mode = NULL;
714
715 /* FIXME should hold a ref? */
716 modeset->fb = NULL;
717}
718
Dave Airlie785b93e2009-08-28 15:46:53 +1000719static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
720{
721 int i;
722
Dave Airlie6e86d582016-04-27 11:24:51 +1000723 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100724 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000725 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000726 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000727 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300728
Sascha Hauera1b77362012-02-01 11:38:22 +0100729 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300730 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
731
732 drm_fb_helper_modeset_release(helper, modeset);
733 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100734 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000735 kfree(helper->crtc_info);
736}
737
Noralf Trønnescfe63422016-08-23 13:54:06 +0200738static void drm_fb_helper_resume_worker(struct work_struct *work)
739{
740 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
741 resume_work);
742
743 console_lock();
744 fb_set_suspend(helper->fbdev, 0);
745 console_unlock();
746}
747
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200748static void drm_fb_helper_dirty_work(struct work_struct *work)
749{
750 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
751 dirty_work);
752 struct drm_clip_rect *clip = &helper->dirty_clip;
753 struct drm_clip_rect clip_copy;
754 unsigned long flags;
755
756 spin_lock_irqsave(&helper->dirty_lock, flags);
757 clip_copy = *clip;
758 clip->x1 = clip->y1 = ~0;
759 clip->x2 = clip->y2 = 0;
760 spin_unlock_irqrestore(&helper->dirty_lock, flags);
761
Takashi Iwai87d3b652016-10-20 17:05:30 +0200762 /* call dirty callback only when it has been really touched */
763 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
764 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200765}
766
Daniel Vetter207fd322013-01-20 22:13:14 +0100767/**
Thierry Reding10a23102014-06-27 17:19:24 +0200768 * drm_fb_helper_prepare - setup a drm_fb_helper structure
769 * @dev: DRM device
770 * @helper: driver-allocated fbdev helper structure to set up
771 * @funcs: pointer to structure of functions associate with this helper
772 *
773 * Sets up the bare minimum to make the framebuffer helper usable. This is
774 * useful to implement race-free initialization of the polling helpers.
775 */
776void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
777 const struct drm_fb_helper_funcs *funcs)
778{
779 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200780 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200781 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200782 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
783 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200784 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200785 helper->funcs = funcs;
786 helper->dev = dev;
787}
788EXPORT_SYMBOL(drm_fb_helper_prepare);
789
790/**
Daniel Vettered84e252017-02-07 15:10:49 +0100791 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100792 * @dev: drm device
793 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100794 * @max_conn_count: max connector count
795 *
796 * This allocates the structures for the fbdev helper with the given limits.
797 * Note that this won't yet touch the hardware (through the driver interfaces)
798 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
799 * to allow driver writes more control over the exact init sequence.
800 *
Thierry Reding10a23102014-06-27 17:19:24 +0200801 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100802 *
803 * RETURNS:
804 * Zero if everything went ok, nonzero otherwise.
805 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000806int drm_fb_helper_init(struct drm_device *dev,
807 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200808 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000809{
Dave Airlie785b93e2009-08-28 15:46:53 +1000810 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200811 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000812 int i;
813
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100814 if (!drm_fbdev_emulation) {
815 dev->fb_helper = fb_helper;
Daniel Vetterf64c5572015-08-25 15:45:13 +0200816 return 0;
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100817 }
Daniel Vetterf64c5572015-08-25 15:45:13 +0200818
Xiubo Li04cfe972014-03-10 09:33:58 +0800819 if (!max_conn_count)
820 return -EINVAL;
821
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200822 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000823 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000824 return -ENOMEM;
825
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200826 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000827 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
828 if (!fb_helper->connector_info) {
829 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000830 return -ENOMEM;
831 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000832 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000833 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000834
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200835 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000836 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000837 kcalloc(max_conn_count,
838 sizeof(struct drm_connector *),
839 GFP_KERNEL);
840
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200841 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000842 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000843 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Hans de Goede8f0cb412017-11-25 20:35:50 +0100844 fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000845 }
846
847 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200848 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000849 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000850 i++;
851 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100852
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100853 dev->fb_helper = fb_helper;
854
Dave Airlie785b93e2009-08-28 15:46:53 +1000855 return 0;
856out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000857 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000858 return -ENOMEM;
859}
Dave Airlie4abe3522010-03-30 05:34:18 +0000860EXPORT_SYMBOL(drm_fb_helper_init);
861
Archit Tanejab8017d62015-07-22 14:57:56 +0530862/**
863 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
864 * @fb_helper: driver-allocated fbdev helper
865 *
866 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100867 * by the driver within the fb_probe fb_helper callback function. Drivers do not
868 * need to release the allocated fb_info structure themselves, this is
869 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530870 *
871 * RETURNS:
872 * fb_info pointer if things went okay, pointer containing error code
873 * otherwise
874 */
875struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
876{
877 struct device *dev = fb_helper->dev->dev;
878 struct fb_info *info;
879 int ret;
880
881 info = framebuffer_alloc(0, dev);
882 if (!info)
883 return ERR_PTR(-ENOMEM);
884
885 ret = fb_alloc_cmap(&info->cmap, 256, 0);
886 if (ret)
887 goto err_release;
888
889 info->apertures = alloc_apertures(1);
890 if (!info->apertures) {
891 ret = -ENOMEM;
892 goto err_free_cmap;
893 }
894
895 fb_helper->fbdev = info;
896
897 return info;
898
899err_free_cmap:
900 fb_dealloc_cmap(&info->cmap);
901err_release:
902 framebuffer_release(info);
903 return ERR_PTR(ret);
904}
905EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
906
907/**
908 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
Noralf Trønnesc7779902017-10-30 16:39:37 +0100909 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Tanejab8017d62015-07-22 14:57:56 +0530910 *
911 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100912 * framebuffer device. This must be called before releasing all resources for
913 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530914 */
915void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
916{
917 if (fb_helper && fb_helper->fbdev)
918 unregister_framebuffer(fb_helper->fbdev);
919}
920EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
921
922/**
Daniel Vettered84e252017-02-07 15:10:49 +0100923 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
Noralf Trønnesc7779902017-10-30 16:39:37 +0100924 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vettered84e252017-02-07 15:10:49 +0100925 *
926 * This cleans up all remaining resources associated with @fb_helper. Must be
927 * called after drm_fb_helper_unlink_fbi() was called.
928 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000929void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
930{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100931 struct fb_info *info;
932
Noralf Trønnes29ad20b222017-10-30 16:39:38 +0100933 if (!fb_helper)
934 return;
935
936 fb_helper->dev->fb_helper = NULL;
937
938 if (!drm_fbdev_emulation)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200939 return;
940
Noralf Trønnesb52f09c2017-08-28 19:17:43 +0200941 cancel_work_sync(&fb_helper->resume_work);
942 cancel_work_sync(&fb_helper->dirty_work);
943
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100944 info = fb_helper->fbdev;
945 if (info) {
946 if (info->cmap.len)
947 fb_dealloc_cmap(&info->cmap);
948 framebuffer_release(info);
949 }
950 fb_helper->fbdev = NULL;
951
Chris Wilsona53ca632016-11-29 12:02:17 +0000952 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000953 if (!list_empty(&fb_helper->kernel_fb_list)) {
954 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200955 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000956 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000957 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000958 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000959
Thierry Redinge9827d82017-07-04 17:18:23 +0200960 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000961 drm_fb_helper_crtc_free(fb_helper);
962
Dave Airlie4abe3522010-03-30 05:34:18 +0000963}
964EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000965
Archit Taneja47074ab2015-07-22 14:57:57 +0530966/**
967 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
Noralf Trønnesc7779902017-10-30 16:39:37 +0100968 * @fb_helper: driver-allocated fbdev helper, can be NULL
Archit Taneja47074ab2015-07-22 14:57:57 +0530969 *
970 * A wrapper around unlink_framebuffer implemented by fbdev core
971 */
972void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
973{
974 if (fb_helper && fb_helper->fbdev)
975 unlink_framebuffer(fb_helper->fbdev);
976}
977EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
978
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200979static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
980 u32 width, u32 height)
981{
982 struct drm_fb_helper *helper = info->par;
983 struct drm_clip_rect *clip = &helper->dirty_clip;
984 unsigned long flags;
985
986 if (!helper->fb->funcs->dirty)
987 return;
988
989 spin_lock_irqsave(&helper->dirty_lock, flags);
990 clip->x1 = min_t(u32, clip->x1, x);
991 clip->y1 = min_t(u32, clip->y1, y);
992 clip->x2 = max_t(u32, clip->x2, x + width);
993 clip->y2 = max_t(u32, clip->y2, y + height);
994 spin_unlock_irqrestore(&helper->dirty_lock, flags);
995
996 schedule_work(&helper->dirty_work);
997}
998
999/**
1000 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
1001 * @info: fb_info struct pointer
1002 * @pagelist: list of dirty mmap framebuffer pages
1003 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001004 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001005 * callback function for flushing the fbdev mmap writes.
1006 */
1007void drm_fb_helper_deferred_io(struct fb_info *info,
1008 struct list_head *pagelist)
1009{
1010 unsigned long start, end, min, max;
1011 struct page *page;
1012 u32 y1, y2;
1013
1014 min = ULONG_MAX;
1015 max = 0;
1016 list_for_each_entry(page, pagelist, lru) {
1017 start = page->index << PAGE_SHIFT;
1018 end = start + PAGE_SIZE - 1;
1019 min = min(min, start);
1020 max = max(max, end);
1021 }
1022
1023 if (min < max) {
1024 y1 = min / info->fix.line_length;
1025 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1026 info->var.yres);
1027 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1028 }
1029}
1030EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1031
Archit Tanejacbb1a822015-07-31 16:21:41 +05301032/**
Noralf Trønnes48c95712017-12-15 18:51:17 +01001033 * drm_fb_helper_defio_init - fbdev deferred I/O initialization
1034 * @fb_helper: driver-allocated fbdev helper
1035 *
1036 * This function allocates &fb_deferred_io, sets callback to
1037 * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init().
1038 * It should be called from the &drm_fb_helper_funcs->fb_probe callback.
1039 * drm_fb_helper_fbdev_teardown() cleans up deferred I/O.
1040 *
1041 * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done
1042 * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby
1043 * affect other instances of that &fb_ops.
1044 *
1045 * Returns:
1046 * 0 on success or a negative error code on failure.
1047 */
1048int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper)
1049{
1050 struct fb_info *info = fb_helper->fbdev;
1051 struct fb_deferred_io *fbdefio;
1052 struct fb_ops *fbops;
1053
1054 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
1055 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
1056 if (!fbdefio || !fbops) {
1057 kfree(fbdefio);
1058 kfree(fbops);
1059 return -ENOMEM;
1060 }
1061
1062 info->fbdefio = fbdefio;
1063 fbdefio->delay = msecs_to_jiffies(50);
1064 fbdefio->deferred_io = drm_fb_helper_deferred_io;
1065
1066 *fbops = *info->fbops;
1067 info->fbops = fbops;
1068
1069 fb_deferred_io_init(info);
1070
1071 return 0;
1072}
1073EXPORT_SYMBOL(drm_fb_helper_defio_init);
1074
1075/**
Archit Tanejacbb1a822015-07-31 16:21:41 +05301076 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1077 * @info: fb_info struct pointer
1078 * @buf: userspace buffer to read from framebuffer memory
1079 * @count: number of bytes to read from framebuffer memory
1080 * @ppos: read offset within framebuffer memory
1081 *
1082 * A wrapper around fb_sys_read implemented by fbdev core
1083 */
1084ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1085 size_t count, loff_t *ppos)
1086{
1087 return fb_sys_read(info, buf, count, ppos);
1088}
1089EXPORT_SYMBOL(drm_fb_helper_sys_read);
1090
1091/**
1092 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1093 * @info: fb_info struct pointer
1094 * @buf: userspace buffer to write to framebuffer memory
1095 * @count: number of bytes to write to framebuffer memory
1096 * @ppos: write offset within framebuffer memory
1097 *
1098 * A wrapper around fb_sys_write implemented by fbdev core
1099 */
1100ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1101 size_t count, loff_t *ppos)
1102{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001103 ssize_t ret;
1104
1105 ret = fb_sys_write(info, buf, count, ppos);
1106 if (ret > 0)
1107 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1108 info->var.yres);
1109
1110 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301111}
1112EXPORT_SYMBOL(drm_fb_helper_sys_write);
1113
Archit Taneja742547b2015-07-31 16:21:42 +05301114/**
1115 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1116 * @info: fbdev registered by the helper
1117 * @rect: info about rectangle to fill
1118 *
1119 * A wrapper around sys_fillrect implemented by fbdev core
1120 */
1121void drm_fb_helper_sys_fillrect(struct fb_info *info,
1122 const struct fb_fillrect *rect)
1123{
1124 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001125 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1126 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301127}
1128EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1129
1130/**
1131 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1132 * @info: fbdev registered by the helper
1133 * @area: info about area to copy
1134 *
1135 * A wrapper around sys_copyarea implemented by fbdev core
1136 */
1137void drm_fb_helper_sys_copyarea(struct fb_info *info,
1138 const struct fb_copyarea *area)
1139{
1140 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001141 drm_fb_helper_dirty(info, area->dx, area->dy,
1142 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301143}
1144EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1145
1146/**
1147 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1148 * @info: fbdev registered by the helper
1149 * @image: info about image to blit
1150 *
1151 * A wrapper around sys_imageblit implemented by fbdev core
1152 */
1153void drm_fb_helper_sys_imageblit(struct fb_info *info,
1154 const struct fb_image *image)
1155{
1156 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001157 drm_fb_helper_dirty(info, image->dx, image->dy,
1158 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301159}
1160EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1161
1162/**
1163 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1164 * @info: fbdev registered by the helper
1165 * @rect: info about rectangle to fill
1166 *
1167 * A wrapper around cfb_imageblit implemented by fbdev core
1168 */
1169void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1170 const struct fb_fillrect *rect)
1171{
1172 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001173 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1174 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301175}
1176EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1177
1178/**
1179 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1180 * @info: fbdev registered by the helper
1181 * @area: info about area to copy
1182 *
1183 * A wrapper around cfb_copyarea implemented by fbdev core
1184 */
1185void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1186 const struct fb_copyarea *area)
1187{
1188 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001189 drm_fb_helper_dirty(info, area->dx, area->dy,
1190 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301191}
1192EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1193
1194/**
1195 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1196 * @info: fbdev registered by the helper
1197 * @image: info about image to blit
1198 *
1199 * A wrapper around cfb_imageblit implemented by fbdev core
1200 */
1201void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1202 const struct fb_image *image)
1203{
1204 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001205 drm_fb_helper_dirty(info, image->dx, image->dy,
1206 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301207}
1208EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1209
Archit Tanejafdefa582015-07-31 16:21:43 +05301210/**
1211 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
Noralf Trønnesc7779902017-10-30 16:39:37 +01001212 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001213 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301214 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001215 * A wrapper around fb_set_suspend implemented by fbdev core.
1216 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1217 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301218 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001219void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301220{
1221 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001222 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301223}
1224EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1225
Noralf Trønnescfe63422016-08-23 13:54:06 +02001226/**
1227 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1228 * takes the console lock
Noralf Trønnesc7779902017-10-30 16:39:37 +01001229 * @fb_helper: driver-allocated fbdev helper, can be NULL
Daniel Vetter28579f32016-08-23 17:27:27 +02001230 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001231 *
1232 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1233 * isn't available on resume, a worker is tasked with waiting for the lock
1234 * to become available. The console lock can be pretty contented on resume
1235 * due to all the printk activity.
1236 *
1237 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001238 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001239 *
1240 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1241 */
1242void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001243 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001244{
1245 if (!fb_helper || !fb_helper->fbdev)
1246 return;
1247
1248 /* make sure there's no pending/ongoing resume */
1249 flush_work(&fb_helper->resume_work);
1250
1251 if (suspend) {
1252 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1253 return;
1254
1255 console_lock();
1256
1257 } else {
1258 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1259 return;
1260
1261 if (!console_trylock()) {
1262 schedule_work(&fb_helper->resume_work);
1263 return;
1264 }
1265 }
1266
1267 fb_set_suspend(fb_helper->fbdev, suspend);
1268 console_unlock();
1269}
1270EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1271
Peter Rosinb8e2b012017-07-04 12:36:57 +02001272static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1273{
1274 u32 *palette = (u32 *)info->pseudo_palette;
1275 int i;
1276
1277 if (cmap->start + cmap->len > 16)
1278 return -EINVAL;
1279
1280 for (i = 0; i < cmap->len; ++i) {
1281 u16 red = cmap->red[i];
1282 u16 green = cmap->green[i];
1283 u16 blue = cmap->blue[i];
1284 u32 value;
1285
1286 red >>= 16 - info->var.red.length;
1287 green >>= 16 - info->var.green.length;
1288 blue >>= 16 - info->var.blue.length;
1289 value = (red << info->var.red.offset) |
1290 (green << info->var.green.offset) |
1291 (blue << info->var.blue.offset);
1292 if (info->var.transp.length > 0) {
1293 u32 mask = (1 << info->var.transp.length) - 1;
1294
1295 mask <<= info->var.transp.offset;
1296 value |= mask;
1297 }
1298 palette[cmap->start + i] = value;
1299 }
1300
1301 return 0;
1302}
1303
Peter Rosin964c6002017-07-13 18:25:27 +02001304static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
Dave Airlie068143d2009-10-05 09:58:02 +10001305{
1306 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie068143d2009-10-05 09:58:02 +10001307 struct drm_crtc *crtc;
Peter Rosina3562a02017-07-04 12:36:58 +02001308 u16 *r, *g, *b;
Peter Rosin964c6002017-07-13 18:25:27 +02001309 int i, ret = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001310
Peter Rosin964c6002017-07-13 18:25:27 +02001311 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001312 for (i = 0; i < fb_helper->crtc_count; i++) {
1313 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Peter Rosin964c6002017-07-13 18:25:27 +02001314 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1315 return -EINVAL;
Dave Airlie068143d2009-10-05 09:58:02 +10001316
Peter Rosin964c6002017-07-13 18:25:27 +02001317 if (cmap->start + cmap->len > crtc->gamma_size)
1318 return -EINVAL;
Peter Rosina3562a02017-07-04 12:36:58 +02001319
1320 r = crtc->gamma_store;
1321 g = r + crtc->gamma_size;
1322 b = g + crtc->gamma_size;
1323
1324 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1325 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1326 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1327
Peter Rosin964c6002017-07-13 18:25:27 +02001328 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1329 crtc->gamma_size, NULL);
1330 if (ret)
1331 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001332 }
Peter Rosin964c6002017-07-13 18:25:27 +02001333 drm_modeset_unlock_all(fb_helper->dev);
1334
1335 return ret;
1336}
1337
1338static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1339 struct fb_cmap *cmap)
1340{
1341 struct drm_device *dev = crtc->dev;
1342 struct drm_property_blob *gamma_lut;
1343 struct drm_color_lut *lut;
1344 int size = crtc->gamma_size;
1345 int i;
1346
1347 if (!size || cmap->start + cmap->len > size)
1348 return ERR_PTR(-EINVAL);
1349
1350 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1351 if (IS_ERR(gamma_lut))
1352 return gamma_lut;
1353
Ville Syrjälä11b83e32018-02-23 21:25:02 +02001354 lut = gamma_lut->data;
Peter Rosin964c6002017-07-13 18:25:27 +02001355 if (cmap->start || cmap->len != size) {
1356 u16 *r = crtc->gamma_store;
1357 u16 *g = r + crtc->gamma_size;
1358 u16 *b = g + crtc->gamma_size;
1359
1360 for (i = 0; i < cmap->start; i++) {
1361 lut[i].red = r[i];
1362 lut[i].green = g[i];
1363 lut[i].blue = b[i];
1364 }
1365 for (i = cmap->start + cmap->len; i < size; i++) {
1366 lut[i].red = r[i];
1367 lut[i].green = g[i];
1368 lut[i].blue = b[i];
1369 }
1370 }
1371
1372 for (i = 0; i < cmap->len; i++) {
1373 lut[cmap->start + i].red = cmap->red[i];
1374 lut[cmap->start + i].green = cmap->green[i];
1375 lut[cmap->start + i].blue = cmap->blue[i];
1376 }
1377
1378 return gamma_lut;
1379}
1380
1381static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1382{
1383 struct drm_fb_helper *fb_helper = info->par;
1384 struct drm_device *dev = fb_helper->dev;
1385 struct drm_property_blob *gamma_lut = NULL;
1386 struct drm_modeset_acquire_ctx ctx;
1387 struct drm_crtc_state *crtc_state;
1388 struct drm_atomic_state *state;
1389 struct drm_crtc *crtc;
1390 u16 *r, *g, *b;
1391 int i, ret = 0;
1392 bool replaced;
1393
1394 drm_modeset_acquire_init(&ctx, 0);
1395
1396 state = drm_atomic_state_alloc(dev);
1397 if (!state) {
1398 ret = -ENOMEM;
1399 goto out_ctx;
1400 }
1401
1402 state->acquire_ctx = &ctx;
1403retry:
1404 for (i = 0; i < fb_helper->crtc_count; i++) {
1405 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1406
1407 if (!gamma_lut)
1408 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1409 if (IS_ERR(gamma_lut)) {
1410 ret = PTR_ERR(gamma_lut);
1411 gamma_lut = NULL;
1412 goto out_state;
1413 }
1414
1415 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1416 if (IS_ERR(crtc_state)) {
1417 ret = PTR_ERR(crtc_state);
1418 goto out_state;
1419 }
1420
1421 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1422 NULL);
1423 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1424 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1425 gamma_lut);
1426 crtc_state->color_mgmt_changed |= replaced;
1427 }
1428
1429 ret = drm_atomic_commit(state);
1430 if (ret)
1431 goto out_state;
1432
1433 for (i = 0; i < fb_helper->crtc_count; i++) {
1434 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1435
1436 r = crtc->gamma_store;
1437 g = r + crtc->gamma_size;
1438 b = g + crtc->gamma_size;
1439
1440 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1441 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1442 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1443 }
1444
1445out_state:
1446 if (ret == -EDEADLK)
1447 goto backoff;
1448
1449 drm_property_blob_put(gamma_lut);
1450 drm_atomic_state_put(state);
1451out_ctx:
1452 drm_modeset_drop_locks(&ctx);
1453 drm_modeset_acquire_fini(&ctx);
1454
1455 return ret;
1456
1457backoff:
1458 drm_atomic_state_clear(state);
1459 drm_modeset_backoff(&ctx);
1460 goto retry;
1461}
1462
1463/**
1464 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1465 * @cmap: cmap to set
1466 * @info: fbdev registered by the helper
1467 */
1468int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1469{
1470 struct drm_fb_helper *fb_helper = info->par;
1471 int ret;
1472
1473 if (oops_in_progress)
1474 return -EBUSY;
1475
1476 mutex_lock(&fb_helper->lock);
1477
1478 if (!drm_fb_helper_is_bound(fb_helper)) {
1479 ret = -EBUSY;
1480 goto out;
1481 }
1482
1483 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1484 ret = setcmap_pseudo_palette(cmap, info);
1485 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1486 ret = setcmap_atomic(cmap, info);
1487 else
1488 ret = setcmap_legacy(cmap, info);
1489
1490out:
Thierry Redinge9827d82017-07-04 17:18:23 +02001491 mutex_unlock(&fb_helper->lock);
Peter Rosin964c6002017-07-13 18:25:27 +02001492
1493 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001494}
1495EXPORT_SYMBOL(drm_fb_helper_setcmap);
1496
Daniel Vetter207fd322013-01-20 22:13:14 +01001497/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001498 * drm_fb_helper_ioctl - legacy ioctl implementation
1499 * @info: fbdev registered by the helper
1500 * @cmd: ioctl command
1501 * @arg: ioctl argument
1502 *
1503 * A helper to implement the standard fbdev ioctl. Only
1504 * FBIO_WAITFORVSYNC is implemented for now.
1505 */
1506int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1507 unsigned long arg)
1508{
1509 struct drm_fb_helper *fb_helper = info->par;
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001510 struct drm_mode_set *mode_set;
1511 struct drm_crtc *crtc;
1512 int ret = 0;
1513
Thierry Redinge9827d82017-07-04 17:18:23 +02001514 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001515 if (!drm_fb_helper_is_bound(fb_helper)) {
1516 ret = -EBUSY;
1517 goto unlock;
1518 }
1519
1520 switch (cmd) {
1521 case FBIO_WAITFORVSYNC:
1522 /*
1523 * Only consider the first CRTC.
1524 *
1525 * This ioctl is supposed to take the CRTC number as
1526 * an argument, but in fbdev times, what that number
1527 * was supposed to be was quite unclear, different
1528 * drivers were passing that argument differently
1529 * (some by reference, some by value), and most of the
1530 * userspace applications were just hardcoding 0 as an
1531 * argument.
1532 *
1533 * The first CRTC should be the integrated panel on
1534 * most drivers, so this is the best choice we can
1535 * make. If we're not smart enough here, one should
1536 * just consider switch the userspace to KMS.
1537 */
1538 mode_set = &fb_helper->crtc_info[0].mode_set;
1539 crtc = mode_set->crtc;
1540
1541 /*
1542 * Only wait for a vblank event if the CRTC is
1543 * enabled, otherwise just don't do anythintg,
1544 * not even report an error.
1545 */
1546 ret = drm_crtc_vblank_get(crtc);
1547 if (!ret) {
1548 drm_crtc_wait_one_vblank(crtc);
1549 drm_crtc_vblank_put(crtc);
1550 }
1551
1552 ret = 0;
1553 goto unlock;
1554 default:
1555 ret = -ENOTTY;
1556 }
1557
1558unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001559 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001560 return ret;
1561}
1562EXPORT_SYMBOL(drm_fb_helper_ioctl);
1563
1564/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001565 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001566 * @var: screeninfo to check
1567 * @info: fbdev registered by the helper
1568 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001569int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1570 struct fb_info *info)
1571{
1572 struct drm_fb_helper *fb_helper = info->par;
1573 struct drm_framebuffer *fb = fb_helper->fb;
1574 int depth;
1575
Jason Wesself90ebd92010-08-05 09:22:32 -05001576 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001577 return -EINVAL;
1578
Stefan Agner865afb12016-10-11 16:15:04 -07001579 /*
1580 * Changes struct fb_var_screeninfo are currently not pushed back
1581 * to KMS, hence fail if different settings are requested.
1582 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001583 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001584 var->xres > fb->width || var->yres > fb->height ||
1585 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1586 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001587 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1588 var->xres, var->yres, var->bits_per_pixel,
1589 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001590 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001591 return -EINVAL;
1592 }
1593
1594 switch (var->bits_per_pixel) {
1595 case 16:
1596 depth = (var->green.length == 6) ? 16 : 15;
1597 break;
1598 case 32:
1599 depth = (var->transp.length > 0) ? 32 : 24;
1600 break;
1601 default:
1602 depth = var->bits_per_pixel;
1603 break;
1604 }
1605
1606 switch (depth) {
1607 case 8:
1608 var->red.offset = 0;
1609 var->green.offset = 0;
1610 var->blue.offset = 0;
1611 var->red.length = 8;
1612 var->green.length = 8;
1613 var->blue.length = 8;
1614 var->transp.length = 0;
1615 var->transp.offset = 0;
1616 break;
1617 case 15:
1618 var->red.offset = 10;
1619 var->green.offset = 5;
1620 var->blue.offset = 0;
1621 var->red.length = 5;
1622 var->green.length = 5;
1623 var->blue.length = 5;
1624 var->transp.length = 1;
1625 var->transp.offset = 15;
1626 break;
1627 case 16:
1628 var->red.offset = 11;
1629 var->green.offset = 5;
1630 var->blue.offset = 0;
1631 var->red.length = 5;
1632 var->green.length = 6;
1633 var->blue.length = 5;
1634 var->transp.length = 0;
1635 var->transp.offset = 0;
1636 break;
1637 case 24:
1638 var->red.offset = 16;
1639 var->green.offset = 8;
1640 var->blue.offset = 0;
1641 var->red.length = 8;
1642 var->green.length = 8;
1643 var->blue.length = 8;
1644 var->transp.length = 0;
1645 var->transp.offset = 0;
1646 break;
1647 case 32:
1648 var->red.offset = 16;
1649 var->green.offset = 8;
1650 var->blue.offset = 0;
1651 var->red.length = 8;
1652 var->green.length = 8;
1653 var->blue.length = 8;
1654 var->transp.length = 8;
1655 var->transp.offset = 24;
1656 break;
1657 default:
1658 return -EINVAL;
1659 }
1660 return 0;
1661}
1662EXPORT_SYMBOL(drm_fb_helper_check_var);
1663
Daniel Vetter207fd322013-01-20 22:13:14 +01001664/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001665 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001666 * @info: fbdev registered by the helper
1667 *
1668 * This will let fbcon do the mode init and is called at initialization time by
1669 * the fbdev core when registering the driver, and later on through the hotplug
1670 * callback.
1671 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001672int drm_fb_helper_set_par(struct fb_info *info)
1673{
1674 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001675 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001676
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001677 if (oops_in_progress)
1678 return -EBUSY;
1679
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001680 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001681 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001682 return -EINVAL;
1683 }
1684
Rob Clark5ea1f752014-05-30 12:29:48 -04001685 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001686
Dave Airlie785b93e2009-08-28 15:46:53 +10001687 return 0;
1688}
1689EXPORT_SYMBOL(drm_fb_helper_set_par);
1690
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001691static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
Rob Clark1edf0262015-08-25 15:35:59 -04001692{
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001693 int i;
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001694
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001695 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001696 struct drm_mode_set *mode_set;
1697
1698 mode_set = &fb_helper->crtc_info[i].mode_set;
1699
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001700 mode_set->x = x;
1701 mode_set->y = y;
Rob Clark1edf0262015-08-25 15:35:59 -04001702 }
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001703}
Rob Clark1edf0262015-08-25 15:35:59 -04001704
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001705static int pan_display_atomic(struct fb_var_screeninfo *var,
1706 struct fb_info *info)
1707{
1708 struct drm_fb_helper *fb_helper = info->par;
1709 int ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001710
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001711 pan_set(fb_helper, var->xoffset, var->yoffset);
Rob Clark1edf0262015-08-25 15:35:59 -04001712
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001713 ret = restore_fbdev_mode_atomic(fb_helper, true);
1714 if (!ret) {
1715 info->var.xoffset = var->xoffset;
1716 info->var.yoffset = var->yoffset;
1717 } else
1718 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001719
Rob Clark1edf0262015-08-25 15:35:59 -04001720 return ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001721}
1722
Daniel Vetter71286452017-04-03 10:33:04 +02001723static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001724 struct fb_info *info)
1725{
1726 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001727 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001728 int ret = 0;
1729 int i;
1730
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001731 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001732 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001733 modeset = &fb_helper->crtc_info[i].mode_set;
1734
1735 modeset->x = var->xoffset;
1736 modeset->y = var->yoffset;
1737
1738 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001739 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001740 if (!ret) {
1741 info->var.xoffset = var->xoffset;
1742 info->var.yoffset = var->yoffset;
1743 }
1744 }
1745 }
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001746 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001747
1748 return ret;
1749}
1750
1751/**
1752 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1753 * @var: updated screen information
1754 * @info: fbdev registered by the helper
1755 */
1756int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1757 struct fb_info *info)
1758{
1759 struct drm_fb_helper *fb_helper = info->par;
1760 struct drm_device *dev = fb_helper->dev;
1761 int ret;
1762
1763 if (oops_in_progress)
1764 return -EBUSY;
1765
Thierry Redinge9827d82017-07-04 17:18:23 +02001766 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001767 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001768 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001769 return -EBUSY;
1770 }
1771
1772 if (drm_drv_uses_atomic_modeset(dev))
1773 ret = pan_display_atomic(var, info);
1774 else
1775 ret = pan_display_legacy(var, info);
Thierry Redinge9827d82017-07-04 17:18:23 +02001776 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001777
Dave Airlie785b93e2009-08-28 15:46:53 +10001778 return ret;
1779}
1780EXPORT_SYMBOL(drm_fb_helper_pan_display);
1781
Daniel Vetter8acf6582013-01-21 23:38:37 +01001782/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001783 * Allocates the backing storage and sets up the fbdev info structure through
Daniel Vetterca91a272017-07-06 15:00:21 +02001784 * the ->fb_probe callback.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001785 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001786static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1787 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001788{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001789 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001790 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001791 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001792 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001793 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001794
1795 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1796 sizes.surface_depth = 24;
1797 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001798 sizes.fb_width = (u32)-1;
1799 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001800
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001801 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001802 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001803 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001804
Dave Airlie785b93e2009-08-28 15:46:53 +10001805 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001806 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001807 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001808 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001809
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001810 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001811
1812 if (cmdline_mode->bpp_specified) {
1813 switch (cmdline_mode->bpp) {
1814 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001815 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001816 break;
1817 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001818 sizes.surface_depth = 15;
1819 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001820 break;
1821 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001822 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001823 break;
1824 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001825 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001826 break;
1827 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001828 sizes.surface_depth = 24;
1829 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001830 break;
1831 }
1832 break;
1833 }
1834 }
1835
Dave Airlie8be48d92010-03-30 05:34:14 +00001836 crtc_count = 0;
1837 for (i = 0; i < fb_helper->crtc_count; i++) {
1838 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001839 struct drm_mode_set *mode_set;
1840 int x, y, j;
1841 /* in case of tile group, are we the last tile vert or horiz?
1842 * If no tile group you are always the last one both vertically
1843 * and horizontally
1844 */
1845 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001846
Dave Airlie8be48d92010-03-30 05:34:14 +00001847 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001848 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001849
1850 if (!desired_mode)
1851 continue;
1852
1853 crtc_count++;
1854
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001855 x = fb_helper->crtc_info[i].x;
1856 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001857
1858 if (gamma_size == 0)
1859 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1860
1861 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1862 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001863
1864 for (j = 0; j < mode_set->num_connectors; j++) {
1865 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001866
Rob Clark0e3704c2015-03-11 10:23:14 -04001867 if (connector->has_tile) {
1868 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1869 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1870 /* cloning to multiple tiles is just crazy-talk, so: */
1871 break;
1872 }
1873 }
1874
1875 if (lasth)
1876 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1877 if (lastv)
1878 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001879 }
1880
Dave Airlie38651672010-03-30 05:34:13 +00001881 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Daniel Vetterca91a272017-07-06 15:00:21 +02001882 DRM_INFO("Cannot find any crtc or sizes\n");
Maarten Lankhorst52dd0652017-11-28 12:16:03 +01001883
1884 /* First time: disable all crtc's.. */
1885 if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
1886 restore_fbdev_mode(fb_helper);
Daniel Vetterca91a272017-07-06 15:00:21 +02001887 return -EAGAIN;
Dave Airlie785b93e2009-08-28 15:46:53 +10001888 }
1889
Xinliang Liu5f152572017-02-15 17:19:08 +01001890 /* Handle our overallocation */
1891 sizes.surface_height *= drm_fbdev_overalloc;
1892 sizes.surface_height /= 100;
1893
Dave Airlie38651672010-03-30 05:34:13 +00001894 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001895 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1896 if (ret < 0)
1897 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001898
Maarten Lankhorst8d44e9e2017-12-20 10:35:44 +01001899 strcpy(fb_helper->fb->comm, "[fbcon]");
Dave Airlie785b93e2009-08-28 15:46:53 +10001900 return 0;
1901}
Dave Airlie785b93e2009-08-28 15:46:53 +10001902
Daniel Vetter207fd322013-01-20 22:13:14 +01001903/**
1904 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1905 * @info: fbdev registered by the helper
1906 * @pitch: desired pitch
1907 * @depth: desired depth
1908 *
1909 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1910 * fbdev emulations. Drivers which support acceleration methods which impose
1911 * additional constraints need to set up their own limits.
1912 *
1913 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001914 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001915 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001916void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1917 uint32_t depth)
1918{
1919 info->fix.type = FB_TYPE_PACKED_PIXELS;
1920 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1921 FB_VISUAL_TRUECOLOR;
1922 info->fix.mmio_start = 0;
1923 info->fix.mmio_len = 0;
1924 info->fix.type_aux = 0;
1925 info->fix.xpanstep = 1; /* doing it in hw */
1926 info->fix.ypanstep = 1; /* doing it in hw */
1927 info->fix.ywrapstep = 0;
1928 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001929
1930 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001931}
1932EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1933
Daniel Vetter207fd322013-01-20 22:13:14 +01001934/**
1935 * drm_fb_helper_fill_var - initalizes variable fbdev information
1936 * @info: fbdev instance to set up
1937 * @fb_helper: fb helper instance to use as template
1938 * @fb_width: desired fb width
1939 * @fb_height: desired fb height
1940 *
1941 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001942 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001943 *
1944 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001945 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1946 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001947 */
Dave Airlie38651672010-03-30 05:34:13 +00001948void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001949 uint32_t fb_width, uint32_t fb_height)
1950{
Dave Airlie38651672010-03-30 05:34:13 +00001951 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001952
Dave Airlie38651672010-03-30 05:34:13 +00001953 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001954 info->var.xres_virtual = fb->width;
1955 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001956 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001957 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001958 info->var.xoffset = 0;
1959 info->var.yoffset = 0;
1960 info->var.activate = FB_ACTIVATE_NOW;
Dave Airlie785b93e2009-08-28 15:46:53 +10001961
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001962 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001963 case 8:
1964 info->var.red.offset = 0;
1965 info->var.green.offset = 0;
1966 info->var.blue.offset = 0;
1967 info->var.red.length = 8; /* 8bit DAC */
1968 info->var.green.length = 8;
1969 info->var.blue.length = 8;
1970 info->var.transp.offset = 0;
1971 info->var.transp.length = 0;
1972 break;
1973 case 15:
1974 info->var.red.offset = 10;
1975 info->var.green.offset = 5;
1976 info->var.blue.offset = 0;
1977 info->var.red.length = 5;
1978 info->var.green.length = 5;
1979 info->var.blue.length = 5;
1980 info->var.transp.offset = 15;
1981 info->var.transp.length = 1;
1982 break;
1983 case 16:
1984 info->var.red.offset = 11;
1985 info->var.green.offset = 5;
1986 info->var.blue.offset = 0;
1987 info->var.red.length = 5;
1988 info->var.green.length = 6;
1989 info->var.blue.length = 5;
1990 info->var.transp.offset = 0;
1991 break;
1992 case 24:
1993 info->var.red.offset = 16;
1994 info->var.green.offset = 8;
1995 info->var.blue.offset = 0;
1996 info->var.red.length = 8;
1997 info->var.green.length = 8;
1998 info->var.blue.length = 8;
1999 info->var.transp.offset = 0;
2000 info->var.transp.length = 0;
2001 break;
2002 case 32:
2003 info->var.red.offset = 16;
2004 info->var.green.offset = 8;
2005 info->var.blue.offset = 0;
2006 info->var.red.length = 8;
2007 info->var.green.length = 8;
2008 info->var.blue.length = 8;
2009 info->var.transp.offset = 24;
2010 info->var.transp.length = 8;
2011 break;
2012 default:
2013 break;
2014 }
2015
2016 info->var.xres = fb_width;
2017 info->var.yres = fb_height;
2018}
2019EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00002020
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002021static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
Daniel Vettere13a0582017-07-05 06:56:29 +02002022 uint32_t maxX,
2023 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00002024{
2025 struct drm_connector *connector;
Daniel Vettere13a0582017-07-05 06:56:29 +02002026 int i, count = 0;
Dave Airlie38651672010-03-30 05:34:13 +00002027
Chris Wilson966a6a12016-11-29 12:02:15 +00002028 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002029 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002030 count += connector->funcs->fill_modes(connector, maxX, maxY);
2031 }
2032
2033 return count;
2034}
2035
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002036struct 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 +00002037{
2038 struct drm_display_mode *mode;
2039
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002040 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01002041 if (mode->hdisplay > width ||
2042 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00002043 continue;
2044 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2045 return mode;
2046 }
2047 return NULL;
2048}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002049EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002050
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002051static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00002052{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02002053 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00002054}
2055
Vincent Abrioua09759e2017-01-06 17:44:43 +01002056struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00002057{
Chris Wilson1794d252011-04-17 07:43:32 +01002058 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002059 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01002060 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00002061
Chris Wilsoneaf99c72014-08-06 10:08:32 +02002062 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00002063 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00002064 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00002065
2066 /* attempt to find a matching mode in the list of modes
2067 * we have gotten so far, if not add a CVT mode that conforms
2068 */
2069 if (cmdline_mode->rb || cmdline_mode->margins)
2070 goto create_mode;
2071
Takashi Iwaic683f422014-03-19 14:53:13 +01002072 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002073again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002074 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00002075 /* check width/height */
2076 if (mode->hdisplay != cmdline_mode->xres ||
2077 mode->vdisplay != cmdline_mode->yres)
2078 continue;
2079
2080 if (cmdline_mode->refresh_specified) {
2081 if (mode->vrefresh != cmdline_mode->refresh)
2082 continue;
2083 }
2084
2085 if (cmdline_mode->interlace) {
2086 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2087 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01002088 } else if (prefer_non_interlace) {
2089 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2090 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002091 }
2092 return mode;
2093 }
2094
Takashi Iwaic683f422014-03-19 14:53:13 +01002095 if (prefer_non_interlace) {
2096 prefer_non_interlace = false;
2097 goto again;
2098 }
2099
Dave Airlie38651672010-03-30 05:34:13 +00002100create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01002101 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2102 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002103 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00002104 return mode;
2105}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002106EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002107
2108static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2109{
2110 bool enable;
2111
Dave Airlieb5f05382017-10-16 05:08:39 +01002112 if (connector->display_info.non_desktop)
2113 return false;
2114
Sachin Kamat96081cd2012-11-15 03:43:30 +00002115 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00002116 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002117 else
Dave Airlie38651672010-03-30 05:34:13 +00002118 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002119
Dave Airlie38651672010-03-30 05:34:13 +00002120 return enable;
2121}
2122
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002123static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2124 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00002125{
2126 bool any_enabled = false;
2127 struct drm_connector *connector;
2128 int i = 0;
2129
Chris Wilson966a6a12016-11-29 12:02:15 +00002130 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002131 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002132 enabled[i] = drm_connector_enabled(connector, true);
2133 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
Dave Airlieb5f05382017-10-16 05:08:39 +01002134 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
2135
Dave Airlie38651672010-03-30 05:34:13 +00002136 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00002137 }
2138
2139 if (any_enabled)
2140 return;
2141
Chris Wilson966a6a12016-11-29 12:02:15 +00002142 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002143 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002144 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00002145 }
2146}
2147
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002148static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2149 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002150 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002151 bool *enabled, int width, int height)
2152{
2153 int count, i, j;
2154 bool can_clone = false;
2155 struct drm_fb_helper_connector *fb_helper_conn;
2156 struct drm_display_mode *dmt_mode, *mode;
2157
2158 /* only contemplate cloning in the single crtc case */
2159 if (fb_helper->crtc_count > 1)
2160 return false;
2161
2162 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002163 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002164 if (enabled[i])
2165 count++;
2166 }
2167
2168 /* only contemplate cloning if more than one connector is enabled */
2169 if (count <= 1)
2170 return false;
2171
2172 /* check the command line or if nothing common pick 1024x768 */
2173 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002174 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002175 if (!enabled[i])
2176 continue;
2177 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002178 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002179 if (!modes[i]) {
2180 can_clone = false;
2181 break;
2182 }
2183 for (j = 0; j < i; j++) {
2184 if (!enabled[j])
2185 continue;
2186 if (!drm_mode_equal(modes[j], modes[i]))
2187 can_clone = false;
2188 }
2189 }
2190
2191 if (can_clone) {
2192 DRM_DEBUG_KMS("can clone using command line\n");
2193 return true;
2194 }
2195
2196 /* try and find a 1024x768 mode on each connector */
2197 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002198 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002199
Chris Wilson966a6a12016-11-29 12:02:15 +00002200 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002201 if (!enabled[i])
2202 continue;
2203
2204 fb_helper_conn = fb_helper->connector_info[i];
2205 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2206 if (drm_mode_equal(mode, dmt_mode))
2207 modes[i] = mode;
2208 }
2209 if (!modes[i])
2210 can_clone = false;
2211 }
2212
2213 if (can_clone) {
2214 DRM_DEBUG_KMS("can clone using 1024x768\n");
2215 return true;
2216 }
2217 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2218 return false;
2219}
2220
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002221static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2222 struct drm_display_mode **modes,
2223 struct drm_fb_offset *offsets,
2224 int idx,
2225 int h_idx, int v_idx)
2226{
2227 struct drm_fb_helper_connector *fb_helper_conn;
2228 int i;
2229 int hoffset = 0, voffset = 0;
2230
Chris Wilson966a6a12016-11-29 12:02:15 +00002231 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002232 fb_helper_conn = fb_helper->connector_info[i];
2233 if (!fb_helper_conn->connector->has_tile)
2234 continue;
2235
2236 if (!modes[i] && (h_idx || v_idx)) {
2237 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2238 fb_helper_conn->connector->base.id);
2239 continue;
2240 }
2241 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2242 hoffset += modes[i]->hdisplay;
2243
2244 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2245 voffset += modes[i]->vdisplay;
2246 }
2247 offsets[idx].x = hoffset;
2248 offsets[idx].y = voffset;
2249 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2250 return 0;
2251}
2252
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002253static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002254 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002255 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002256 bool *enabled, int width, int height)
2257{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002258 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002259 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2260 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002261 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002262 int i;
2263
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002264retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002265 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002266 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002267
Chris Wilsonc96521e2016-11-27 17:09:10 +00002268 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002269 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002270
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002271 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002272 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002273 continue;
2274 }
2275
2276 /* first pass over all the untiled connectors */
2277 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2278 continue;
2279
2280 if (tile_pass == 1) {
2281 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2282 fb_helper_conn->connector->tile_v_loc != 0)
2283 continue;
2284
2285 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002286 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002287 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2288 /* if this tile_pass doesn't cover any of the tiles - keep going */
2289 continue;
2290
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002291 /*
2292 * find the tile offsets for this pass - need to find
2293 * all tiles left and above
2294 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002295 drm_get_tile_offsets(fb_helper, modes, offsets,
2296 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2297 }
Dave Airlie38651672010-03-30 05:34:13 +00002298 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002299 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002300
2301 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002302 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002303 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002304 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2305 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 +00002306 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002307 }
2308 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002309 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2310 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002311 break;
2312 }
2313 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2314 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002315 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002316 }
2317
2318 if ((conn_configured & mask) != mask) {
2319 tile_pass++;
2320 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002321 }
2322 return true;
2323}
2324
Dave Airlie8be48d92010-03-30 05:34:14 +00002325static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2326 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002327 struct drm_display_mode **modes,
2328 int n, int width, int height)
2329{
2330 int c, o;
2331 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002332 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002333 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002334 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002335 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002336 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002337
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002338 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002339 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002340
2341 fb_helper_conn = fb_helper->connector_info[n];
2342 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002343
2344 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002345 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002346 if (modes[n] == NULL)
2347 return best_score;
2348
Harsha Sharma4b947b1c2017-10-13 13:07:47 +05302349 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002350 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002351 if (!crtcs)
2352 return best_score;
2353
2354 my_score = 1;
2355 if (connector->status == connector_status_connected)
2356 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002357 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002358 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002359 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002360 my_score++;
2361
2362 connector_funcs = connector->helper_private;
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002363
2364 /*
2365 * If the DRM device implements atomic hooks and ->best_encoder() is
2366 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2367 * helper.
2368 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002369 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002370 !connector_funcs->best_encoder)
2371 encoder = drm_atomic_helper_best_encoder(connector);
2372 else
2373 encoder = connector_funcs->best_encoder(connector);
2374
Dave Airlie38651672010-03-30 05:34:13 +00002375 if (!encoder)
2376 goto out;
2377
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002378 /*
2379 * select a crtc for this connector and then attempt to configure
2380 * remaining connectors
2381 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002382 for (c = 0; c < fb_helper->crtc_count; c++) {
2383 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002384
Sachin Kamat96081cd2012-11-15 03:43:30 +00002385 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002386 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002387
2388 for (o = 0; o < n; o++)
2389 if (best_crtcs[o] == crtc)
2390 break;
2391
2392 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002393 /* ignore cloning unless only a single crtc */
2394 if (fb_helper->crtc_count > 1)
2395 continue;
2396
2397 if (!drm_mode_equal(modes[o], modes[n]))
2398 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002399 }
2400
2401 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002402 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2403 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002404 width, height);
2405 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002406 best_score = score;
2407 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002408 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002409 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002410 }
Dave Airlie38651672010-03-30 05:34:13 +00002411 }
2412out:
2413 kfree(crtcs);
2414 return best_score;
2415}
2416
Hans de Goede8f0cb412017-11-25 20:35:50 +01002417/*
2418 * This function checks if rotation is necessary because of panel orientation
2419 * and if it is, if it is supported.
2420 * If rotation is necessary and supported, its gets set in fb_crtc.rotation.
2421 * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets
2422 * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only
2423 * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do
2424 * the unsupported rotation.
2425 */
2426static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper,
2427 struct drm_fb_helper_crtc *fb_crtc,
2428 struct drm_connector *connector)
2429{
2430 struct drm_plane *plane = fb_crtc->mode_set.crtc->primary;
2431 uint64_t valid_mask = 0;
2432 int i, rotation;
2433
2434 fb_crtc->rotation = DRM_MODE_ROTATE_0;
2435
2436 switch (connector->display_info.panel_orientation) {
2437 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
2438 rotation = DRM_MODE_ROTATE_180;
2439 break;
2440 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
2441 rotation = DRM_MODE_ROTATE_90;
2442 break;
2443 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
2444 rotation = DRM_MODE_ROTATE_270;
2445 break;
2446 default:
2447 rotation = DRM_MODE_ROTATE_0;
2448 }
2449
2450 /*
2451 * TODO: support 90 / 270 degree hardware rotation,
2452 * depending on the hardware this may require the framebuffer
2453 * to be in a specific tiling format.
2454 */
2455 if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) {
2456 fb_helper->sw_rotations |= rotation;
2457 return;
2458 }
2459
2460 for (i = 0; i < plane->rotation_property->num_values; i++)
2461 valid_mask |= (1ULL << plane->rotation_property->values[i]);
2462
2463 if (!(rotation & valid_mask)) {
2464 fb_helper->sw_rotations |= rotation;
2465 return;
2466 }
2467
2468 fb_crtc->rotation = rotation;
2469 /* Rotating in hardware, fbcon should not rotate */
2470 fb_helper->sw_rotations |= DRM_MODE_ROTATE_0;
2471}
2472
Chris Wilson64e94402016-11-29 12:02:16 +00002473static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2474 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002475{
Dave Airlie8be48d92010-03-30 05:34:14 +00002476 struct drm_device *dev = fb_helper->dev;
2477 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002478 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002479 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002480 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002481 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002482
2483 DRM_DEBUG_KMS("\n");
Chris Wilson966a6a12016-11-29 12:02:15 +00002484 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002485 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002486
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002487 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002488 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002489 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002490 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002491 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002492 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002493 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002494 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002495 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002496 DRM_ERROR("Memory allocation failed\n");
2497 goto out;
2498 }
2499
Daniel Vettere13a0582017-07-05 06:56:29 +02002500 mutex_lock(&fb_helper->dev->mode_config.mutex);
2501 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2502 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002503 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002504
Jesse Barnes11e17a02013-02-19 13:31:39 -08002505 if (!(fb_helper->funcs->initial_config &&
2506 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002507 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002508 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002509 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2510 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2511 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002512
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002513 if (!drm_target_cloned(fb_helper, modes, offsets,
2514 enabled, width, height) &&
2515 !drm_target_preferred(fb_helper, modes, offsets,
2516 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002517 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002518
2519 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2520 width, height);
2521
2522 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002523 }
Daniel Vettere13a0582017-07-05 06:56:29 +02002524 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002525
Dave Airlie8be48d92010-03-30 05:34:14 +00002526 /* need to set the modesets up here for use later */
2527 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002528 for (i = 0; i < fb_helper->crtc_count; i++)
2529 drm_fb_helper_modeset_release(fb_helper,
2530 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002531
Hans de Goede8f0cb412017-11-25 20:35:50 +01002532 fb_helper->sw_rotations = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002533 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002534 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002535 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002536 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie38651672010-03-30 05:34:13 +00002537
Dave Airlie8be48d92010-03-30 05:34:14 +00002538 if (mode && fb_crtc) {
David Lechner27a061f2017-08-02 13:00:13 -05002539 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Ville Syrjäläa2889602016-10-26 17:41:18 +03002540 struct drm_connector *connector =
2541 fb_helper->connector_info[i]->connector;
2542
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002543 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2544 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002545
Dave Airlie8be48d92010-03-30 05:34:14 +00002546 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002547 fb_crtc->x = offset->x;
2548 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002549 modeset->mode = drm_mode_duplicate(dev,
2550 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002551 drm_connector_get(connector);
Hans de Goede8f0cb412017-11-25 20:35:50 +01002552 drm_setup_crtc_rotation(fb_helper, fb_crtc, connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002553 modeset->connectors[modeset->num_connectors++] = connector;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002554 modeset->x = offset->x;
2555 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002556 }
Dave Airlie38651672010-03-30 05:34:13 +00002557 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002558out:
Dave Airlie38651672010-03-30 05:34:13 +00002559 kfree(crtcs);
2560 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002561 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002562 kfree(enabled);
2563}
2564
David Lechnerf461bd22017-08-03 11:19:08 -05002565/*
2566 * This is a continuation of drm_setup_crtcs() that sets up anything related
2567 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2568 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2569 * So, any setup that touches those fields needs to be done here instead of in
2570 * drm_setup_crtcs().
2571 */
2572static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2573{
David Lechner991a3992017-08-04 11:25:24 -05002574 struct fb_info *info = fb_helper->fbdev;
David Lechnerf461bd22017-08-03 11:19:08 -05002575 int i;
2576
2577 for (i = 0; i < fb_helper->crtc_count; i++)
2578 if (fb_helper->crtc_info[i].mode_set.num_connectors)
2579 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
David Lechner991a3992017-08-04 11:25:24 -05002580
2581 mutex_lock(&fb_helper->dev->mode_config.mutex);
2582 drm_fb_helper_for_each_connector(fb_helper, i) {
2583 struct drm_connector *connector =
2584 fb_helper->connector_info[i]->connector;
2585
2586 /* use first connected connector for the physical dimensions */
2587 if (connector->status == connector_status_connected) {
2588 info->var.width = connector->display_info.width_mm;
2589 info->var.height = connector->display_info.height_mm;
2590 break;
2591 }
2592 }
2593 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Hans de Goede8f0cb412017-11-25 20:35:50 +01002594
2595 switch (fb_helper->sw_rotations) {
2596 case DRM_MODE_ROTATE_0:
2597 info->fbcon_rotate_hint = FB_ROTATE_UR;
2598 break;
2599 case DRM_MODE_ROTATE_90:
2600 info->fbcon_rotate_hint = FB_ROTATE_CCW;
2601 break;
2602 case DRM_MODE_ROTATE_180:
2603 info->fbcon_rotate_hint = FB_ROTATE_UD;
2604 break;
2605 case DRM_MODE_ROTATE_270:
2606 info->fbcon_rotate_hint = FB_ROTATE_CW;
2607 break;
2608 default:
2609 /*
2610 * Multiple bits are set / multiple rotations requested
2611 * fbcon cannot handle separate rotation settings per
2612 * output, so fallback to unrotated.
2613 */
2614 info->fbcon_rotate_hint = FB_ROTATE_UR;
2615 }
David Lechnerf461bd22017-08-03 11:19:08 -05002616}
2617
Daniel Vetterca91a272017-07-06 15:00:21 +02002618/* Note: Drops fb_helper->lock before returning. */
2619static int
2620__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2621 int bpp_sel)
2622{
2623 struct drm_device *dev = fb_helper->dev;
2624 struct fb_info *info;
2625 unsigned int width, height;
2626 int ret;
2627
2628 width = dev->mode_config.max_width;
2629 height = dev->mode_config.max_height;
2630
2631 drm_setup_crtcs(fb_helper, width, height);
2632 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2633 if (ret < 0) {
2634 if (ret == -EAGAIN) {
2635 fb_helper->preferred_bpp = bpp_sel;
2636 fb_helper->deferred_setup = true;
2637 ret = 0;
2638 }
2639 mutex_unlock(&fb_helper->lock);
2640
2641 return ret;
2642 }
David Lechnerf461bd22017-08-03 11:19:08 -05002643 drm_setup_crtcs_fb(fb_helper);
Daniel Vetterca91a272017-07-06 15:00:21 +02002644
2645 fb_helper->deferred_setup = false;
2646
2647 info = fb_helper->fbdev;
2648 info->var.pixclock = 0;
2649
2650 /* Need to drop locks to avoid recursive deadlock in
2651 * register_framebuffer. This is ok because the only thing left to do is
2652 * register the fbdev emulation instance in kernel_fb_helper_list. */
2653 mutex_unlock(&fb_helper->lock);
2654
2655 ret = register_framebuffer(info);
2656 if (ret < 0)
2657 return ret;
2658
2659 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2660 info->node, info->fix.id);
2661
2662 mutex_lock(&kernel_fb_helper_lock);
2663 if (list_empty(&kernel_fb_helper_list))
2664 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2665
2666 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2667 mutex_unlock(&kernel_fb_helper_lock);
2668
2669 return 0;
2670}
2671
Dave Airlie38651672010-03-30 05:34:13 +00002672/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002673 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002674 * @fb_helper: fb_helper device struct
2675 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002676 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002677 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002678 * At the moment, this is a cloned configuration across all heads with
2679 * a new framebuffer object as the backing store.
2680 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002681 * Note that this also registers the fbdev and so allows userspace to call into
2682 * the driver through the fbdev interfaces.
2683 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002684 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2685 * to let the driver allocate and initialize the fbdev info structure and the
2686 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002687 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2688 * values for the fbdev info structure.
2689 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002690 * HANG DEBUGGING:
2691 *
2692 * When you have fbcon support built-in or already loaded, this function will do
2693 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2694 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2695 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2696 * to consoles, not even serial console. This means when your driver crashes,
2697 * you will see absolutely nothing else but a system stuck in this function,
2698 * with no further output. Any kind of printk() you place within your own driver
2699 * or in the drm core modeset code will also never show up.
2700 *
2701 * Standard debug practice is to run the fbcon setup without taking the
2702 * console_lock as a hack, to be able to see backtraces and crashes on the
2703 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2704 * cmdline option.
2705 *
2706 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002707 * first modeset from userspace will crash in the same way, and is even easier
2708 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002709 * kernel cmdline option.
2710 *
Dave Airlie38651672010-03-30 05:34:13 +00002711 * RETURNS:
2712 * Zero if everything went ok, nonzero otherwise.
2713 */
Thierry Reding01934c22014-12-19 11:21:32 +01002714int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002715{
Chris Wilson966a6a12016-11-29 12:02:15 +00002716 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002717
Daniel Vetterf64c5572015-08-25 15:45:13 +02002718 if (!drm_fbdev_emulation)
2719 return 0;
2720
Thierry Redinge9827d82017-07-04 17:18:23 +02002721 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002722 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00002723
Daniel Vetterca91a272017-07-06 15:00:21 +02002724 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002725}
Dave Airlie8be48d92010-03-30 05:34:14 +00002726EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002727
Chris Wilson73943712011-04-22 11:03:57 +01002728/**
2729 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002730 * probing all the outputs attached to the fb
Noralf Trønnesc7779902017-10-30 16:39:37 +01002731 * @fb_helper: driver-allocated fbdev helper, can be NULL
Chris Wilson73943712011-04-22 11:03:57 +01002732 *
Chris Wilson73943712011-04-22 11:03:57 +01002733 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002734 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002735 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002736 * Called at runtime, takes the mode config locks to be able to check/change the
2737 * modeset configuration. Must be run from process context (which usually means
2738 * either the output polling work or a work item launched from the driver's
2739 * hotplug interrupt).
2740 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002741 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002742 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002743 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2744 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002745 *
Chris Wilson73943712011-04-22 11:03:57 +01002746 * RETURNS:
2747 * 0 on success and a non-zero error code otherwise.
2748 */
2749int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002750{
Thierry Redinge9827d82017-07-04 17:18:23 +02002751 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002752
Noralf Trønnesc7779902017-10-30 16:39:37 +01002753 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +02002754 return 0;
2755
Thierry Redinge9827d82017-07-04 17:18:23 +02002756 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002757 if (fb_helper->deferred_setup) {
2758 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
2759 fb_helper->preferred_bpp);
2760 return err;
2761 }
2762
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002763 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002764 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002765 mutex_unlock(&fb_helper->lock);
2766 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002767 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002768
Dave Airlie38651672010-03-30 05:34:13 +00002769 DRM_DEBUG_KMS("\n");
2770
Chris Wilson64e94402016-11-29 12:02:16 +00002771 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
David Lechnerf461bd22017-08-03 11:19:08 -05002772 drm_setup_crtcs_fb(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +02002773 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002774
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002775 drm_fb_helper_set_par(fb_helper->fbdev);
2776
2777 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002778}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002779EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002780
Noralf Trønnes304a4f62017-10-30 16:39:39 +01002781/**
Noralf Trønnes87412162017-12-15 18:51:14 +01002782 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
2783 * @dev: DRM device
2784 * @fb_helper: fbdev helper structure to set up
2785 * @funcs: fbdev helper functions
2786 * @preferred_bpp: Preferred bits per pixel for the device.
2787 * @dev->mode_config.preferred_depth is used if this is zero.
2788 * @max_conn_count: Maximum number of connectors.
2789 * @dev->mode_config.num_connector is used if this is zero.
2790 *
2791 * This function sets up fbdev emulation and registers fbdev for access by
2792 * userspace. If all connectors are disconnected, setup is deferred to the next
2793 * time drm_fb_helper_hotplug_event() is called.
2794 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
2795 * function.
2796 *
2797 * See also: drm_fb_helper_initial_config()
2798 *
2799 * Returns:
2800 * Zero on success or negative error code on failure.
2801 */
2802int drm_fb_helper_fbdev_setup(struct drm_device *dev,
2803 struct drm_fb_helper *fb_helper,
2804 const struct drm_fb_helper_funcs *funcs,
2805 unsigned int preferred_bpp,
2806 unsigned int max_conn_count)
2807{
2808 int ret;
2809
2810 if (!preferred_bpp)
2811 preferred_bpp = dev->mode_config.preferred_depth;
2812 if (!preferred_bpp)
2813 preferred_bpp = 32;
2814
2815 if (!max_conn_count)
2816 max_conn_count = dev->mode_config.num_connector;
2817 if (!max_conn_count) {
2818 DRM_DEV_ERROR(dev->dev, "No connectors\n");
2819 return -EINVAL;
2820 }
2821
2822 drm_fb_helper_prepare(dev, fb_helper, funcs);
2823
2824 ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
2825 if (ret < 0) {
2826 DRM_DEV_ERROR(dev->dev, "Failed to initialize fbdev helper\n");
2827 return ret;
2828 }
2829
2830 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
2831 if (ret < 0) {
2832 DRM_DEV_ERROR(dev->dev, "Failed to add connectors\n");
2833 goto err_drm_fb_helper_fini;
2834 }
2835
2836 if (!drm_drv_uses_atomic_modeset(dev))
2837 drm_helper_disable_unused_functions(dev);
2838
2839 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
2840 if (ret < 0) {
2841 DRM_DEV_ERROR(dev->dev, "Failed to set fbdev configuration\n");
2842 goto err_drm_fb_helper_fini;
2843 }
2844
2845 return 0;
2846
2847err_drm_fb_helper_fini:
2848 drm_fb_helper_fini(fb_helper);
2849
2850 return ret;
2851}
2852EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
2853
2854/**
2855 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
2856 * @dev: DRM device
2857 *
2858 * This function unregisters fbdev if not already done and cleans up the
2859 * associated resources including the &drm_framebuffer.
2860 * The driver is responsible for freeing the &drm_fb_helper structure which is
2861 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
2862 * when this function returns.
2863 *
2864 * In order to support device removal/unplug while file handles are still open,
2865 * drm_fb_helper_unregister_fbi() should be called on device removal and
2866 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
2867 * file handles are closed.
2868 */
2869void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
2870{
2871 struct drm_fb_helper *fb_helper = dev->fb_helper;
Noralf Trønnes48c95712017-12-15 18:51:17 +01002872 struct fb_ops *fbops = NULL;
Noralf Trønnes87412162017-12-15 18:51:14 +01002873
2874 if (!fb_helper)
2875 return;
2876
2877 /* Unregister if it hasn't been done already */
2878 if (fb_helper->fbdev && fb_helper->fbdev->dev)
2879 drm_fb_helper_unregister_fbi(fb_helper);
2880
Noralf Trønnes48c95712017-12-15 18:51:17 +01002881 if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) {
2882 fb_deferred_io_cleanup(fb_helper->fbdev);
2883 kfree(fb_helper->fbdev->fbdefio);
2884 fbops = fb_helper->fbdev->fbops;
2885 }
2886
Noralf Trønnes87412162017-12-15 18:51:14 +01002887 drm_fb_helper_fini(fb_helper);
Noralf Trønnes48c95712017-12-15 18:51:17 +01002888 kfree(fbops);
Noralf Trønnes87412162017-12-15 18:51:14 +01002889
2890 if (fb_helper->fb)
2891 drm_framebuffer_remove(fb_helper->fb);
2892}
2893EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
2894
2895/**
Noralf Trønnes304a4f62017-10-30 16:39:39 +01002896 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
2897 * @dev: DRM device
2898 *
2899 * This function can be used as the &drm_driver->lastclose callback for drivers
2900 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
2901 */
2902void drm_fb_helper_lastclose(struct drm_device *dev)
2903{
2904 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
2905}
2906EXPORT_SYMBOL(drm_fb_helper_lastclose);
2907
2908/**
2909 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2910 * helper for fbdev emulation
2911 * @dev: DRM device
2912 *
2913 * This function can be used as the
2914 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2915 * need to call drm_fb_helper_hotplug_event().
2916 */
2917void drm_fb_helper_output_poll_changed(struct drm_device *dev)
2918{
2919 drm_fb_helper_hotplug_event(dev->fb_helper);
2920}
2921EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
2922
David Rientjes6a108a12011-01-20 14:44:16 -08002923/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002924 * but the module doesn't depend on any fb console symbols. At least
2925 * attempt to load fbcon to avoid leaving the system without a usable console.
2926 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002927int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002928{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002929#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07002930 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06002931 struct module *fbcon;
2932
2933 mutex_lock(&module_mutex);
2934 fbcon = find_module(name);
2935 mutex_unlock(&module_mutex);
2936
2937 if (!fbcon)
2938 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002939#endif
David Fries3ce05162010-12-12 12:39:22 -06002940 return 0;
2941}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002942EXPORT_SYMBOL(drm_fb_helper_modinit);