blob: 4b968f5b9c8503dcc5998c208520b10bc34d5b4f [file] [log] [blame]
Dave Airlie785b93e2009-08-28 15:46:53 +10001/*
2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 *
6 * DRM framebuffer helper functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
29 */
Sachin Kamatd56b1b92012-11-15 03:43:29 +000030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Noralf Trønnescfe63422016-08-23 13:54:06 +020032#include <linux/console.h>
Andy Shevchenko3b40a442010-02-02 14:40:32 -080033#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100034#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_crtc_helper.h>
Rob Clarkbbb1e522015-08-25 15:35:58 -040041#include <drm/drm_atomic.h>
42#include <drm/drm_atomic_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100043
Ville Syrjälä699fbee2016-09-19 16:33:44 +030044#include "drm_crtc_helper_internal.h"
45
Daniel Vetterf64c5572015-08-25 15:45:13 +020046static bool drm_fbdev_emulation = true;
47module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48MODULE_PARM_DESC(fbdev_emulation,
49 "Enable legacy fbdev emulation [default=true]");
50
Xinliang Liu5f152572017-02-15 17:19:08 +010051static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
52module_param(drm_fbdev_overalloc, int, 0444);
53MODULE_PARM_DESC(drm_fbdev_overalloc,
54 "Overallocation of the fbdev buffer (%) [default="
55 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
56
Dave Airlie785b93e2009-08-28 15:46:53 +100057static LIST_HEAD(kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +000058static DEFINE_MUTEX(kernel_fb_helper_lock);
Dave Airlie785b93e2009-08-28 15:46:53 +100059
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010060/**
61 * DOC: fbdev helpers
62 *
63 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020064 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010065 * helper functions used by many drivers to implement the kernel mode setting
66 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010067 *
Thierry Reding10a23102014-06-27 17:19:24 +020068 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71 * default behaviour can override the third step with their own code.
Daniel Vettered84e252017-02-07 15:10:49 +010072 * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73 * unregisters using drm_fb_helper_unregister_fbi().
Daniel Vetter207fd322013-01-20 22:13:14 +010074 *
75 * At runtime drivers should restore the fbdev console by calling
Daniel Vetter6806cdf2017-01-25 07:26:43 +010076 * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77 * callback. They should also notify the fb helper code from updates to the
78 * output configuration by calling drm_fb_helper_hotplug_event(). For easier
Daniel Vetter207fd322013-01-20 22:13:14 +010079 * integration with the output polling code in drm_crtc_helper.c the modeset
Daniel Vetter6806cdf2017-01-25 07:26:43 +010080 * code provides a &drm_mode_config_funcs.output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010081 *
82 * All other functions exported by the fb helper library can be used to
83 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020084 *
85 * It is possible, though perhaps somewhat tricky, to implement race-free
86 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87 * helper must be called first to initialize the minimum required to make
88 * hotplug detection work. Drivers also need to make sure to properly set up
Daniel Vetter6806cdf2017-01-25 07:26:43 +010089 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
Thierry Reding10a23102014-06-27 17:19:24 +020090 * it is safe to enable interrupts and start processing hotplug events. At the
91 * same time, drivers should initialize all modeset objects such as CRTCs,
92 * encoders and connectors. To finish up the fbdev helper initialization, the
93 * drm_fb_helper_init() function is called. To probe for all attached displays
94 * and set up an initial configuration using the detected hardware, drivers
95 * should call drm_fb_helper_single_add_all_connectors() followed by
96 * drm_fb_helper_initial_config().
Noralf Trønneseaa434d2016-04-28 17:18:33 +020097 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +010098 * If &drm_framebuffer_funcs.dirty is set, the
Noralf Trønnes2dad5512016-05-11 18:09:17 +020099 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100100 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
Noralf Trønnes2dad5512016-05-11 18:09:17 +0200101 * away. This worker then calls the dirty() function ensuring that it will
102 * always run in process context since the fb_*() function could be running in
103 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
104 * callback it will also schedule dirty_work with the damage collected from the
105 * mmap page writes.
Daniel Vetterd0ddc0332012-11-01 14:45:17 +0100106 */
107
Chris Wilson966a6a12016-11-29 12:02:15 +0000108#define drm_fb_helper_for_each_connector(fbh, i__) \
Daniel Vettere13a0582017-07-05 06:56:29 +0200109 for (({ lockdep_assert_held(&(fbh)->lock); }), \
Chris Wilson966a6a12016-11-29 12:02:15 +0000110 i__ = 0; i__ < (fbh)->connector_count; i__++)
111
Thierry Redingaf2405a2017-07-04 17:18:21 +0200112static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
113 struct drm_connector *connector)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200114{
Thierry Reding50021ff2017-03-29 16:43:53 +0200115 struct drm_fb_helper_connector *fb_conn;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200116 struct drm_fb_helper_connector **temp;
Thierry Reding50021ff2017-03-29 16:43:53 +0200117 unsigned int count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200118
119 if (!drm_fbdev_emulation)
120 return 0;
121
Thierry Redinge9827d82017-07-04 17:18:23 +0200122 lockdep_assert_held(&fb_helper->lock);
Thierry Reding50021ff2017-03-29 16:43:53 +0200123
124 count = fb_helper->connector_count + 1;
125
126 if (count > fb_helper->connector_info_alloc_count) {
127 size_t size = count * sizeof(fb_conn);
128
129 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200130 if (!temp)
131 return -ENOMEM;
132
Thierry Reding50021ff2017-03-29 16:43:53 +0200133 fb_helper->connector_info_alloc_count = count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200134 fb_helper->connector_info = temp;
135 }
136
Thierry Reding50021ff2017-03-29 16:43:53 +0200137 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
138 if (!fb_conn)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200139 return -ENOMEM;
140
141 drm_connector_get(connector);
Thierry Reding50021ff2017-03-29 16:43:53 +0200142 fb_conn->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
Thierry Redingaf2405a2017-07-04 17:18:21 +0200144
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200145 return 0;
146}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200147
148int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
149 struct drm_connector *connector)
150{
151 int err;
152
Thierry Redinge9827d82017-07-04 17:18:23 +0200153 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200154 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200155 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200156
157 return err;
158}
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200159EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
160
Daniel Vetter207fd322013-01-20 22:13:14 +0100161/**
162 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
163 * emulation helper
164 * @fb_helper: fbdev initialized with drm_fb_helper_init
165 *
166 * This functions adds all the available connectors for use with the given
167 * fb_helper. This is a separate step to allow drivers to freely assign
168 * connectors to the fbdev, e.g. if some are reserved for special purposes or
169 * not adequate to be used for the fbcon.
170 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200171 * This function is protected against concurrent connector hotadds/removals
172 * using drm_fb_helper_add_one_connector() and
173 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100174 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000175int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000176{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000177 struct drm_device *dev = fb_helper->dev;
178 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100179 struct drm_connector_list_iter conn_iter;
180 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000181
Daniel Vetterf64c5572015-08-25 15:45:13 +0200182 if (!drm_fbdev_emulation)
183 return 0;
184
Thierry Redinge9827d82017-07-04 17:18:23 +0200185 mutex_lock(&fb_helper->lock);
Thierry Redingb982dab2017-02-28 15:46:43 +0100186 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100187 drm_for_each_connector_iter(connector, &conn_iter) {
Thierry Redingaf2405a2017-07-04 17:18:21 +0200188 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100189 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000190 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000191 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100192 goto out;
193
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000194fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000195 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300196 struct drm_fb_helper_connector *fb_helper_connector =
197 fb_helper->connector_info[i];
198
Thierry Redingad093602017-02-28 15:46:39 +0100199 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300200
201 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000202 fb_helper->connector_info[i] = NULL;
203 }
204 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100205out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100206 drm_connector_list_iter_end(&conn_iter);
Thierry Redinge9827d82017-07-04 17:18:23 +0200207 mutex_unlock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200208
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100209 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000210}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000211EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000212
Thierry Redingaf2405a2017-07-04 17:18:21 +0200213static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
214 struct drm_connector *connector)
Dave Airlie65c2a892014-06-05 14:01:30 +1000215{
216 struct drm_fb_helper_connector *fb_helper_connector;
217 int i, j;
218
Daniel Vetterf64c5572015-08-25 15:45:13 +0200219 if (!drm_fbdev_emulation)
220 return 0;
221
Thierry Redinge9827d82017-07-04 17:18:23 +0200222 lockdep_assert_held(&fb_helper->lock);
Dave Airlie65c2a892014-06-05 14:01:30 +1000223
Thierry Redinge9827d82017-07-04 17:18:23 +0200224 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie65c2a892014-06-05 14:01:30 +1000225 if (fb_helper->connector_info[i]->connector == connector)
226 break;
227 }
228
229 if (i == fb_helper->connector_count)
230 return -EINVAL;
231 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100232 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000233
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200234 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000235 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200236
Dave Airlie65c2a892014-06-05 14:01:30 +1000237 fb_helper->connector_count--;
238 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500239
Dave Airlie65c2a892014-06-05 14:01:30 +1000240 return 0;
241}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200242
243int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
244 struct drm_connector *connector)
245{
246 int err;
247
Thierry Redinge9827d82017-07-04 17:18:23 +0200248 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200249 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
Thierry Redinge9827d82017-07-04 17:18:23 +0200250 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200251
252 return err;
253}
Dave Airlie65c2a892014-06-05 14:01:30 +1000254EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
255
Jason Wessel99231022010-10-13 14:09:43 -0500256static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
257{
258 uint16_t *r_base, *g_base, *b_base;
259
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200260 if (crtc->funcs->gamma_set == NULL)
261 return;
262
Jason Wessel99231022010-10-13 14:09:43 -0500263 r_base = crtc->gamma_store;
264 g_base = r_base + crtc->gamma_size;
265 b_base = g_base + crtc->gamma_size;
266
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200267 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
268 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500269}
270
Daniel Vetter207fd322013-01-20 22:13:14 +0100271/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100272 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100273 * @info: fbdev registered by the helper
274 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500275int drm_fb_helper_debug_enter(struct fb_info *info)
276{
277 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200278 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500279 int i;
280
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500281 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
282 for (i = 0; i < helper->crtc_count; i++) {
283 struct drm_mode_set *mode_set =
284 &helper->crtc_info[i].mode_set;
285
286 if (!mode_set->crtc->enabled)
287 continue;
288
289 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100290 if (funcs->mode_set_base_atomic == NULL)
291 continue;
292
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200293 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
294 continue;
295
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500296 funcs->mode_set_base_atomic(mode_set->crtc,
297 mode_set->fb,
298 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500299 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500300 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500301 }
302 }
303
304 return 0;
305}
306EXPORT_SYMBOL(drm_fb_helper_debug_enter);
307
Daniel Vetter207fd322013-01-20 22:13:14 +0100308/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100309 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100310 * @info: fbdev registered by the helper
311 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500312int drm_fb_helper_debug_leave(struct fb_info *info)
313{
314 struct drm_fb_helper *helper = info->par;
315 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200316 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500317 struct drm_framebuffer *fb;
318 int i;
319
320 for (i = 0; i < helper->crtc_count; i++) {
321 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200322
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500323 crtc = mode_set->crtc;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200324 if (drm_drv_uses_atomic_modeset(crtc->dev))
325 continue;
326
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500327 funcs = crtc->helper_private;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200328 fb = crtc->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500329
330 if (!crtc->enabled)
331 continue;
332
333 if (!fb) {
334 DRM_ERROR("no fb to restore??\n");
335 continue;
336 }
337
Stefan Christ1b99b722016-11-14 00:03:11 +0100338 if (funcs->mode_set_base_atomic == NULL)
339 continue;
340
Jason Wessel99231022010-10-13 14:09:43 -0500341 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500342 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500343 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500344 }
345
346 return 0;
347}
348EXPORT_SYMBOL(drm_fb_helper_debug_leave);
349
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200350static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
Rob Clarkbbb1e522015-08-25 15:35:58 -0400351{
352 struct drm_device *dev = fb_helper->dev;
353 struct drm_plane *plane;
354 struct drm_atomic_state *state;
355 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200356 unsigned int plane_mask;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200357 struct drm_modeset_acquire_ctx ctx;
358
359 drm_modeset_acquire_init(&ctx, 0);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400360
361 state = drm_atomic_state_alloc(dev);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200362 if (!state) {
363 ret = -ENOMEM;
364 goto out_ctx;
365 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400366
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200367 state->acquire_ctx = &ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400368retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100369 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400370 drm_for_each_plane(plane, dev) {
371 struct drm_plane_state *plane_state;
372
373 plane_state = drm_atomic_get_plane_state(state, plane);
374 if (IS_ERR(plane_state)) {
375 ret = PTR_ERR(plane_state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200376 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400377 }
378
Robert Fossc2c446a2017-05-19 16:50:17 -0400379 plane_state->rotation = DRM_MODE_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400380
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100381 plane->old_fb = plane->fb;
382 plane_mask |= 1 << drm_plane_index(plane);
383
Rob Clarkbbb1e522015-08-25 15:35:58 -0400384 /* disable non-primary: */
385 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
386 continue;
387
388 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
389 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200390 goto out_state;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400391 }
392
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200393 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400394 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
395
396 ret = __drm_atomic_helper_set_config(mode_set, state);
397 if (ret != 0)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200398 goto out_state;
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200399
400 /*
401 * __drm_atomic_helper_set_config() sets active when a
402 * mode is set, unconditionally clear it if we force DPMS off
403 */
404 if (!active) {
405 struct drm_crtc *crtc = mode_set->crtc;
406 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
407
408 crtc_state->active = false;
409 }
Rob Clarkbbb1e522015-08-25 15:35:58 -0400410 }
411
412 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400413
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200414out_state:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100415 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700416
Rob Clarkbbb1e522015-08-25 15:35:58 -0400417 if (ret == -EDEADLK)
418 goto backoff;
419
Chris Wilson08536952016-10-14 13:18:18 +0100420 drm_atomic_state_put(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200421out_ctx:
422 drm_modeset_drop_locks(&ctx);
423 drm_modeset_acquire_fini(&ctx);
424
Rob Clarkbbb1e522015-08-25 15:35:58 -0400425 return ret;
426
427backoff:
428 drm_atomic_state_clear(state);
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200429 drm_modeset_backoff(&ctx);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400430
431 goto retry;
432}
433
Daniel Vetter71286452017-04-03 10:33:04 +0200434static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100435{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300436 struct drm_device *dev = fb_helper->dev;
437 struct drm_plane *plane;
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200438 int i, ret = 0;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100439
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200440 drm_modeset_lock_all(fb_helper->dev);
Daniel Vetter6295d602015-07-09 23:44:25 +0200441 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700442 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
443 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100444
Ville Syrjälä6686df82016-10-21 22:22:45 +0300445 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300446 drm_mode_plane_set_obj_prop(plane,
447 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400448 DRM_MODE_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530449 }
450
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100451 for (i = 0; i < fb_helper->crtc_count; i++) {
452 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300453 struct drm_crtc *crtc = mode_set->crtc;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300454
Alex Deucher03f9abb2015-09-30 14:47:37 -0400455 if (crtc->funcs->cursor_set2) {
456 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
457 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200458 goto out;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400459 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300460 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
461 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200462 goto out;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300463 }
464
Daniel Vetter2d13b672012-12-11 13:47:23 +0100465 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100466 if (ret)
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200467 goto out;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100468 }
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200469out:
470 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200471
Daniel Vetter1d0c641092017-07-04 17:18:27 +0200472 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100473}
Rob Clark5ea1f752014-05-30 12:29:48 -0400474
Daniel Vetter71286452017-04-03 10:33:04 +0200475static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
476{
477 struct drm_device *dev = fb_helper->dev;
478
Daniel Vetter71286452017-04-03 10:33:04 +0200479 if (drm_drv_uses_atomic_modeset(dev))
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200480 return restore_fbdev_mode_atomic(fb_helper, true);
Daniel Vetter71286452017-04-03 10:33:04 +0200481 else
482 return restore_fbdev_mode_legacy(fb_helper);
483}
484
Rob Clark5ea1f752014-05-30 12:29:48 -0400485/**
486 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
487 * @fb_helper: fbcon to restore
488 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100489 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400490 * when implementing an fbcon on top of kms using this helper. This ensures that
491 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200492 *
493 * RETURNS:
494 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400495 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200496int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400497{
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200498 bool do_delayed;
499 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000500
Daniel Vetterf64c5572015-08-25 15:45:13 +0200501 if (!drm_fbdev_emulation)
502 return -ENODEV;
503
Daniel Vetterca91a272017-07-06 15:00:21 +0200504 if (READ_ONCE(fb_helper->deferred_setup))
505 return 0;
506
Thierry Redinge9827d82017-07-04 17:18:23 +0200507 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400508 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000509
510 do_delayed = fb_helper->delayed_hotplug;
511 if (do_delayed)
512 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200513 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000514
515 if (do_delayed)
516 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200517
Rob Clark5ea1f752014-05-30 12:29:48 -0400518 return ret;
519}
520EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100521
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200522static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
523{
524 struct drm_device *dev = fb_helper->dev;
525 struct drm_crtc *crtc;
526 int bound = 0, crtcs_bound = 0;
527
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200528 /*
529 * Sometimes user space wants everything disabled, so don't steal the
530 * display if there's a master.
531 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200532 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200533 return false;
534
535 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200536 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200537 if (crtc->primary->fb)
538 crtcs_bound++;
539 if (crtc->primary->fb == fb_helper->fb)
540 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200541 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200542 }
543
544 if (bound < crtcs_bound)
545 return false;
546
547 return true;
548}
549
550#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100551/*
552 * restore fbcon display for all kms driver's using this helper, used for sysrq
553 * and panic handling.
554 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530555static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000556{
Dave Airlie785b93e2009-08-28 15:46:53 +1000557 bool ret, error = false;
558 struct drm_fb_helper *helper;
559
560 if (list_empty(&kernel_fb_helper_list))
561 return false;
562
563 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200564 struct drm_device *dev = helper->dev;
565
566 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100567 continue;
568
Thierry Redinge9827d82017-07-04 17:18:23 +0200569 mutex_lock(&helper->lock);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200570 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100571 if (ret)
572 error = true;
Thierry Redinge9827d82017-07-04 17:18:23 +0200573 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000574 }
575 return error;
576}
577
Dave Airlie785b93e2009-08-28 15:46:53 +1000578static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
579{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100580 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200581
Daniel Vetterd21bf462013-01-20 18:09:52 +0100582 ret = drm_fb_helper_force_kernel_mode();
583 if (ret == true)
584 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000585}
586static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
587
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700588static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000589{
590 schedule_work(&drm_fb_helper_restore_work);
591}
592
593static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
594 .handler = drm_fb_helper_sysrq,
595 .help_msg = "force-fb(V)",
596 .action_msg = "Restore framebuffer console",
597};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000598#else
599static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200600#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000601
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200602static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000603{
Dave Airlie785b93e2009-08-28 15:46:53 +1000604 struct drm_device *dev = fb_helper->dev;
605 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700606 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700607 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000608
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200609 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700610 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000611 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000612
Dave Airlie8be48d92010-03-30 05:34:14 +0000613 if (!crtc->enabled)
614 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000615
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100616 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000617 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700618 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200619 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500620 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100621 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700622 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000623 }
Daniel Vetter84849902012-12-02 00:28:11 +0100624 drm_modeset_unlock_all(dev);
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200625}
626
627static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
628{
629 struct drm_fb_helper *fb_helper = info->par;
630
631 /*
632 * For each CRTC in this fb, turn the connectors on/off.
633 */
634 mutex_lock(&fb_helper->lock);
635 if (!drm_fb_helper_is_bound(fb_helper)) {
636 mutex_unlock(&fb_helper->lock);
637 return;
638 }
639
640 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
641 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
642 else
643 dpms_legacy(fb_helper, dpms_mode);
Thierry Redinge9827d82017-07-04 17:18:23 +0200644 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000645}
646
Daniel Vetter207fd322013-01-20 22:13:14 +0100647/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100648 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100649 * @blank: desired blanking state
650 * @info: fbdev registered by the helper
651 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000652int drm_fb_helper_blank(int blank, struct fb_info *info)
653{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200654 if (oops_in_progress)
655 return -EBUSY;
656
Dave Airlie785b93e2009-08-28 15:46:53 +1000657 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000658 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000659 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100660 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000661 break;
James Simmons731b5a12009-10-29 20:39:07 +0000662 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000663 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100664 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000665 break;
James Simmons731b5a12009-10-29 20:39:07 +0000666 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000667 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100668 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000669 break;
James Simmons731b5a12009-10-29 20:39:07 +0000670 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000671 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100672 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000673 break;
James Simmons731b5a12009-10-29 20:39:07 +0000674 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000675 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100676 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000677 break;
678 }
679 return 0;
680}
681EXPORT_SYMBOL(drm_fb_helper_blank);
682
Ville Syrjäläa2889602016-10-26 17:41:18 +0300683static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
684 struct drm_mode_set *modeset)
685{
686 int i;
687
688 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100689 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300690 modeset->connectors[i] = NULL;
691 }
692 modeset->num_connectors = 0;
693
694 drm_mode_destroy(helper->dev, modeset->mode);
695 modeset->mode = NULL;
696
697 /* FIXME should hold a ref? */
698 modeset->fb = NULL;
699}
700
Dave Airlie785b93e2009-08-28 15:46:53 +1000701static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
702{
703 int i;
704
Dave Airlie6e86d582016-04-27 11:24:51 +1000705 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100706 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000707 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000708 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000709 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300710
Sascha Hauera1b77362012-02-01 11:38:22 +0100711 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300712 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
713
714 drm_fb_helper_modeset_release(helper, modeset);
715 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100716 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000717 kfree(helper->crtc_info);
718}
719
Noralf Trønnescfe63422016-08-23 13:54:06 +0200720static void drm_fb_helper_resume_worker(struct work_struct *work)
721{
722 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
723 resume_work);
724
725 console_lock();
726 fb_set_suspend(helper->fbdev, 0);
727 console_unlock();
728}
729
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200730static void drm_fb_helper_dirty_work(struct work_struct *work)
731{
732 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
733 dirty_work);
734 struct drm_clip_rect *clip = &helper->dirty_clip;
735 struct drm_clip_rect clip_copy;
736 unsigned long flags;
737
738 spin_lock_irqsave(&helper->dirty_lock, flags);
739 clip_copy = *clip;
740 clip->x1 = clip->y1 = ~0;
741 clip->x2 = clip->y2 = 0;
742 spin_unlock_irqrestore(&helper->dirty_lock, flags);
743
Takashi Iwai87d3b652016-10-20 17:05:30 +0200744 /* call dirty callback only when it has been really touched */
745 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
746 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200747}
748
Daniel Vetter207fd322013-01-20 22:13:14 +0100749/**
Thierry Reding10a23102014-06-27 17:19:24 +0200750 * drm_fb_helper_prepare - setup a drm_fb_helper structure
751 * @dev: DRM device
752 * @helper: driver-allocated fbdev helper structure to set up
753 * @funcs: pointer to structure of functions associate with this helper
754 *
755 * Sets up the bare minimum to make the framebuffer helper usable. This is
756 * useful to implement race-free initialization of the polling helpers.
757 */
758void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
759 const struct drm_fb_helper_funcs *funcs)
760{
761 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200762 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200763 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200764 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
765 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200766 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200767 helper->funcs = funcs;
768 helper->dev = dev;
769}
770EXPORT_SYMBOL(drm_fb_helper_prepare);
771
772/**
Daniel Vettered84e252017-02-07 15:10:49 +0100773 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100774 * @dev: drm device
775 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100776 * @max_conn_count: max connector count
777 *
778 * This allocates the structures for the fbdev helper with the given limits.
779 * Note that this won't yet touch the hardware (through the driver interfaces)
780 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
781 * to allow driver writes more control over the exact init sequence.
782 *
Thierry Reding10a23102014-06-27 17:19:24 +0200783 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100784 *
785 * RETURNS:
786 * Zero if everything went ok, nonzero otherwise.
787 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000788int drm_fb_helper_init(struct drm_device *dev,
789 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200790 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000791{
Dave Airlie785b93e2009-08-28 15:46:53 +1000792 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200793 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000794 int i;
795
Daniel Vetterf64c5572015-08-25 15:45:13 +0200796 if (!drm_fbdev_emulation)
797 return 0;
798
Xiubo Li04cfe972014-03-10 09:33:58 +0800799 if (!max_conn_count)
800 return -EINVAL;
801
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200802 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000803 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000804 return -ENOMEM;
805
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200806 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000807 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
808 if (!fb_helper->connector_info) {
809 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000810 return -ENOMEM;
811 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000812 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000813 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000814
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200815 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000816 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000817 kcalloc(max_conn_count,
818 sizeof(struct drm_connector *),
819 GFP_KERNEL);
820
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200821 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000822 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000823 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000824 }
825
826 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200827 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000828 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000829 i++;
830 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100831
Dave Airlie785b93e2009-08-28 15:46:53 +1000832 return 0;
833out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000834 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000835 return -ENOMEM;
836}
Dave Airlie4abe3522010-03-30 05:34:18 +0000837EXPORT_SYMBOL(drm_fb_helper_init);
838
Archit Tanejab8017d62015-07-22 14:57:56 +0530839/**
840 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
841 * @fb_helper: driver-allocated fbdev helper
842 *
843 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100844 * by the driver within the fb_probe fb_helper callback function. Drivers do not
845 * need to release the allocated fb_info structure themselves, this is
846 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530847 *
848 * RETURNS:
849 * fb_info pointer if things went okay, pointer containing error code
850 * otherwise
851 */
852struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
853{
854 struct device *dev = fb_helper->dev->dev;
855 struct fb_info *info;
856 int ret;
857
858 info = framebuffer_alloc(0, dev);
859 if (!info)
860 return ERR_PTR(-ENOMEM);
861
862 ret = fb_alloc_cmap(&info->cmap, 256, 0);
863 if (ret)
864 goto err_release;
865
866 info->apertures = alloc_apertures(1);
867 if (!info->apertures) {
868 ret = -ENOMEM;
869 goto err_free_cmap;
870 }
871
872 fb_helper->fbdev = info;
873
874 return info;
875
876err_free_cmap:
877 fb_dealloc_cmap(&info->cmap);
878err_release:
879 framebuffer_release(info);
880 return ERR_PTR(ret);
881}
882EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
883
884/**
885 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
886 * @fb_helper: driver-allocated fbdev helper
887 *
888 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100889 * framebuffer device. This must be called before releasing all resources for
890 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530891 */
892void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
893{
894 if (fb_helper && fb_helper->fbdev)
895 unregister_framebuffer(fb_helper->fbdev);
896}
897EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
898
899/**
Daniel Vettered84e252017-02-07 15:10:49 +0100900 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
901 * @fb_helper: driver-allocated fbdev helper
902 *
903 * This cleans up all remaining resources associated with @fb_helper. Must be
904 * called after drm_fb_helper_unlink_fbi() was called.
905 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000906void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
907{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100908 struct fb_info *info;
909
910 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200911 return;
912
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100913 info = fb_helper->fbdev;
914 if (info) {
915 if (info->cmap.len)
916 fb_dealloc_cmap(&info->cmap);
917 framebuffer_release(info);
918 }
919 fb_helper->fbdev = NULL;
920
Chris Wilson24f76b22017-02-07 12:49:56 +0000921 cancel_work_sync(&fb_helper->resume_work);
Chris Wilsonf21b9a92017-02-07 12:49:55 +0000922 cancel_work_sync(&fb_helper->dirty_work);
923
Chris Wilsona53ca632016-11-29 12:02:17 +0000924 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000925 if (!list_empty(&fb_helper->kernel_fb_list)) {
926 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200927 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000928 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000929 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000930 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000931
Thierry Redinge9827d82017-07-04 17:18:23 +0200932 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000933 drm_fb_helper_crtc_free(fb_helper);
934
Dave Airlie4abe3522010-03-30 05:34:18 +0000935}
936EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000937
Archit Taneja47074ab2015-07-22 14:57:57 +0530938/**
939 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
940 * @fb_helper: driver-allocated fbdev helper
941 *
942 * A wrapper around unlink_framebuffer implemented by fbdev core
943 */
944void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
945{
946 if (fb_helper && fb_helper->fbdev)
947 unlink_framebuffer(fb_helper->fbdev);
948}
949EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
950
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200951static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
952 u32 width, u32 height)
953{
954 struct drm_fb_helper *helper = info->par;
955 struct drm_clip_rect *clip = &helper->dirty_clip;
956 unsigned long flags;
957
958 if (!helper->fb->funcs->dirty)
959 return;
960
961 spin_lock_irqsave(&helper->dirty_lock, flags);
962 clip->x1 = min_t(u32, clip->x1, x);
963 clip->y1 = min_t(u32, clip->y1, y);
964 clip->x2 = max_t(u32, clip->x2, x + width);
965 clip->y2 = max_t(u32, clip->y2, y + height);
966 spin_unlock_irqrestore(&helper->dirty_lock, flags);
967
968 schedule_work(&helper->dirty_work);
969}
970
971/**
972 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
973 * @info: fb_info struct pointer
974 * @pagelist: list of dirty mmap framebuffer pages
975 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100976 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200977 * callback function for flushing the fbdev mmap writes.
978 */
979void drm_fb_helper_deferred_io(struct fb_info *info,
980 struct list_head *pagelist)
981{
982 unsigned long start, end, min, max;
983 struct page *page;
984 u32 y1, y2;
985
986 min = ULONG_MAX;
987 max = 0;
988 list_for_each_entry(page, pagelist, lru) {
989 start = page->index << PAGE_SHIFT;
990 end = start + PAGE_SIZE - 1;
991 min = min(min, start);
992 max = max(max, end);
993 }
994
995 if (min < max) {
996 y1 = min / info->fix.line_length;
997 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
998 info->var.yres);
999 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1000 }
1001}
1002EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1003
Archit Tanejacbb1a822015-07-31 16:21:41 +05301004/**
1005 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1006 * @info: fb_info struct pointer
1007 * @buf: userspace buffer to read from framebuffer memory
1008 * @count: number of bytes to read from framebuffer memory
1009 * @ppos: read offset within framebuffer memory
1010 *
1011 * A wrapper around fb_sys_read implemented by fbdev core
1012 */
1013ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1014 size_t count, loff_t *ppos)
1015{
1016 return fb_sys_read(info, buf, count, ppos);
1017}
1018EXPORT_SYMBOL(drm_fb_helper_sys_read);
1019
1020/**
1021 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1022 * @info: fb_info struct pointer
1023 * @buf: userspace buffer to write to framebuffer memory
1024 * @count: number of bytes to write to framebuffer memory
1025 * @ppos: write offset within framebuffer memory
1026 *
1027 * A wrapper around fb_sys_write implemented by fbdev core
1028 */
1029ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1030 size_t count, loff_t *ppos)
1031{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001032 ssize_t ret;
1033
1034 ret = fb_sys_write(info, buf, count, ppos);
1035 if (ret > 0)
1036 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1037 info->var.yres);
1038
1039 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301040}
1041EXPORT_SYMBOL(drm_fb_helper_sys_write);
1042
Archit Taneja742547b2015-07-31 16:21:42 +05301043/**
1044 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1045 * @info: fbdev registered by the helper
1046 * @rect: info about rectangle to fill
1047 *
1048 * A wrapper around sys_fillrect implemented by fbdev core
1049 */
1050void drm_fb_helper_sys_fillrect(struct fb_info *info,
1051 const struct fb_fillrect *rect)
1052{
1053 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001054 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1055 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301056}
1057EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1058
1059/**
1060 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1061 * @info: fbdev registered by the helper
1062 * @area: info about area to copy
1063 *
1064 * A wrapper around sys_copyarea implemented by fbdev core
1065 */
1066void drm_fb_helper_sys_copyarea(struct fb_info *info,
1067 const struct fb_copyarea *area)
1068{
1069 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001070 drm_fb_helper_dirty(info, area->dx, area->dy,
1071 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301072}
1073EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1074
1075/**
1076 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1077 * @info: fbdev registered by the helper
1078 * @image: info about image to blit
1079 *
1080 * A wrapper around sys_imageblit implemented by fbdev core
1081 */
1082void drm_fb_helper_sys_imageblit(struct fb_info *info,
1083 const struct fb_image *image)
1084{
1085 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001086 drm_fb_helper_dirty(info, image->dx, image->dy,
1087 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301088}
1089EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1090
1091/**
1092 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1093 * @info: fbdev registered by the helper
1094 * @rect: info about rectangle to fill
1095 *
1096 * A wrapper around cfb_imageblit implemented by fbdev core
1097 */
1098void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1099 const struct fb_fillrect *rect)
1100{
1101 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001102 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1103 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301104}
1105EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1106
1107/**
1108 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1109 * @info: fbdev registered by the helper
1110 * @area: info about area to copy
1111 *
1112 * A wrapper around cfb_copyarea implemented by fbdev core
1113 */
1114void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1115 const struct fb_copyarea *area)
1116{
1117 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001118 drm_fb_helper_dirty(info, area->dx, area->dy,
1119 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301120}
1121EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1122
1123/**
1124 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1125 * @info: fbdev registered by the helper
1126 * @image: info about image to blit
1127 *
1128 * A wrapper around cfb_imageblit implemented by fbdev core
1129 */
1130void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1131 const struct fb_image *image)
1132{
1133 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001134 drm_fb_helper_dirty(info, image->dx, image->dy,
1135 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301136}
1137EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1138
Archit Tanejafdefa582015-07-31 16:21:43 +05301139/**
1140 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1141 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001142 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301143 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001144 * A wrapper around fb_set_suspend implemented by fbdev core.
1145 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1146 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301147 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001148void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301149{
1150 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001151 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301152}
1153EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1154
Noralf Trønnescfe63422016-08-23 13:54:06 +02001155/**
1156 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1157 * takes the console lock
1158 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001159 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001160 *
1161 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1162 * isn't available on resume, a worker is tasked with waiting for the lock
1163 * to become available. The console lock can be pretty contented on resume
1164 * due to all the printk activity.
1165 *
1166 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001167 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001168 *
1169 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1170 */
1171void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001172 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001173{
1174 if (!fb_helper || !fb_helper->fbdev)
1175 return;
1176
1177 /* make sure there's no pending/ongoing resume */
1178 flush_work(&fb_helper->resume_work);
1179
1180 if (suspend) {
1181 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1182 return;
1183
1184 console_lock();
1185
1186 } else {
1187 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1188 return;
1189
1190 if (!console_trylock()) {
1191 schedule_work(&fb_helper->resume_work);
1192 return;
1193 }
1194 }
1195
1196 fb_set_suspend(fb_helper->fbdev, suspend);
1197 console_unlock();
1198}
1199EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1200
Peter Rosinb8e2b012017-07-04 12:36:57 +02001201static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1202{
1203 u32 *palette = (u32 *)info->pseudo_palette;
1204 int i;
1205
1206 if (cmap->start + cmap->len > 16)
1207 return -EINVAL;
1208
1209 for (i = 0; i < cmap->len; ++i) {
1210 u16 red = cmap->red[i];
1211 u16 green = cmap->green[i];
1212 u16 blue = cmap->blue[i];
1213 u32 value;
1214
1215 red >>= 16 - info->var.red.length;
1216 green >>= 16 - info->var.green.length;
1217 blue >>= 16 - info->var.blue.length;
1218 value = (red << info->var.red.offset) |
1219 (green << info->var.green.offset) |
1220 (blue << info->var.blue.offset);
1221 if (info->var.transp.length > 0) {
1222 u32 mask = (1 << info->var.transp.length) - 1;
1223
1224 mask <<= info->var.transp.offset;
1225 value |= mask;
1226 }
1227 palette[cmap->start + i] = value;
1228 }
1229
1230 return 0;
1231}
1232
Peter Rosin964c6002017-07-13 18:25:27 +02001233static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
Dave Airlie068143d2009-10-05 09:58:02 +10001234{
1235 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie068143d2009-10-05 09:58:02 +10001236 struct drm_crtc *crtc;
Peter Rosina3562a02017-07-04 12:36:58 +02001237 u16 *r, *g, *b;
Peter Rosin964c6002017-07-13 18:25:27 +02001238 int i, ret = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001239
Peter Rosin964c6002017-07-13 18:25:27 +02001240 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001241 for (i = 0; i < fb_helper->crtc_count; i++) {
1242 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Peter Rosin964c6002017-07-13 18:25:27 +02001243 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1244 return -EINVAL;
Dave Airlie068143d2009-10-05 09:58:02 +10001245
Peter Rosin964c6002017-07-13 18:25:27 +02001246 if (cmap->start + cmap->len > crtc->gamma_size)
1247 return -EINVAL;
Peter Rosina3562a02017-07-04 12:36:58 +02001248
1249 r = crtc->gamma_store;
1250 g = r + crtc->gamma_size;
1251 b = g + crtc->gamma_size;
1252
1253 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1254 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1255 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1256
Peter Rosin964c6002017-07-13 18:25:27 +02001257 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1258 crtc->gamma_size, NULL);
1259 if (ret)
1260 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001261 }
Peter Rosin964c6002017-07-13 18:25:27 +02001262 drm_modeset_unlock_all(fb_helper->dev);
1263
1264 return ret;
1265}
1266
1267static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1268 struct fb_cmap *cmap)
1269{
1270 struct drm_device *dev = crtc->dev;
1271 struct drm_property_blob *gamma_lut;
1272 struct drm_color_lut *lut;
1273 int size = crtc->gamma_size;
1274 int i;
1275
1276 if (!size || cmap->start + cmap->len > size)
1277 return ERR_PTR(-EINVAL);
1278
1279 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1280 if (IS_ERR(gamma_lut))
1281 return gamma_lut;
1282
1283 lut = (struct drm_color_lut *)gamma_lut->data;
1284 if (cmap->start || cmap->len != size) {
1285 u16 *r = crtc->gamma_store;
1286 u16 *g = r + crtc->gamma_size;
1287 u16 *b = g + crtc->gamma_size;
1288
1289 for (i = 0; i < cmap->start; i++) {
1290 lut[i].red = r[i];
1291 lut[i].green = g[i];
1292 lut[i].blue = b[i];
1293 }
1294 for (i = cmap->start + cmap->len; i < size; i++) {
1295 lut[i].red = r[i];
1296 lut[i].green = g[i];
1297 lut[i].blue = b[i];
1298 }
1299 }
1300
1301 for (i = 0; i < cmap->len; i++) {
1302 lut[cmap->start + i].red = cmap->red[i];
1303 lut[cmap->start + i].green = cmap->green[i];
1304 lut[cmap->start + i].blue = cmap->blue[i];
1305 }
1306
1307 return gamma_lut;
1308}
1309
1310static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1311{
1312 struct drm_fb_helper *fb_helper = info->par;
1313 struct drm_device *dev = fb_helper->dev;
1314 struct drm_property_blob *gamma_lut = NULL;
1315 struct drm_modeset_acquire_ctx ctx;
1316 struct drm_crtc_state *crtc_state;
1317 struct drm_atomic_state *state;
1318 struct drm_crtc *crtc;
1319 u16 *r, *g, *b;
1320 int i, ret = 0;
1321 bool replaced;
1322
1323 drm_modeset_acquire_init(&ctx, 0);
1324
1325 state = drm_atomic_state_alloc(dev);
1326 if (!state) {
1327 ret = -ENOMEM;
1328 goto out_ctx;
1329 }
1330
1331 state->acquire_ctx = &ctx;
1332retry:
1333 for (i = 0; i < fb_helper->crtc_count; i++) {
1334 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1335
1336 if (!gamma_lut)
1337 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1338 if (IS_ERR(gamma_lut)) {
1339 ret = PTR_ERR(gamma_lut);
1340 gamma_lut = NULL;
1341 goto out_state;
1342 }
1343
1344 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1345 if (IS_ERR(crtc_state)) {
1346 ret = PTR_ERR(crtc_state);
1347 goto out_state;
1348 }
1349
1350 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1351 NULL);
1352 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1353 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1354 gamma_lut);
1355 crtc_state->color_mgmt_changed |= replaced;
1356 }
1357
1358 ret = drm_atomic_commit(state);
1359 if (ret)
1360 goto out_state;
1361
1362 for (i = 0; i < fb_helper->crtc_count; i++) {
1363 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1364
1365 r = crtc->gamma_store;
1366 g = r + crtc->gamma_size;
1367 b = g + crtc->gamma_size;
1368
1369 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1370 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1371 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1372 }
1373
1374out_state:
1375 if (ret == -EDEADLK)
1376 goto backoff;
1377
1378 drm_property_blob_put(gamma_lut);
1379 drm_atomic_state_put(state);
1380out_ctx:
1381 drm_modeset_drop_locks(&ctx);
1382 drm_modeset_acquire_fini(&ctx);
1383
1384 return ret;
1385
1386backoff:
1387 drm_atomic_state_clear(state);
1388 drm_modeset_backoff(&ctx);
1389 goto retry;
1390}
1391
1392/**
1393 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1394 * @cmap: cmap to set
1395 * @info: fbdev registered by the helper
1396 */
1397int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1398{
1399 struct drm_fb_helper *fb_helper = info->par;
1400 int ret;
1401
1402 if (oops_in_progress)
1403 return -EBUSY;
1404
1405 mutex_lock(&fb_helper->lock);
1406
1407 if (!drm_fb_helper_is_bound(fb_helper)) {
1408 ret = -EBUSY;
1409 goto out;
1410 }
1411
1412 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1413 ret = setcmap_pseudo_palette(cmap, info);
1414 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1415 ret = setcmap_atomic(cmap, info);
1416 else
1417 ret = setcmap_legacy(cmap, info);
1418
1419out:
Thierry Redinge9827d82017-07-04 17:18:23 +02001420 mutex_unlock(&fb_helper->lock);
Peter Rosin964c6002017-07-13 18:25:27 +02001421
1422 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001423}
1424EXPORT_SYMBOL(drm_fb_helper_setcmap);
1425
Daniel Vetter207fd322013-01-20 22:13:14 +01001426/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001427 * drm_fb_helper_ioctl - legacy ioctl implementation
1428 * @info: fbdev registered by the helper
1429 * @cmd: ioctl command
1430 * @arg: ioctl argument
1431 *
1432 * A helper to implement the standard fbdev ioctl. Only
1433 * FBIO_WAITFORVSYNC is implemented for now.
1434 */
1435int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1436 unsigned long arg)
1437{
1438 struct drm_fb_helper *fb_helper = info->par;
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001439 struct drm_mode_set *mode_set;
1440 struct drm_crtc *crtc;
1441 int ret = 0;
1442
Thierry Redinge9827d82017-07-04 17:18:23 +02001443 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001444 if (!drm_fb_helper_is_bound(fb_helper)) {
1445 ret = -EBUSY;
1446 goto unlock;
1447 }
1448
1449 switch (cmd) {
1450 case FBIO_WAITFORVSYNC:
1451 /*
1452 * Only consider the first CRTC.
1453 *
1454 * This ioctl is supposed to take the CRTC number as
1455 * an argument, but in fbdev times, what that number
1456 * was supposed to be was quite unclear, different
1457 * drivers were passing that argument differently
1458 * (some by reference, some by value), and most of the
1459 * userspace applications were just hardcoding 0 as an
1460 * argument.
1461 *
1462 * The first CRTC should be the integrated panel on
1463 * most drivers, so this is the best choice we can
1464 * make. If we're not smart enough here, one should
1465 * just consider switch the userspace to KMS.
1466 */
1467 mode_set = &fb_helper->crtc_info[0].mode_set;
1468 crtc = mode_set->crtc;
1469
1470 /*
1471 * Only wait for a vblank event if the CRTC is
1472 * enabled, otherwise just don't do anythintg,
1473 * not even report an error.
1474 */
1475 ret = drm_crtc_vblank_get(crtc);
1476 if (!ret) {
1477 drm_crtc_wait_one_vblank(crtc);
1478 drm_crtc_vblank_put(crtc);
1479 }
1480
1481 ret = 0;
1482 goto unlock;
1483 default:
1484 ret = -ENOTTY;
1485 }
1486
1487unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001488 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001489 return ret;
1490}
1491EXPORT_SYMBOL(drm_fb_helper_ioctl);
1492
1493/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001494 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001495 * @var: screeninfo to check
1496 * @info: fbdev registered by the helper
1497 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001498int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1499 struct fb_info *info)
1500{
1501 struct drm_fb_helper *fb_helper = info->par;
1502 struct drm_framebuffer *fb = fb_helper->fb;
1503 int depth;
1504
Jason Wesself90ebd92010-08-05 09:22:32 -05001505 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001506 return -EINVAL;
1507
Stefan Agner865afb12016-10-11 16:15:04 -07001508 /*
1509 * Changes struct fb_var_screeninfo are currently not pushed back
1510 * to KMS, hence fail if different settings are requested.
1511 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001512 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001513 var->xres > fb->width || var->yres > fb->height ||
1514 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1515 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001516 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1517 var->xres, var->yres, var->bits_per_pixel,
1518 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001519 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001520 return -EINVAL;
1521 }
1522
1523 switch (var->bits_per_pixel) {
1524 case 16:
1525 depth = (var->green.length == 6) ? 16 : 15;
1526 break;
1527 case 32:
1528 depth = (var->transp.length > 0) ? 32 : 24;
1529 break;
1530 default:
1531 depth = var->bits_per_pixel;
1532 break;
1533 }
1534
1535 switch (depth) {
1536 case 8:
1537 var->red.offset = 0;
1538 var->green.offset = 0;
1539 var->blue.offset = 0;
1540 var->red.length = 8;
1541 var->green.length = 8;
1542 var->blue.length = 8;
1543 var->transp.length = 0;
1544 var->transp.offset = 0;
1545 break;
1546 case 15:
1547 var->red.offset = 10;
1548 var->green.offset = 5;
1549 var->blue.offset = 0;
1550 var->red.length = 5;
1551 var->green.length = 5;
1552 var->blue.length = 5;
1553 var->transp.length = 1;
1554 var->transp.offset = 15;
1555 break;
1556 case 16:
1557 var->red.offset = 11;
1558 var->green.offset = 5;
1559 var->blue.offset = 0;
1560 var->red.length = 5;
1561 var->green.length = 6;
1562 var->blue.length = 5;
1563 var->transp.length = 0;
1564 var->transp.offset = 0;
1565 break;
1566 case 24:
1567 var->red.offset = 16;
1568 var->green.offset = 8;
1569 var->blue.offset = 0;
1570 var->red.length = 8;
1571 var->green.length = 8;
1572 var->blue.length = 8;
1573 var->transp.length = 0;
1574 var->transp.offset = 0;
1575 break;
1576 case 32:
1577 var->red.offset = 16;
1578 var->green.offset = 8;
1579 var->blue.offset = 0;
1580 var->red.length = 8;
1581 var->green.length = 8;
1582 var->blue.length = 8;
1583 var->transp.length = 8;
1584 var->transp.offset = 24;
1585 break;
1586 default:
1587 return -EINVAL;
1588 }
1589 return 0;
1590}
1591EXPORT_SYMBOL(drm_fb_helper_check_var);
1592
Daniel Vetter207fd322013-01-20 22:13:14 +01001593/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001594 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001595 * @info: fbdev registered by the helper
1596 *
1597 * This will let fbcon do the mode init and is called at initialization time by
1598 * the fbdev core when registering the driver, and later on through the hotplug
1599 * callback.
1600 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001601int drm_fb_helper_set_par(struct fb_info *info)
1602{
1603 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001604 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001605
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001606 if (oops_in_progress)
1607 return -EBUSY;
1608
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001609 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001610 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001611 return -EINVAL;
1612 }
1613
Rob Clark5ea1f752014-05-30 12:29:48 -04001614 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001615
Dave Airlie785b93e2009-08-28 15:46:53 +10001616 return 0;
1617}
1618EXPORT_SYMBOL(drm_fb_helper_set_par);
1619
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001620static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
Rob Clark1edf0262015-08-25 15:35:59 -04001621{
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001622 int i;
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001623
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001624 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001625 struct drm_mode_set *mode_set;
1626
1627 mode_set = &fb_helper->crtc_info[i].mode_set;
1628
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001629 mode_set->x = x;
1630 mode_set->y = y;
Rob Clark1edf0262015-08-25 15:35:59 -04001631 }
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001632}
Rob Clark1edf0262015-08-25 15:35:59 -04001633
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001634static int pan_display_atomic(struct fb_var_screeninfo *var,
1635 struct fb_info *info)
1636{
1637 struct drm_fb_helper *fb_helper = info->par;
1638 int ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001639
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001640 pan_set(fb_helper, var->xoffset, var->yoffset);
Rob Clark1edf0262015-08-25 15:35:59 -04001641
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001642 ret = restore_fbdev_mode_atomic(fb_helper, true);
1643 if (!ret) {
1644 info->var.xoffset = var->xoffset;
1645 info->var.yoffset = var->yoffset;
1646 } else
1647 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001648
Rob Clark1edf0262015-08-25 15:35:59 -04001649 return ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001650}
1651
Daniel Vetter71286452017-04-03 10:33:04 +02001652static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001653 struct fb_info *info)
1654{
1655 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001656 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001657 int ret = 0;
1658 int i;
1659
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001660 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001661 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001662 modeset = &fb_helper->crtc_info[i].mode_set;
1663
1664 modeset->x = var->xoffset;
1665 modeset->y = var->yoffset;
1666
1667 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001668 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001669 if (!ret) {
1670 info->var.xoffset = var->xoffset;
1671 info->var.yoffset = var->yoffset;
1672 }
1673 }
1674 }
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001675 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001676
1677 return ret;
1678}
1679
1680/**
1681 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1682 * @var: updated screen information
1683 * @info: fbdev registered by the helper
1684 */
1685int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1686 struct fb_info *info)
1687{
1688 struct drm_fb_helper *fb_helper = info->par;
1689 struct drm_device *dev = fb_helper->dev;
1690 int ret;
1691
1692 if (oops_in_progress)
1693 return -EBUSY;
1694
Thierry Redinge9827d82017-07-04 17:18:23 +02001695 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001696 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001697 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001698 return -EBUSY;
1699 }
1700
1701 if (drm_drv_uses_atomic_modeset(dev))
1702 ret = pan_display_atomic(var, info);
1703 else
1704 ret = pan_display_legacy(var, info);
Thierry Redinge9827d82017-07-04 17:18:23 +02001705 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001706
Dave Airlie785b93e2009-08-28 15:46:53 +10001707 return ret;
1708}
1709EXPORT_SYMBOL(drm_fb_helper_pan_display);
1710
Daniel Vetter8acf6582013-01-21 23:38:37 +01001711/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001712 * Allocates the backing storage and sets up the fbdev info structure through
Daniel Vetterca91a272017-07-06 15:00:21 +02001713 * the ->fb_probe callback.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001714 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001715static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1716 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001717{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001718 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001719 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001720 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001721 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001722 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001723
1724 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1725 sizes.surface_depth = 24;
1726 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001727 sizes.fb_width = (u32)-1;
1728 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001729
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001730 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001731 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001732 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001733
Dave Airlie785b93e2009-08-28 15:46:53 +10001734 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001735 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001736 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001737 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001738
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001739 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001740
1741 if (cmdline_mode->bpp_specified) {
1742 switch (cmdline_mode->bpp) {
1743 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001744 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001745 break;
1746 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001747 sizes.surface_depth = 15;
1748 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001749 break;
1750 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001751 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001752 break;
1753 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001754 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001755 break;
1756 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001757 sizes.surface_depth = 24;
1758 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001759 break;
1760 }
1761 break;
1762 }
1763 }
1764
Dave Airlie8be48d92010-03-30 05:34:14 +00001765 crtc_count = 0;
1766 for (i = 0; i < fb_helper->crtc_count; i++) {
1767 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001768 struct drm_mode_set *mode_set;
1769 int x, y, j;
1770 /* in case of tile group, are we the last tile vert or horiz?
1771 * If no tile group you are always the last one both vertically
1772 * and horizontally
1773 */
1774 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001775
Dave Airlie8be48d92010-03-30 05:34:14 +00001776 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001777 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001778
1779 if (!desired_mode)
1780 continue;
1781
1782 crtc_count++;
1783
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001784 x = fb_helper->crtc_info[i].x;
1785 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001786
1787 if (gamma_size == 0)
1788 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1789
1790 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1791 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001792
1793 for (j = 0; j < mode_set->num_connectors; j++) {
1794 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001795
Rob Clark0e3704c2015-03-11 10:23:14 -04001796 if (connector->has_tile) {
1797 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1798 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1799 /* cloning to multiple tiles is just crazy-talk, so: */
1800 break;
1801 }
1802 }
1803
1804 if (lasth)
1805 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1806 if (lastv)
1807 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001808 }
1809
Dave Airlie38651672010-03-30 05:34:13 +00001810 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Daniel Vetterca91a272017-07-06 15:00:21 +02001811 DRM_INFO("Cannot find any crtc or sizes\n");
1812 return -EAGAIN;
Dave Airlie785b93e2009-08-28 15:46:53 +10001813 }
1814
Xinliang Liu5f152572017-02-15 17:19:08 +01001815 /* Handle our overallocation */
1816 sizes.surface_height *= drm_fbdev_overalloc;
1817 sizes.surface_height /= 100;
1818
Dave Airlie38651672010-03-30 05:34:13 +00001819 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001820 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1821 if (ret < 0)
1822 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001823
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001824 /*
1825 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1826 * events, but at init time drm_setup_crtcs needs to be called before
1827 * the fb is allocated (since we need to figure out the desired size of
1828 * the fb before we can allocate it ...). Hence we need to fix things up
1829 * here again.
1830 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001831 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001832 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1833 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1834
Dave Airlie785b93e2009-08-28 15:46:53 +10001835 return 0;
1836}
Dave Airlie785b93e2009-08-28 15:46:53 +10001837
Daniel Vetter207fd322013-01-20 22:13:14 +01001838/**
1839 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1840 * @info: fbdev registered by the helper
1841 * @pitch: desired pitch
1842 * @depth: desired depth
1843 *
1844 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1845 * fbdev emulations. Drivers which support acceleration methods which impose
1846 * additional constraints need to set up their own limits.
1847 *
1848 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001849 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001850 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001851void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1852 uint32_t depth)
1853{
1854 info->fix.type = FB_TYPE_PACKED_PIXELS;
1855 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1856 FB_VISUAL_TRUECOLOR;
1857 info->fix.mmio_start = 0;
1858 info->fix.mmio_len = 0;
1859 info->fix.type_aux = 0;
1860 info->fix.xpanstep = 1; /* doing it in hw */
1861 info->fix.ypanstep = 1; /* doing it in hw */
1862 info->fix.ywrapstep = 0;
1863 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001864
1865 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001866}
1867EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1868
Daniel Vetter207fd322013-01-20 22:13:14 +01001869/**
1870 * drm_fb_helper_fill_var - initalizes variable fbdev information
1871 * @info: fbdev instance to set up
1872 * @fb_helper: fb helper instance to use as template
1873 * @fb_width: desired fb width
1874 * @fb_height: desired fb height
1875 *
1876 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001877 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001878 *
1879 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001880 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1881 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001882 */
Dave Airlie38651672010-03-30 05:34:13 +00001883void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001884 uint32_t fb_width, uint32_t fb_height)
1885{
Dave Airlie38651672010-03-30 05:34:13 +00001886 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001887
Dave Airlie38651672010-03-30 05:34:13 +00001888 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001889 info->var.xres_virtual = fb->width;
1890 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001891 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001892 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001893 info->var.xoffset = 0;
1894 info->var.yoffset = 0;
1895 info->var.activate = FB_ACTIVATE_NOW;
1896 info->var.height = -1;
1897 info->var.width = -1;
1898
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001899 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001900 case 8:
1901 info->var.red.offset = 0;
1902 info->var.green.offset = 0;
1903 info->var.blue.offset = 0;
1904 info->var.red.length = 8; /* 8bit DAC */
1905 info->var.green.length = 8;
1906 info->var.blue.length = 8;
1907 info->var.transp.offset = 0;
1908 info->var.transp.length = 0;
1909 break;
1910 case 15:
1911 info->var.red.offset = 10;
1912 info->var.green.offset = 5;
1913 info->var.blue.offset = 0;
1914 info->var.red.length = 5;
1915 info->var.green.length = 5;
1916 info->var.blue.length = 5;
1917 info->var.transp.offset = 15;
1918 info->var.transp.length = 1;
1919 break;
1920 case 16:
1921 info->var.red.offset = 11;
1922 info->var.green.offset = 5;
1923 info->var.blue.offset = 0;
1924 info->var.red.length = 5;
1925 info->var.green.length = 6;
1926 info->var.blue.length = 5;
1927 info->var.transp.offset = 0;
1928 break;
1929 case 24:
1930 info->var.red.offset = 16;
1931 info->var.green.offset = 8;
1932 info->var.blue.offset = 0;
1933 info->var.red.length = 8;
1934 info->var.green.length = 8;
1935 info->var.blue.length = 8;
1936 info->var.transp.offset = 0;
1937 info->var.transp.length = 0;
1938 break;
1939 case 32:
1940 info->var.red.offset = 16;
1941 info->var.green.offset = 8;
1942 info->var.blue.offset = 0;
1943 info->var.red.length = 8;
1944 info->var.green.length = 8;
1945 info->var.blue.length = 8;
1946 info->var.transp.offset = 24;
1947 info->var.transp.length = 8;
1948 break;
1949 default:
1950 break;
1951 }
1952
1953 info->var.xres = fb_width;
1954 info->var.yres = fb_height;
1955}
1956EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001957
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001958static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
Daniel Vettere13a0582017-07-05 06:56:29 +02001959 uint32_t maxX,
1960 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001961{
1962 struct drm_connector *connector;
Daniel Vettere13a0582017-07-05 06:56:29 +02001963 int i, count = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001964
Chris Wilson966a6a12016-11-29 12:02:15 +00001965 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001966 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001967 count += connector->funcs->fill_modes(connector, maxX, maxY);
1968 }
1969
1970 return count;
1971}
1972
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001973struct 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 +00001974{
1975 struct drm_display_mode *mode;
1976
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001977 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001978 if (mode->hdisplay > width ||
1979 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001980 continue;
1981 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1982 return mode;
1983 }
1984 return NULL;
1985}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001986EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001987
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001988static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001989{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001990 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001991}
1992
Vincent Abrioua09759e2017-01-06 17:44:43 +01001993struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00001994{
Chris Wilson1794d252011-04-17 07:43:32 +01001995 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001996 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001997 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001998
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001999 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00002000 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00002001 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00002002
2003 /* attempt to find a matching mode in the list of modes
2004 * we have gotten so far, if not add a CVT mode that conforms
2005 */
2006 if (cmdline_mode->rb || cmdline_mode->margins)
2007 goto create_mode;
2008
Takashi Iwaic683f422014-03-19 14:53:13 +01002009 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002010again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002011 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00002012 /* check width/height */
2013 if (mode->hdisplay != cmdline_mode->xres ||
2014 mode->vdisplay != cmdline_mode->yres)
2015 continue;
2016
2017 if (cmdline_mode->refresh_specified) {
2018 if (mode->vrefresh != cmdline_mode->refresh)
2019 continue;
2020 }
2021
2022 if (cmdline_mode->interlace) {
2023 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2024 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01002025 } else if (prefer_non_interlace) {
2026 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2027 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002028 }
2029 return mode;
2030 }
2031
Takashi Iwaic683f422014-03-19 14:53:13 +01002032 if (prefer_non_interlace) {
2033 prefer_non_interlace = false;
2034 goto again;
2035 }
2036
Dave Airlie38651672010-03-30 05:34:13 +00002037create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01002038 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2039 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002040 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00002041 return mode;
2042}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002043EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002044
2045static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2046{
2047 bool enable;
2048
Sachin Kamat96081cd2012-11-15 03:43:30 +00002049 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00002050 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002051 else
Dave Airlie38651672010-03-30 05:34:13 +00002052 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002053
Dave Airlie38651672010-03-30 05:34:13 +00002054 return enable;
2055}
2056
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002057static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2058 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00002059{
2060 bool any_enabled = false;
2061 struct drm_connector *connector;
2062 int i = 0;
2063
Chris Wilson966a6a12016-11-29 12:02:15 +00002064 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002065 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002066 enabled[i] = drm_connector_enabled(connector, true);
2067 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
2068 enabled[i] ? "yes" : "no");
2069 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00002070 }
2071
2072 if (any_enabled)
2073 return;
2074
Chris Wilson966a6a12016-11-29 12:02:15 +00002075 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002076 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002077 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00002078 }
2079}
2080
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002081static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2082 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002083 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002084 bool *enabled, int width, int height)
2085{
2086 int count, i, j;
2087 bool can_clone = false;
2088 struct drm_fb_helper_connector *fb_helper_conn;
2089 struct drm_display_mode *dmt_mode, *mode;
2090
2091 /* only contemplate cloning in the single crtc case */
2092 if (fb_helper->crtc_count > 1)
2093 return false;
2094
2095 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002096 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002097 if (enabled[i])
2098 count++;
2099 }
2100
2101 /* only contemplate cloning if more than one connector is enabled */
2102 if (count <= 1)
2103 return false;
2104
2105 /* check the command line or if nothing common pick 1024x768 */
2106 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002107 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002108 if (!enabled[i])
2109 continue;
2110 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002111 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002112 if (!modes[i]) {
2113 can_clone = false;
2114 break;
2115 }
2116 for (j = 0; j < i; j++) {
2117 if (!enabled[j])
2118 continue;
2119 if (!drm_mode_equal(modes[j], modes[i]))
2120 can_clone = false;
2121 }
2122 }
2123
2124 if (can_clone) {
2125 DRM_DEBUG_KMS("can clone using command line\n");
2126 return true;
2127 }
2128
2129 /* try and find a 1024x768 mode on each connector */
2130 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002131 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002132
Chris Wilson966a6a12016-11-29 12:02:15 +00002133 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002134 if (!enabled[i])
2135 continue;
2136
2137 fb_helper_conn = fb_helper->connector_info[i];
2138 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2139 if (drm_mode_equal(mode, dmt_mode))
2140 modes[i] = mode;
2141 }
2142 if (!modes[i])
2143 can_clone = false;
2144 }
2145
2146 if (can_clone) {
2147 DRM_DEBUG_KMS("can clone using 1024x768\n");
2148 return true;
2149 }
2150 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2151 return false;
2152}
2153
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002154static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2155 struct drm_display_mode **modes,
2156 struct drm_fb_offset *offsets,
2157 int idx,
2158 int h_idx, int v_idx)
2159{
2160 struct drm_fb_helper_connector *fb_helper_conn;
2161 int i;
2162 int hoffset = 0, voffset = 0;
2163
Chris Wilson966a6a12016-11-29 12:02:15 +00002164 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002165 fb_helper_conn = fb_helper->connector_info[i];
2166 if (!fb_helper_conn->connector->has_tile)
2167 continue;
2168
2169 if (!modes[i] && (h_idx || v_idx)) {
2170 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2171 fb_helper_conn->connector->base.id);
2172 continue;
2173 }
2174 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2175 hoffset += modes[i]->hdisplay;
2176
2177 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2178 voffset += modes[i]->vdisplay;
2179 }
2180 offsets[idx].x = hoffset;
2181 offsets[idx].y = voffset;
2182 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2183 return 0;
2184}
2185
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002186static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002187 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002188 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002189 bool *enabled, int width, int height)
2190{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002191 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002192 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2193 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002194 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002195 int i;
2196
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002197retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002198 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002199 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002200
Chris Wilsonc96521e2016-11-27 17:09:10 +00002201 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002202 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002203
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002204 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002205 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002206 continue;
2207 }
2208
2209 /* first pass over all the untiled connectors */
2210 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2211 continue;
2212
2213 if (tile_pass == 1) {
2214 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2215 fb_helper_conn->connector->tile_v_loc != 0)
2216 continue;
2217
2218 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002219 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002220 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2221 /* if this tile_pass doesn't cover any of the tiles - keep going */
2222 continue;
2223
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002224 /*
2225 * find the tile offsets for this pass - need to find
2226 * all tiles left and above
2227 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002228 drm_get_tile_offsets(fb_helper, modes, offsets,
2229 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2230 }
Dave Airlie38651672010-03-30 05:34:13 +00002231 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002232 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002233
2234 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002235 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002236 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002237 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2238 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 +00002239 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002240 }
2241 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002242 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2243 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002244 break;
2245 }
2246 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2247 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002248 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002249 }
2250
2251 if ((conn_configured & mask) != mask) {
2252 tile_pass++;
2253 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002254 }
2255 return true;
2256}
2257
Dave Airlie8be48d92010-03-30 05:34:14 +00002258static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2259 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002260 struct drm_display_mode **modes,
2261 int n, int width, int height)
2262{
2263 int c, o;
2264 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002265 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002266 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002267 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002268 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002269 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002270
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002271 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002272 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002273
2274 fb_helper_conn = fb_helper->connector_info[n];
2275 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002276
2277 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002278 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002279 if (modes[n] == NULL)
2280 return best_score;
2281
Lyude255f0e72016-05-12 10:56:59 -04002282 crtcs = kzalloc(fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002283 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002284 if (!crtcs)
2285 return best_score;
2286
2287 my_score = 1;
2288 if (connector->status == connector_status_connected)
2289 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002290 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002291 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002292 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002293 my_score++;
2294
2295 connector_funcs = connector->helper_private;
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002296
2297 /*
2298 * If the DRM device implements atomic hooks and ->best_encoder() is
2299 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2300 * helper.
2301 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002302 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002303 !connector_funcs->best_encoder)
2304 encoder = drm_atomic_helper_best_encoder(connector);
2305 else
2306 encoder = connector_funcs->best_encoder(connector);
2307
Dave Airlie38651672010-03-30 05:34:13 +00002308 if (!encoder)
2309 goto out;
2310
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002311 /*
2312 * select a crtc for this connector and then attempt to configure
2313 * remaining connectors
2314 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002315 for (c = 0; c < fb_helper->crtc_count; c++) {
2316 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002317
Sachin Kamat96081cd2012-11-15 03:43:30 +00002318 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002319 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002320
2321 for (o = 0; o < n; o++)
2322 if (best_crtcs[o] == crtc)
2323 break;
2324
2325 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002326 /* ignore cloning unless only a single crtc */
2327 if (fb_helper->crtc_count > 1)
2328 continue;
2329
2330 if (!drm_mode_equal(modes[o], modes[n]))
2331 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002332 }
2333
2334 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002335 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2336 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002337 width, height);
2338 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002339 best_score = score;
2340 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002341 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002342 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002343 }
Dave Airlie38651672010-03-30 05:34:13 +00002344 }
2345out:
2346 kfree(crtcs);
2347 return best_score;
2348}
2349
Chris Wilson64e94402016-11-29 12:02:16 +00002350static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2351 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002352{
Dave Airlie8be48d92010-03-30 05:34:14 +00002353 struct drm_device *dev = fb_helper->dev;
2354 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002355 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002356 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002357 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002358 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002359
2360 DRM_DEBUG_KMS("\n");
Chris Wilson966a6a12016-11-29 12:02:15 +00002361 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002362 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002363
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002364 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002365 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002366 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002367 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002368 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002369 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002370 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002371 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002372 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002373 DRM_ERROR("Memory allocation failed\n");
2374 goto out;
2375 }
2376
Daniel Vettere13a0582017-07-05 06:56:29 +02002377 mutex_lock(&fb_helper->dev->mode_config.mutex);
2378 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2379 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002380 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002381
Jesse Barnes11e17a02013-02-19 13:31:39 -08002382 if (!(fb_helper->funcs->initial_config &&
2383 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002384 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002385 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002386 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2387 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2388 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002389
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002390 if (!drm_target_cloned(fb_helper, modes, offsets,
2391 enabled, width, height) &&
2392 !drm_target_preferred(fb_helper, modes, offsets,
2393 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002394 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002395
2396 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2397 width, height);
2398
2399 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002400 }
Daniel Vettere13a0582017-07-05 06:56:29 +02002401 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002402
Dave Airlie8be48d92010-03-30 05:34:14 +00002403 /* need to set the modesets up here for use later */
2404 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002405 for (i = 0; i < fb_helper->crtc_count; i++)
2406 drm_fb_helper_modeset_release(fb_helper,
2407 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002408
Chris Wilson966a6a12016-11-29 12:02:15 +00002409 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002410 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002411 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002412 struct drm_fb_offset *offset = &offsets[i];
Ville Syrjäläa2889602016-10-26 17:41:18 +03002413 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00002414
Dave Airlie8be48d92010-03-30 05:34:14 +00002415 if (mode && fb_crtc) {
Ville Syrjäläa2889602016-10-26 17:41:18 +03002416 struct drm_connector *connector =
2417 fb_helper->connector_info[i]->connector;
2418
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002419 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2420 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002421
Dave Airlie8be48d92010-03-30 05:34:14 +00002422 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002423 fb_crtc->x = offset->x;
2424 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002425 modeset->mode = drm_mode_duplicate(dev,
2426 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002427 drm_connector_get(connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002428 modeset->connectors[modeset->num_connectors++] = connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002429 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002430 modeset->x = offset->x;
2431 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002432 }
Dave Airlie38651672010-03-30 05:34:13 +00002433 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002434out:
Dave Airlie38651672010-03-30 05:34:13 +00002435 kfree(crtcs);
2436 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002437 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002438 kfree(enabled);
2439}
2440
Daniel Vetterca91a272017-07-06 15:00:21 +02002441/* Note: Drops fb_helper->lock before returning. */
2442static int
2443__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2444 int bpp_sel)
2445{
2446 struct drm_device *dev = fb_helper->dev;
2447 struct fb_info *info;
2448 unsigned int width, height;
2449 int ret;
2450
2451 width = dev->mode_config.max_width;
2452 height = dev->mode_config.max_height;
2453
2454 drm_setup_crtcs(fb_helper, width, height);
2455 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2456 if (ret < 0) {
2457 if (ret == -EAGAIN) {
2458 fb_helper->preferred_bpp = bpp_sel;
2459 fb_helper->deferred_setup = true;
2460 ret = 0;
2461 }
2462 mutex_unlock(&fb_helper->lock);
2463
2464 return ret;
2465 }
2466
2467 fb_helper->deferred_setup = false;
2468
2469 info = fb_helper->fbdev;
2470 info->var.pixclock = 0;
2471
2472 /* Need to drop locks to avoid recursive deadlock in
2473 * register_framebuffer. This is ok because the only thing left to do is
2474 * register the fbdev emulation instance in kernel_fb_helper_list. */
2475 mutex_unlock(&fb_helper->lock);
2476
2477 ret = register_framebuffer(info);
2478 if (ret < 0)
2479 return ret;
2480
2481 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2482 info->node, info->fix.id);
2483
2484 mutex_lock(&kernel_fb_helper_lock);
2485 if (list_empty(&kernel_fb_helper_list))
2486 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2487
2488 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2489 mutex_unlock(&kernel_fb_helper_lock);
2490
2491 return 0;
2492}
2493
Dave Airlie38651672010-03-30 05:34:13 +00002494/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002495 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002496 * @fb_helper: fb_helper device struct
2497 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002498 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002499 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002500 * At the moment, this is a cloned configuration across all heads with
2501 * a new framebuffer object as the backing store.
2502 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002503 * Note that this also registers the fbdev and so allows userspace to call into
2504 * the driver through the fbdev interfaces.
2505 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002506 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2507 * to let the driver allocate and initialize the fbdev info structure and the
2508 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002509 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2510 * values for the fbdev info structure.
2511 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002512 * HANG DEBUGGING:
2513 *
2514 * When you have fbcon support built-in or already loaded, this function will do
2515 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2516 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2517 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2518 * to consoles, not even serial console. This means when your driver crashes,
2519 * you will see absolutely nothing else but a system stuck in this function,
2520 * with no further output. Any kind of printk() you place within your own driver
2521 * or in the drm core modeset code will also never show up.
2522 *
2523 * Standard debug practice is to run the fbcon setup without taking the
2524 * console_lock as a hack, to be able to see backtraces and crashes on the
2525 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2526 * cmdline option.
2527 *
2528 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002529 * first modeset from userspace will crash in the same way, and is even easier
2530 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002531 * kernel cmdline option.
2532 *
Dave Airlie38651672010-03-30 05:34:13 +00002533 * RETURNS:
2534 * Zero if everything went ok, nonzero otherwise.
2535 */
Thierry Reding01934c22014-12-19 11:21:32 +01002536int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002537{
Chris Wilson966a6a12016-11-29 12:02:15 +00002538 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002539
Daniel Vetterf64c5572015-08-25 15:45:13 +02002540 if (!drm_fbdev_emulation)
2541 return 0;
2542
Thierry Redinge9827d82017-07-04 17:18:23 +02002543 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002544 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00002545
Daniel Vetterca91a272017-07-06 15:00:21 +02002546 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002547}
Dave Airlie8be48d92010-03-30 05:34:14 +00002548EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002549
Chris Wilson73943712011-04-22 11:03:57 +01002550/**
2551 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002552 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01002553 * @fb_helper: the drm_fb_helper
2554 *
Chris Wilson73943712011-04-22 11:03:57 +01002555 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002556 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002557 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002558 * Called at runtime, takes the mode config locks to be able to check/change the
2559 * modeset configuration. Must be run from process context (which usually means
2560 * either the output polling work or a work item launched from the driver's
2561 * hotplug interrupt).
2562 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002563 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002564 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002565 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2566 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002567 *
Chris Wilson73943712011-04-22 11:03:57 +01002568 * RETURNS:
2569 * 0 on success and a non-zero error code otherwise.
2570 */
2571int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002572{
Thierry Redinge9827d82017-07-04 17:18:23 +02002573 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002574
Daniel Vetterf64c5572015-08-25 15:45:13 +02002575 if (!drm_fbdev_emulation)
2576 return 0;
2577
Thierry Redinge9827d82017-07-04 17:18:23 +02002578 mutex_lock(&fb_helper->lock);
Daniel Vetterca91a272017-07-06 15:00:21 +02002579 if (fb_helper->deferred_setup) {
2580 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
2581 fb_helper->preferred_bpp);
2582 return err;
2583 }
2584
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002585 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002586 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002587 mutex_unlock(&fb_helper->lock);
2588 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002589 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002590
Dave Airlie38651672010-03-30 05:34:13 +00002591 DRM_DEBUG_KMS("\n");
2592
Chris Wilson64e94402016-11-29 12:02:16 +00002593 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
Thierry Redinge9827d82017-07-04 17:18:23 +02002594 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002595
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002596 drm_fb_helper_set_par(fb_helper->fbdev);
2597
2598 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002599}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002600EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002601
David Rientjes6a108a12011-01-20 14:44:16 -08002602/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002603 * but the module doesn't depend on any fb console symbols. At least
2604 * attempt to load fbcon to avoid leaving the system without a usable console.
2605 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002606int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002607{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002608#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07002609 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06002610 struct module *fbcon;
2611
2612 mutex_lock(&module_mutex);
2613 fbcon = find_module(name);
2614 mutex_unlock(&module_mutex);
2615
2616 if (!fbcon)
2617 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002618#endif
David Fries3ce05162010-12-12 12:39:22 -06002619 return 0;
2620}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002621EXPORT_SYMBOL(drm_fb_helper_modinit);