blob: 42090fe00ef9652736e4378ff09cbf0a9d5ce41b [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
Thierry Redinge9827d82017-07-04 17:18:23 +0200504 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400505 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000506
507 do_delayed = fb_helper->delayed_hotplug;
508 if (do_delayed)
509 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200510 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000511
512 if (do_delayed)
513 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200514
Rob Clark5ea1f752014-05-30 12:29:48 -0400515 return ret;
516}
517EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100518
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200519static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
520{
521 struct drm_device *dev = fb_helper->dev;
522 struct drm_crtc *crtc;
523 int bound = 0, crtcs_bound = 0;
524
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200525 /*
526 * Sometimes user space wants everything disabled, so don't steal the
527 * display if there's a master.
528 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200529 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200530 return false;
531
532 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200533 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200534 if (crtc->primary->fb)
535 crtcs_bound++;
536 if (crtc->primary->fb == fb_helper->fb)
537 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200538 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200539 }
540
541 if (bound < crtcs_bound)
542 return false;
543
544 return true;
545}
546
547#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100548/*
549 * restore fbcon display for all kms driver's using this helper, used for sysrq
550 * and panic handling.
551 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530552static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000553{
Dave Airlie785b93e2009-08-28 15:46:53 +1000554 bool ret, error = false;
555 struct drm_fb_helper *helper;
556
557 if (list_empty(&kernel_fb_helper_list))
558 return false;
559
560 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200561 struct drm_device *dev = helper->dev;
562
563 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100564 continue;
565
Thierry Redinge9827d82017-07-04 17:18:23 +0200566 mutex_lock(&helper->lock);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200567 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100568 if (ret)
569 error = true;
Thierry Redinge9827d82017-07-04 17:18:23 +0200570 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000571 }
572 return error;
573}
574
Dave Airlie785b93e2009-08-28 15:46:53 +1000575static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
576{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100577 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200578
Daniel Vetterd21bf462013-01-20 18:09:52 +0100579 ret = drm_fb_helper_force_kernel_mode();
580 if (ret == true)
581 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000582}
583static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
584
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700585static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000586{
587 schedule_work(&drm_fb_helper_restore_work);
588}
589
590static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
591 .handler = drm_fb_helper_sysrq,
592 .help_msg = "force-fb(V)",
593 .action_msg = "Restore framebuffer console",
594};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000595#else
596static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200597#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000598
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200599static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000600{
Dave Airlie785b93e2009-08-28 15:46:53 +1000601 struct drm_device *dev = fb_helper->dev;
602 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700603 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700604 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000605
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200606 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700607 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000608 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000609
Dave Airlie8be48d92010-03-30 05:34:14 +0000610 if (!crtc->enabled)
611 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000612
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100613 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000614 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700615 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200616 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500617 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100618 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700619 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000620 }
Daniel Vetter84849902012-12-02 00:28:11 +0100621 drm_modeset_unlock_all(dev);
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +0200622}
623
624static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
625{
626 struct drm_fb_helper *fb_helper = info->par;
627
628 /*
629 * For each CRTC in this fb, turn the connectors on/off.
630 */
631 mutex_lock(&fb_helper->lock);
632 if (!drm_fb_helper_is_bound(fb_helper)) {
633 mutex_unlock(&fb_helper->lock);
634 return;
635 }
636
637 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
638 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
639 else
640 dpms_legacy(fb_helper, dpms_mode);
Thierry Redinge9827d82017-07-04 17:18:23 +0200641 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000642}
643
Daniel Vetter207fd322013-01-20 22:13:14 +0100644/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100645 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100646 * @blank: desired blanking state
647 * @info: fbdev registered by the helper
648 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000649int drm_fb_helper_blank(int blank, struct fb_info *info)
650{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200651 if (oops_in_progress)
652 return -EBUSY;
653
Dave Airlie785b93e2009-08-28 15:46:53 +1000654 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000655 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000656 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100657 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000658 break;
James Simmons731b5a12009-10-29 20:39:07 +0000659 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000660 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100661 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000662 break;
James Simmons731b5a12009-10-29 20:39:07 +0000663 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000664 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100665 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000666 break;
James Simmons731b5a12009-10-29 20:39:07 +0000667 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000668 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100669 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000670 break;
James Simmons731b5a12009-10-29 20:39:07 +0000671 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000672 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100673 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000674 break;
675 }
676 return 0;
677}
678EXPORT_SYMBOL(drm_fb_helper_blank);
679
Ville Syrjäläa2889602016-10-26 17:41:18 +0300680static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
681 struct drm_mode_set *modeset)
682{
683 int i;
684
685 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100686 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300687 modeset->connectors[i] = NULL;
688 }
689 modeset->num_connectors = 0;
690
691 drm_mode_destroy(helper->dev, modeset->mode);
692 modeset->mode = NULL;
693
694 /* FIXME should hold a ref? */
695 modeset->fb = NULL;
696}
697
Dave Airlie785b93e2009-08-28 15:46:53 +1000698static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
699{
700 int i;
701
Dave Airlie6e86d582016-04-27 11:24:51 +1000702 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100703 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000704 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000705 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000706 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300707
Sascha Hauera1b77362012-02-01 11:38:22 +0100708 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300709 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
710
711 drm_fb_helper_modeset_release(helper, modeset);
712 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100713 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000714 kfree(helper->crtc_info);
715}
716
Noralf Trønnescfe63422016-08-23 13:54:06 +0200717static void drm_fb_helper_resume_worker(struct work_struct *work)
718{
719 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
720 resume_work);
721
722 console_lock();
723 fb_set_suspend(helper->fbdev, 0);
724 console_unlock();
725}
726
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200727static void drm_fb_helper_dirty_work(struct work_struct *work)
728{
729 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
730 dirty_work);
731 struct drm_clip_rect *clip = &helper->dirty_clip;
732 struct drm_clip_rect clip_copy;
733 unsigned long flags;
734
735 spin_lock_irqsave(&helper->dirty_lock, flags);
736 clip_copy = *clip;
737 clip->x1 = clip->y1 = ~0;
738 clip->x2 = clip->y2 = 0;
739 spin_unlock_irqrestore(&helper->dirty_lock, flags);
740
Takashi Iwai87d3b652016-10-20 17:05:30 +0200741 /* call dirty callback only when it has been really touched */
742 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
743 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200744}
745
Daniel Vetter207fd322013-01-20 22:13:14 +0100746/**
Thierry Reding10a23102014-06-27 17:19:24 +0200747 * drm_fb_helper_prepare - setup a drm_fb_helper structure
748 * @dev: DRM device
749 * @helper: driver-allocated fbdev helper structure to set up
750 * @funcs: pointer to structure of functions associate with this helper
751 *
752 * Sets up the bare minimum to make the framebuffer helper usable. This is
753 * useful to implement race-free initialization of the polling helpers.
754 */
755void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
756 const struct drm_fb_helper_funcs *funcs)
757{
758 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200759 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200760 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200761 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
762 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200763 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200764 helper->funcs = funcs;
765 helper->dev = dev;
766}
767EXPORT_SYMBOL(drm_fb_helper_prepare);
768
769/**
Daniel Vettered84e252017-02-07 15:10:49 +0100770 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100771 * @dev: drm device
772 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100773 * @max_conn_count: max connector count
774 *
775 * This allocates the structures for the fbdev helper with the given limits.
776 * Note that this won't yet touch the hardware (through the driver interfaces)
777 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
778 * to allow driver writes more control over the exact init sequence.
779 *
Thierry Reding10a23102014-06-27 17:19:24 +0200780 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100781 *
782 * RETURNS:
783 * Zero if everything went ok, nonzero otherwise.
784 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000785int drm_fb_helper_init(struct drm_device *dev,
786 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200787 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000788{
Dave Airlie785b93e2009-08-28 15:46:53 +1000789 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200790 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000791 int i;
792
Daniel Vetterf64c5572015-08-25 15:45:13 +0200793 if (!drm_fbdev_emulation)
794 return 0;
795
Xiubo Li04cfe972014-03-10 09:33:58 +0800796 if (!max_conn_count)
797 return -EINVAL;
798
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200799 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000800 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000801 return -ENOMEM;
802
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200803 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000804 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
805 if (!fb_helper->connector_info) {
806 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000807 return -ENOMEM;
808 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000809 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000810 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000811
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200812 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000813 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000814 kcalloc(max_conn_count,
815 sizeof(struct drm_connector *),
816 GFP_KERNEL);
817
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200818 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000819 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000820 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000821 }
822
823 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200824 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000825 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000826 i++;
827 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100828
Dave Airlie785b93e2009-08-28 15:46:53 +1000829 return 0;
830out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000831 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000832 return -ENOMEM;
833}
Dave Airlie4abe3522010-03-30 05:34:18 +0000834EXPORT_SYMBOL(drm_fb_helper_init);
835
Archit Tanejab8017d62015-07-22 14:57:56 +0530836/**
837 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
838 * @fb_helper: driver-allocated fbdev helper
839 *
840 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100841 * by the driver within the fb_probe fb_helper callback function. Drivers do not
842 * need to release the allocated fb_info structure themselves, this is
843 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530844 *
845 * RETURNS:
846 * fb_info pointer if things went okay, pointer containing error code
847 * otherwise
848 */
849struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
850{
851 struct device *dev = fb_helper->dev->dev;
852 struct fb_info *info;
853 int ret;
854
855 info = framebuffer_alloc(0, dev);
856 if (!info)
857 return ERR_PTR(-ENOMEM);
858
859 ret = fb_alloc_cmap(&info->cmap, 256, 0);
860 if (ret)
861 goto err_release;
862
863 info->apertures = alloc_apertures(1);
864 if (!info->apertures) {
865 ret = -ENOMEM;
866 goto err_free_cmap;
867 }
868
869 fb_helper->fbdev = info;
870
871 return info;
872
873err_free_cmap:
874 fb_dealloc_cmap(&info->cmap);
875err_release:
876 framebuffer_release(info);
877 return ERR_PTR(ret);
878}
879EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
880
881/**
882 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
883 * @fb_helper: driver-allocated fbdev helper
884 *
885 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100886 * framebuffer device. This must be called before releasing all resources for
887 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530888 */
889void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
890{
891 if (fb_helper && fb_helper->fbdev)
892 unregister_framebuffer(fb_helper->fbdev);
893}
894EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
895
896/**
Daniel Vettered84e252017-02-07 15:10:49 +0100897 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
898 * @fb_helper: driver-allocated fbdev helper
899 *
900 * This cleans up all remaining resources associated with @fb_helper. Must be
901 * called after drm_fb_helper_unlink_fbi() was called.
902 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000903void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
904{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100905 struct fb_info *info;
906
907 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200908 return;
909
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100910 info = fb_helper->fbdev;
911 if (info) {
912 if (info->cmap.len)
913 fb_dealloc_cmap(&info->cmap);
914 framebuffer_release(info);
915 }
916 fb_helper->fbdev = NULL;
917
Chris Wilson24f76b22017-02-07 12:49:56 +0000918 cancel_work_sync(&fb_helper->resume_work);
Chris Wilsonf21b9a92017-02-07 12:49:55 +0000919 cancel_work_sync(&fb_helper->dirty_work);
920
Chris Wilsona53ca632016-11-29 12:02:17 +0000921 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000922 if (!list_empty(&fb_helper->kernel_fb_list)) {
923 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200924 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000925 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000926 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000927 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000928
Thierry Redinge9827d82017-07-04 17:18:23 +0200929 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000930 drm_fb_helper_crtc_free(fb_helper);
931
Dave Airlie4abe3522010-03-30 05:34:18 +0000932}
933EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000934
Archit Taneja47074ab2015-07-22 14:57:57 +0530935/**
936 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
937 * @fb_helper: driver-allocated fbdev helper
938 *
939 * A wrapper around unlink_framebuffer implemented by fbdev core
940 */
941void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
942{
943 if (fb_helper && fb_helper->fbdev)
944 unlink_framebuffer(fb_helper->fbdev);
945}
946EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
947
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200948static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
949 u32 width, u32 height)
950{
951 struct drm_fb_helper *helper = info->par;
952 struct drm_clip_rect *clip = &helper->dirty_clip;
953 unsigned long flags;
954
955 if (!helper->fb->funcs->dirty)
956 return;
957
958 spin_lock_irqsave(&helper->dirty_lock, flags);
959 clip->x1 = min_t(u32, clip->x1, x);
960 clip->y1 = min_t(u32, clip->y1, y);
961 clip->x2 = max_t(u32, clip->x2, x + width);
962 clip->y2 = max_t(u32, clip->y2, y + height);
963 spin_unlock_irqrestore(&helper->dirty_lock, flags);
964
965 schedule_work(&helper->dirty_work);
966}
967
968/**
969 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
970 * @info: fb_info struct pointer
971 * @pagelist: list of dirty mmap framebuffer pages
972 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100973 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200974 * callback function for flushing the fbdev mmap writes.
975 */
976void drm_fb_helper_deferred_io(struct fb_info *info,
977 struct list_head *pagelist)
978{
979 unsigned long start, end, min, max;
980 struct page *page;
981 u32 y1, y2;
982
983 min = ULONG_MAX;
984 max = 0;
985 list_for_each_entry(page, pagelist, lru) {
986 start = page->index << PAGE_SHIFT;
987 end = start + PAGE_SIZE - 1;
988 min = min(min, start);
989 max = max(max, end);
990 }
991
992 if (min < max) {
993 y1 = min / info->fix.line_length;
994 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
995 info->var.yres);
996 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
997 }
998}
999EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1000
Archit Tanejacbb1a822015-07-31 16:21:41 +05301001/**
1002 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1003 * @info: fb_info struct pointer
1004 * @buf: userspace buffer to read from framebuffer memory
1005 * @count: number of bytes to read from framebuffer memory
1006 * @ppos: read offset within framebuffer memory
1007 *
1008 * A wrapper around fb_sys_read implemented by fbdev core
1009 */
1010ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1011 size_t count, loff_t *ppos)
1012{
1013 return fb_sys_read(info, buf, count, ppos);
1014}
1015EXPORT_SYMBOL(drm_fb_helper_sys_read);
1016
1017/**
1018 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1019 * @info: fb_info struct pointer
1020 * @buf: userspace buffer to write to framebuffer memory
1021 * @count: number of bytes to write to framebuffer memory
1022 * @ppos: write offset within framebuffer memory
1023 *
1024 * A wrapper around fb_sys_write implemented by fbdev core
1025 */
1026ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1027 size_t count, loff_t *ppos)
1028{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001029 ssize_t ret;
1030
1031 ret = fb_sys_write(info, buf, count, ppos);
1032 if (ret > 0)
1033 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1034 info->var.yres);
1035
1036 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301037}
1038EXPORT_SYMBOL(drm_fb_helper_sys_write);
1039
Archit Taneja742547b2015-07-31 16:21:42 +05301040/**
1041 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1042 * @info: fbdev registered by the helper
1043 * @rect: info about rectangle to fill
1044 *
1045 * A wrapper around sys_fillrect implemented by fbdev core
1046 */
1047void drm_fb_helper_sys_fillrect(struct fb_info *info,
1048 const struct fb_fillrect *rect)
1049{
1050 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001051 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1052 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301053}
1054EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1055
1056/**
1057 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1058 * @info: fbdev registered by the helper
1059 * @area: info about area to copy
1060 *
1061 * A wrapper around sys_copyarea implemented by fbdev core
1062 */
1063void drm_fb_helper_sys_copyarea(struct fb_info *info,
1064 const struct fb_copyarea *area)
1065{
1066 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001067 drm_fb_helper_dirty(info, area->dx, area->dy,
1068 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301069}
1070EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1071
1072/**
1073 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1074 * @info: fbdev registered by the helper
1075 * @image: info about image to blit
1076 *
1077 * A wrapper around sys_imageblit implemented by fbdev core
1078 */
1079void drm_fb_helper_sys_imageblit(struct fb_info *info,
1080 const struct fb_image *image)
1081{
1082 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001083 drm_fb_helper_dirty(info, image->dx, image->dy,
1084 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301085}
1086EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1087
1088/**
1089 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1090 * @info: fbdev registered by the helper
1091 * @rect: info about rectangle to fill
1092 *
1093 * A wrapper around cfb_imageblit implemented by fbdev core
1094 */
1095void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1096 const struct fb_fillrect *rect)
1097{
1098 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001099 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1100 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301101}
1102EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1103
1104/**
1105 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1106 * @info: fbdev registered by the helper
1107 * @area: info about area to copy
1108 *
1109 * A wrapper around cfb_copyarea implemented by fbdev core
1110 */
1111void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1112 const struct fb_copyarea *area)
1113{
1114 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001115 drm_fb_helper_dirty(info, area->dx, area->dy,
1116 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301117}
1118EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1119
1120/**
1121 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1122 * @info: fbdev registered by the helper
1123 * @image: info about image to blit
1124 *
1125 * A wrapper around cfb_imageblit implemented by fbdev core
1126 */
1127void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1128 const struct fb_image *image)
1129{
1130 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001131 drm_fb_helper_dirty(info, image->dx, image->dy,
1132 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301133}
1134EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1135
Archit Tanejafdefa582015-07-31 16:21:43 +05301136/**
1137 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1138 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001139 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301140 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001141 * A wrapper around fb_set_suspend implemented by fbdev core.
1142 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1143 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301144 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001145void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301146{
1147 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001148 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301149}
1150EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1151
Noralf Trønnescfe63422016-08-23 13:54:06 +02001152/**
1153 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1154 * takes the console lock
1155 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001156 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001157 *
1158 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1159 * isn't available on resume, a worker is tasked with waiting for the lock
1160 * to become available. The console lock can be pretty contented on resume
1161 * due to all the printk activity.
1162 *
1163 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001164 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001165 *
1166 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1167 */
1168void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001169 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001170{
1171 if (!fb_helper || !fb_helper->fbdev)
1172 return;
1173
1174 /* make sure there's no pending/ongoing resume */
1175 flush_work(&fb_helper->resume_work);
1176
1177 if (suspend) {
1178 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1179 return;
1180
1181 console_lock();
1182
1183 } else {
1184 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1185 return;
1186
1187 if (!console_trylock()) {
1188 schedule_work(&fb_helper->resume_work);
1189 return;
1190 }
1191 }
1192
1193 fb_set_suspend(fb_helper->fbdev, suspend);
1194 console_unlock();
1195}
1196EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1197
Peter Rosinb8e2b012017-07-04 12:36:57 +02001198static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1199{
1200 u32 *palette = (u32 *)info->pseudo_palette;
1201 int i;
1202
1203 if (cmap->start + cmap->len > 16)
1204 return -EINVAL;
1205
1206 for (i = 0; i < cmap->len; ++i) {
1207 u16 red = cmap->red[i];
1208 u16 green = cmap->green[i];
1209 u16 blue = cmap->blue[i];
1210 u32 value;
1211
1212 red >>= 16 - info->var.red.length;
1213 green >>= 16 - info->var.green.length;
1214 blue >>= 16 - info->var.blue.length;
1215 value = (red << info->var.red.offset) |
1216 (green << info->var.green.offset) |
1217 (blue << info->var.blue.offset);
1218 if (info->var.transp.length > 0) {
1219 u32 mask = (1 << info->var.transp.length) - 1;
1220
1221 mask <<= info->var.transp.offset;
1222 value |= mask;
1223 }
1224 palette[cmap->start + i] = value;
1225 }
1226
1227 return 0;
1228}
1229
Peter Rosin964c6002017-07-13 18:25:27 +02001230static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
Dave Airlie068143d2009-10-05 09:58:02 +10001231{
1232 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie068143d2009-10-05 09:58:02 +10001233 struct drm_crtc *crtc;
Peter Rosina3562a02017-07-04 12:36:58 +02001234 u16 *r, *g, *b;
Peter Rosin964c6002017-07-13 18:25:27 +02001235 int i, ret = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001236
Peter Rosin964c6002017-07-13 18:25:27 +02001237 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001238 for (i = 0; i < fb_helper->crtc_count; i++) {
1239 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Peter Rosin964c6002017-07-13 18:25:27 +02001240 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1241 return -EINVAL;
Dave Airlie068143d2009-10-05 09:58:02 +10001242
Peter Rosin964c6002017-07-13 18:25:27 +02001243 if (cmap->start + cmap->len > crtc->gamma_size)
1244 return -EINVAL;
Peter Rosina3562a02017-07-04 12:36:58 +02001245
1246 r = crtc->gamma_store;
1247 g = r + crtc->gamma_size;
1248 b = g + crtc->gamma_size;
1249
1250 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1251 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1252 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1253
Peter Rosin964c6002017-07-13 18:25:27 +02001254 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1255 crtc->gamma_size, NULL);
1256 if (ret)
1257 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001258 }
Peter Rosin964c6002017-07-13 18:25:27 +02001259 drm_modeset_unlock_all(fb_helper->dev);
1260
1261 return ret;
1262}
1263
1264static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1265 struct fb_cmap *cmap)
1266{
1267 struct drm_device *dev = crtc->dev;
1268 struct drm_property_blob *gamma_lut;
1269 struct drm_color_lut *lut;
1270 int size = crtc->gamma_size;
1271 int i;
1272
1273 if (!size || cmap->start + cmap->len > size)
1274 return ERR_PTR(-EINVAL);
1275
1276 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1277 if (IS_ERR(gamma_lut))
1278 return gamma_lut;
1279
1280 lut = (struct drm_color_lut *)gamma_lut->data;
1281 if (cmap->start || cmap->len != size) {
1282 u16 *r = crtc->gamma_store;
1283 u16 *g = r + crtc->gamma_size;
1284 u16 *b = g + crtc->gamma_size;
1285
1286 for (i = 0; i < cmap->start; i++) {
1287 lut[i].red = r[i];
1288 lut[i].green = g[i];
1289 lut[i].blue = b[i];
1290 }
1291 for (i = cmap->start + cmap->len; i < size; i++) {
1292 lut[i].red = r[i];
1293 lut[i].green = g[i];
1294 lut[i].blue = b[i];
1295 }
1296 }
1297
1298 for (i = 0; i < cmap->len; i++) {
1299 lut[cmap->start + i].red = cmap->red[i];
1300 lut[cmap->start + i].green = cmap->green[i];
1301 lut[cmap->start + i].blue = cmap->blue[i];
1302 }
1303
1304 return gamma_lut;
1305}
1306
1307static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1308{
1309 struct drm_fb_helper *fb_helper = info->par;
1310 struct drm_device *dev = fb_helper->dev;
1311 struct drm_property_blob *gamma_lut = NULL;
1312 struct drm_modeset_acquire_ctx ctx;
1313 struct drm_crtc_state *crtc_state;
1314 struct drm_atomic_state *state;
1315 struct drm_crtc *crtc;
1316 u16 *r, *g, *b;
1317 int i, ret = 0;
1318 bool replaced;
1319
1320 drm_modeset_acquire_init(&ctx, 0);
1321
1322 state = drm_atomic_state_alloc(dev);
1323 if (!state) {
1324 ret = -ENOMEM;
1325 goto out_ctx;
1326 }
1327
1328 state->acquire_ctx = &ctx;
1329retry:
1330 for (i = 0; i < fb_helper->crtc_count; i++) {
1331 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1332
1333 if (!gamma_lut)
1334 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1335 if (IS_ERR(gamma_lut)) {
1336 ret = PTR_ERR(gamma_lut);
1337 gamma_lut = NULL;
1338 goto out_state;
1339 }
1340
1341 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1342 if (IS_ERR(crtc_state)) {
1343 ret = PTR_ERR(crtc_state);
1344 goto out_state;
1345 }
1346
1347 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1348 NULL);
1349 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1350 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1351 gamma_lut);
1352 crtc_state->color_mgmt_changed |= replaced;
1353 }
1354
1355 ret = drm_atomic_commit(state);
1356 if (ret)
1357 goto out_state;
1358
1359 for (i = 0; i < fb_helper->crtc_count; i++) {
1360 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1361
1362 r = crtc->gamma_store;
1363 g = r + crtc->gamma_size;
1364 b = g + crtc->gamma_size;
1365
1366 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1367 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1368 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1369 }
1370
1371out_state:
1372 if (ret == -EDEADLK)
1373 goto backoff;
1374
1375 drm_property_blob_put(gamma_lut);
1376 drm_atomic_state_put(state);
1377out_ctx:
1378 drm_modeset_drop_locks(&ctx);
1379 drm_modeset_acquire_fini(&ctx);
1380
1381 return ret;
1382
1383backoff:
1384 drm_atomic_state_clear(state);
1385 drm_modeset_backoff(&ctx);
1386 goto retry;
1387}
1388
1389/**
1390 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1391 * @cmap: cmap to set
1392 * @info: fbdev registered by the helper
1393 */
1394int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1395{
1396 struct drm_fb_helper *fb_helper = info->par;
1397 int ret;
1398
1399 if (oops_in_progress)
1400 return -EBUSY;
1401
1402 mutex_lock(&fb_helper->lock);
1403
1404 if (!drm_fb_helper_is_bound(fb_helper)) {
1405 ret = -EBUSY;
1406 goto out;
1407 }
1408
1409 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1410 ret = setcmap_pseudo_palette(cmap, info);
1411 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1412 ret = setcmap_atomic(cmap, info);
1413 else
1414 ret = setcmap_legacy(cmap, info);
1415
1416out:
Thierry Redinge9827d82017-07-04 17:18:23 +02001417 mutex_unlock(&fb_helper->lock);
Peter Rosin964c6002017-07-13 18:25:27 +02001418
1419 return ret;
Dave Airlie068143d2009-10-05 09:58:02 +10001420}
1421EXPORT_SYMBOL(drm_fb_helper_setcmap);
1422
Daniel Vetter207fd322013-01-20 22:13:14 +01001423/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001424 * drm_fb_helper_ioctl - legacy ioctl implementation
1425 * @info: fbdev registered by the helper
1426 * @cmd: ioctl command
1427 * @arg: ioctl argument
1428 *
1429 * A helper to implement the standard fbdev ioctl. Only
1430 * FBIO_WAITFORVSYNC is implemented for now.
1431 */
1432int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1433 unsigned long arg)
1434{
1435 struct drm_fb_helper *fb_helper = info->par;
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001436 struct drm_mode_set *mode_set;
1437 struct drm_crtc *crtc;
1438 int ret = 0;
1439
Thierry Redinge9827d82017-07-04 17:18:23 +02001440 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001441 if (!drm_fb_helper_is_bound(fb_helper)) {
1442 ret = -EBUSY;
1443 goto unlock;
1444 }
1445
1446 switch (cmd) {
1447 case FBIO_WAITFORVSYNC:
1448 /*
1449 * Only consider the first CRTC.
1450 *
1451 * This ioctl is supposed to take the CRTC number as
1452 * an argument, but in fbdev times, what that number
1453 * was supposed to be was quite unclear, different
1454 * drivers were passing that argument differently
1455 * (some by reference, some by value), and most of the
1456 * userspace applications were just hardcoding 0 as an
1457 * argument.
1458 *
1459 * The first CRTC should be the integrated panel on
1460 * most drivers, so this is the best choice we can
1461 * make. If we're not smart enough here, one should
1462 * just consider switch the userspace to KMS.
1463 */
1464 mode_set = &fb_helper->crtc_info[0].mode_set;
1465 crtc = mode_set->crtc;
1466
1467 /*
1468 * Only wait for a vblank event if the CRTC is
1469 * enabled, otherwise just don't do anythintg,
1470 * not even report an error.
1471 */
1472 ret = drm_crtc_vblank_get(crtc);
1473 if (!ret) {
1474 drm_crtc_wait_one_vblank(crtc);
1475 drm_crtc_vblank_put(crtc);
1476 }
1477
1478 ret = 0;
1479 goto unlock;
1480 default:
1481 ret = -ENOTTY;
1482 }
1483
1484unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001485 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001486 return ret;
1487}
1488EXPORT_SYMBOL(drm_fb_helper_ioctl);
1489
1490/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001491 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001492 * @var: screeninfo to check
1493 * @info: fbdev registered by the helper
1494 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001495int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1496 struct fb_info *info)
1497{
1498 struct drm_fb_helper *fb_helper = info->par;
1499 struct drm_framebuffer *fb = fb_helper->fb;
1500 int depth;
1501
Jason Wesself90ebd92010-08-05 09:22:32 -05001502 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001503 return -EINVAL;
1504
Stefan Agner865afb12016-10-11 16:15:04 -07001505 /*
1506 * Changes struct fb_var_screeninfo are currently not pushed back
1507 * to KMS, hence fail if different settings are requested.
1508 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001509 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001510 var->xres > fb->width || var->yres > fb->height ||
1511 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1512 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001513 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1514 var->xres, var->yres, var->bits_per_pixel,
1515 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001516 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001517 return -EINVAL;
1518 }
1519
1520 switch (var->bits_per_pixel) {
1521 case 16:
1522 depth = (var->green.length == 6) ? 16 : 15;
1523 break;
1524 case 32:
1525 depth = (var->transp.length > 0) ? 32 : 24;
1526 break;
1527 default:
1528 depth = var->bits_per_pixel;
1529 break;
1530 }
1531
1532 switch (depth) {
1533 case 8:
1534 var->red.offset = 0;
1535 var->green.offset = 0;
1536 var->blue.offset = 0;
1537 var->red.length = 8;
1538 var->green.length = 8;
1539 var->blue.length = 8;
1540 var->transp.length = 0;
1541 var->transp.offset = 0;
1542 break;
1543 case 15:
1544 var->red.offset = 10;
1545 var->green.offset = 5;
1546 var->blue.offset = 0;
1547 var->red.length = 5;
1548 var->green.length = 5;
1549 var->blue.length = 5;
1550 var->transp.length = 1;
1551 var->transp.offset = 15;
1552 break;
1553 case 16:
1554 var->red.offset = 11;
1555 var->green.offset = 5;
1556 var->blue.offset = 0;
1557 var->red.length = 5;
1558 var->green.length = 6;
1559 var->blue.length = 5;
1560 var->transp.length = 0;
1561 var->transp.offset = 0;
1562 break;
1563 case 24:
1564 var->red.offset = 16;
1565 var->green.offset = 8;
1566 var->blue.offset = 0;
1567 var->red.length = 8;
1568 var->green.length = 8;
1569 var->blue.length = 8;
1570 var->transp.length = 0;
1571 var->transp.offset = 0;
1572 break;
1573 case 32:
1574 var->red.offset = 16;
1575 var->green.offset = 8;
1576 var->blue.offset = 0;
1577 var->red.length = 8;
1578 var->green.length = 8;
1579 var->blue.length = 8;
1580 var->transp.length = 8;
1581 var->transp.offset = 24;
1582 break;
1583 default:
1584 return -EINVAL;
1585 }
1586 return 0;
1587}
1588EXPORT_SYMBOL(drm_fb_helper_check_var);
1589
Daniel Vetter207fd322013-01-20 22:13:14 +01001590/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001591 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001592 * @info: fbdev registered by the helper
1593 *
1594 * This will let fbcon do the mode init and is called at initialization time by
1595 * the fbdev core when registering the driver, and later on through the hotplug
1596 * callback.
1597 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001598int drm_fb_helper_set_par(struct fb_info *info)
1599{
1600 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001601 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001602
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001603 if (oops_in_progress)
1604 return -EBUSY;
1605
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001606 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001607 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001608 return -EINVAL;
1609 }
1610
Rob Clark5ea1f752014-05-30 12:29:48 -04001611 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001612
Dave Airlie785b93e2009-08-28 15:46:53 +10001613 return 0;
1614}
1615EXPORT_SYMBOL(drm_fb_helper_set_par);
1616
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001617static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
Rob Clark1edf0262015-08-25 15:35:59 -04001618{
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001619 int i;
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001620
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001621 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001622 struct drm_mode_set *mode_set;
1623
1624 mode_set = &fb_helper->crtc_info[i].mode_set;
1625
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001626 mode_set->x = x;
1627 mode_set->y = y;
Rob Clark1edf0262015-08-25 15:35:59 -04001628 }
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001629}
Rob Clark1edf0262015-08-25 15:35:59 -04001630
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001631static int pan_display_atomic(struct fb_var_screeninfo *var,
1632 struct fb_info *info)
1633{
1634 struct drm_fb_helper *fb_helper = info->par;
1635 int ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001636
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001637 pan_set(fb_helper, var->xoffset, var->yoffset);
Rob Clark1edf0262015-08-25 15:35:59 -04001638
Daniel Vetter6b7dc6e2017-07-04 17:18:29 +02001639 ret = restore_fbdev_mode_atomic(fb_helper, true);
1640 if (!ret) {
1641 info->var.xoffset = var->xoffset;
1642 info->var.yoffset = var->yoffset;
1643 } else
1644 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001645
Rob Clark1edf0262015-08-25 15:35:59 -04001646 return ret;
Rob Clark1edf0262015-08-25 15:35:59 -04001647}
1648
Daniel Vetter71286452017-04-03 10:33:04 +02001649static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001650 struct fb_info *info)
1651{
1652 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001653 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001654 int ret = 0;
1655 int i;
1656
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001657 drm_modeset_lock_all(fb_helper->dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001658 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001659 modeset = &fb_helper->crtc_info[i].mode_set;
1660
1661 modeset->x = var->xoffset;
1662 modeset->y = var->yoffset;
1663
1664 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001665 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001666 if (!ret) {
1667 info->var.xoffset = var->xoffset;
1668 info->var.yoffset = var->yoffset;
1669 }
1670 }
1671 }
Daniel Vetter5c2e3442017-07-04 17:18:26 +02001672 drm_modeset_unlock_all(fb_helper->dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001673
1674 return ret;
1675}
1676
1677/**
1678 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1679 * @var: updated screen information
1680 * @info: fbdev registered by the helper
1681 */
1682int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1683 struct fb_info *info)
1684{
1685 struct drm_fb_helper *fb_helper = info->par;
1686 struct drm_device *dev = fb_helper->dev;
1687 int ret;
1688
1689 if (oops_in_progress)
1690 return -EBUSY;
1691
Thierry Redinge9827d82017-07-04 17:18:23 +02001692 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001693 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001694 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001695 return -EBUSY;
1696 }
1697
1698 if (drm_drv_uses_atomic_modeset(dev))
1699 ret = pan_display_atomic(var, info);
1700 else
1701 ret = pan_display_legacy(var, info);
Thierry Redinge9827d82017-07-04 17:18:23 +02001702 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001703
Dave Airlie785b93e2009-08-28 15:46:53 +10001704 return ret;
1705}
1706EXPORT_SYMBOL(drm_fb_helper_pan_display);
1707
Daniel Vetter8acf6582013-01-21 23:38:37 +01001708/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001709 * Allocates the backing storage and sets up the fbdev info structure through
1710 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1711 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001712 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001713static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1714 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001715{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001716 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001717 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001718 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001719 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001720 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001721
1722 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1723 sizes.surface_depth = 24;
1724 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001725 sizes.fb_width = (u32)-1;
1726 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001727
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001728 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001729 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001730 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001731
Dave Airlie785b93e2009-08-28 15:46:53 +10001732 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001733 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001734 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001735 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001736
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001737 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001738
1739 if (cmdline_mode->bpp_specified) {
1740 switch (cmdline_mode->bpp) {
1741 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001742 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001743 break;
1744 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001745 sizes.surface_depth = 15;
1746 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001747 break;
1748 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001749 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001750 break;
1751 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001752 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001753 break;
1754 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001755 sizes.surface_depth = 24;
1756 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001757 break;
1758 }
1759 break;
1760 }
1761 }
1762
Dave Airlie8be48d92010-03-30 05:34:14 +00001763 crtc_count = 0;
1764 for (i = 0; i < fb_helper->crtc_count; i++) {
1765 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001766 struct drm_mode_set *mode_set;
1767 int x, y, j;
1768 /* in case of tile group, are we the last tile vert or horiz?
1769 * If no tile group you are always the last one both vertically
1770 * and horizontally
1771 */
1772 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001773
Dave Airlie8be48d92010-03-30 05:34:14 +00001774 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001775 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001776
1777 if (!desired_mode)
1778 continue;
1779
1780 crtc_count++;
1781
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001782 x = fb_helper->crtc_info[i].x;
1783 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001784
1785 if (gamma_size == 0)
1786 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1787
1788 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1789 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001790
1791 for (j = 0; j < mode_set->num_connectors; j++) {
1792 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001793
Rob Clark0e3704c2015-03-11 10:23:14 -04001794 if (connector->has_tile) {
1795 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1796 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1797 /* cloning to multiple tiles is just crazy-talk, so: */
1798 break;
1799 }
1800 }
1801
1802 if (lasth)
1803 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1804 if (lastv)
1805 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001806 }
1807
Dave Airlie38651672010-03-30 05:34:13 +00001808 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001809 /*
1810 * hmm everyone went away - assume VGA cable just fell out
1811 * and will come back later.
1812 */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001813 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001814 sizes.fb_width = sizes.surface_width = 1024;
1815 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001816 }
1817
Xinliang Liu5f152572017-02-15 17:19:08 +01001818 /* Handle our overallocation */
1819 sizes.surface_height *= drm_fbdev_overalloc;
1820 sizes.surface_height /= 100;
1821
Dave Airlie38651672010-03-30 05:34:13 +00001822 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001823 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1824 if (ret < 0)
1825 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001826
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001827 /*
1828 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1829 * events, but at init time drm_setup_crtcs needs to be called before
1830 * the fb is allocated (since we need to figure out the desired size of
1831 * the fb before we can allocate it ...). Hence we need to fix things up
1832 * here again.
1833 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001834 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001835 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1836 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1837
Dave Airlie785b93e2009-08-28 15:46:53 +10001838 return 0;
1839}
Dave Airlie785b93e2009-08-28 15:46:53 +10001840
Daniel Vetter207fd322013-01-20 22:13:14 +01001841/**
1842 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1843 * @info: fbdev registered by the helper
1844 * @pitch: desired pitch
1845 * @depth: desired depth
1846 *
1847 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1848 * fbdev emulations. Drivers which support acceleration methods which impose
1849 * additional constraints need to set up their own limits.
1850 *
1851 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001852 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001853 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001854void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1855 uint32_t depth)
1856{
1857 info->fix.type = FB_TYPE_PACKED_PIXELS;
1858 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1859 FB_VISUAL_TRUECOLOR;
1860 info->fix.mmio_start = 0;
1861 info->fix.mmio_len = 0;
1862 info->fix.type_aux = 0;
1863 info->fix.xpanstep = 1; /* doing it in hw */
1864 info->fix.ypanstep = 1; /* doing it in hw */
1865 info->fix.ywrapstep = 0;
1866 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001867
1868 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001869}
1870EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1871
Daniel Vetter207fd322013-01-20 22:13:14 +01001872/**
1873 * drm_fb_helper_fill_var - initalizes variable fbdev information
1874 * @info: fbdev instance to set up
1875 * @fb_helper: fb helper instance to use as template
1876 * @fb_width: desired fb width
1877 * @fb_height: desired fb height
1878 *
1879 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001880 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001881 *
1882 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001883 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1884 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001885 */
Dave Airlie38651672010-03-30 05:34:13 +00001886void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001887 uint32_t fb_width, uint32_t fb_height)
1888{
Dave Airlie38651672010-03-30 05:34:13 +00001889 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001890
Dave Airlie38651672010-03-30 05:34:13 +00001891 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001892 info->var.xres_virtual = fb->width;
1893 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001894 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001895 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001896 info->var.xoffset = 0;
1897 info->var.yoffset = 0;
1898 info->var.activate = FB_ACTIVATE_NOW;
1899 info->var.height = -1;
1900 info->var.width = -1;
1901
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001902 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001903 case 8:
1904 info->var.red.offset = 0;
1905 info->var.green.offset = 0;
1906 info->var.blue.offset = 0;
1907 info->var.red.length = 8; /* 8bit DAC */
1908 info->var.green.length = 8;
1909 info->var.blue.length = 8;
1910 info->var.transp.offset = 0;
1911 info->var.transp.length = 0;
1912 break;
1913 case 15:
1914 info->var.red.offset = 10;
1915 info->var.green.offset = 5;
1916 info->var.blue.offset = 0;
1917 info->var.red.length = 5;
1918 info->var.green.length = 5;
1919 info->var.blue.length = 5;
1920 info->var.transp.offset = 15;
1921 info->var.transp.length = 1;
1922 break;
1923 case 16:
1924 info->var.red.offset = 11;
1925 info->var.green.offset = 5;
1926 info->var.blue.offset = 0;
1927 info->var.red.length = 5;
1928 info->var.green.length = 6;
1929 info->var.blue.length = 5;
1930 info->var.transp.offset = 0;
1931 break;
1932 case 24:
1933 info->var.red.offset = 16;
1934 info->var.green.offset = 8;
1935 info->var.blue.offset = 0;
1936 info->var.red.length = 8;
1937 info->var.green.length = 8;
1938 info->var.blue.length = 8;
1939 info->var.transp.offset = 0;
1940 info->var.transp.length = 0;
1941 break;
1942 case 32:
1943 info->var.red.offset = 16;
1944 info->var.green.offset = 8;
1945 info->var.blue.offset = 0;
1946 info->var.red.length = 8;
1947 info->var.green.length = 8;
1948 info->var.blue.length = 8;
1949 info->var.transp.offset = 24;
1950 info->var.transp.length = 8;
1951 break;
1952 default:
1953 break;
1954 }
1955
1956 info->var.xres = fb_width;
1957 info->var.yres = fb_height;
1958}
1959EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001960
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001961static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
Daniel Vettere13a0582017-07-05 06:56:29 +02001962 uint32_t maxX,
1963 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001964{
1965 struct drm_connector *connector;
Daniel Vettere13a0582017-07-05 06:56:29 +02001966 int i, count = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001967
Chris Wilson966a6a12016-11-29 12:02:15 +00001968 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001969 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001970 count += connector->funcs->fill_modes(connector, maxX, maxY);
1971 }
1972
1973 return count;
1974}
1975
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001976struct 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 +00001977{
1978 struct drm_display_mode *mode;
1979
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001980 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001981 if (mode->hdisplay > width ||
1982 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001983 continue;
1984 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1985 return mode;
1986 }
1987 return NULL;
1988}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001989EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001990
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001991static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001992{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001993 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001994}
1995
Vincent Abrioua09759e2017-01-06 17:44:43 +01001996struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00001997{
Chris Wilson1794d252011-04-17 07:43:32 +01001998 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001999 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01002000 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00002001
Chris Wilsoneaf99c72014-08-06 10:08:32 +02002002 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00002003 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00002004 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00002005
2006 /* attempt to find a matching mode in the list of modes
2007 * we have gotten so far, if not add a CVT mode that conforms
2008 */
2009 if (cmdline_mode->rb || cmdline_mode->margins)
2010 goto create_mode;
2011
Takashi Iwaic683f422014-03-19 14:53:13 +01002012 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00002013again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002014 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00002015 /* check width/height */
2016 if (mode->hdisplay != cmdline_mode->xres ||
2017 mode->vdisplay != cmdline_mode->yres)
2018 continue;
2019
2020 if (cmdline_mode->refresh_specified) {
2021 if (mode->vrefresh != cmdline_mode->refresh)
2022 continue;
2023 }
2024
2025 if (cmdline_mode->interlace) {
2026 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2027 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01002028 } else if (prefer_non_interlace) {
2029 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2030 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002031 }
2032 return mode;
2033 }
2034
Takashi Iwaic683f422014-03-19 14:53:13 +01002035 if (prefer_non_interlace) {
2036 prefer_non_interlace = false;
2037 goto again;
2038 }
2039
Dave Airlie38651672010-03-30 05:34:13 +00002040create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01002041 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2042 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002043 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00002044 return mode;
2045}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08002046EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00002047
2048static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2049{
2050 bool enable;
2051
Sachin Kamat96081cd2012-11-15 03:43:30 +00002052 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00002053 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002054 else
Dave Airlie38651672010-03-30 05:34:13 +00002055 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00002056
Dave Airlie38651672010-03-30 05:34:13 +00002057 return enable;
2058}
2059
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002060static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2061 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00002062{
2063 bool any_enabled = false;
2064 struct drm_connector *connector;
2065 int i = 0;
2066
Chris Wilson966a6a12016-11-29 12:02:15 +00002067 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002068 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002069 enabled[i] = drm_connector_enabled(connector, true);
2070 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
2071 enabled[i] ? "yes" : "no");
2072 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00002073 }
2074
2075 if (any_enabled)
2076 return;
2077
Chris Wilson966a6a12016-11-29 12:02:15 +00002078 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002079 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002080 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00002081 }
2082}
2083
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002084static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2085 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002086 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002087 bool *enabled, int width, int height)
2088{
2089 int count, i, j;
2090 bool can_clone = false;
2091 struct drm_fb_helper_connector *fb_helper_conn;
2092 struct drm_display_mode *dmt_mode, *mode;
2093
2094 /* only contemplate cloning in the single crtc case */
2095 if (fb_helper->crtc_count > 1)
2096 return false;
2097
2098 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002099 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002100 if (enabled[i])
2101 count++;
2102 }
2103
2104 /* only contemplate cloning if more than one connector is enabled */
2105 if (count <= 1)
2106 return false;
2107
2108 /* check the command line or if nothing common pick 1024x768 */
2109 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002110 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002111 if (!enabled[i])
2112 continue;
2113 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002114 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002115 if (!modes[i]) {
2116 can_clone = false;
2117 break;
2118 }
2119 for (j = 0; j < i; j++) {
2120 if (!enabled[j])
2121 continue;
2122 if (!drm_mode_equal(modes[j], modes[i]))
2123 can_clone = false;
2124 }
2125 }
2126
2127 if (can_clone) {
2128 DRM_DEBUG_KMS("can clone using command line\n");
2129 return true;
2130 }
2131
2132 /* try and find a 1024x768 mode on each connector */
2133 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002134 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002135
Chris Wilson966a6a12016-11-29 12:02:15 +00002136 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002137 if (!enabled[i])
2138 continue;
2139
2140 fb_helper_conn = fb_helper->connector_info[i];
2141 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2142 if (drm_mode_equal(mode, dmt_mode))
2143 modes[i] = mode;
2144 }
2145 if (!modes[i])
2146 can_clone = false;
2147 }
2148
2149 if (can_clone) {
2150 DRM_DEBUG_KMS("can clone using 1024x768\n");
2151 return true;
2152 }
2153 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2154 return false;
2155}
2156
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002157static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2158 struct drm_display_mode **modes,
2159 struct drm_fb_offset *offsets,
2160 int idx,
2161 int h_idx, int v_idx)
2162{
2163 struct drm_fb_helper_connector *fb_helper_conn;
2164 int i;
2165 int hoffset = 0, voffset = 0;
2166
Chris Wilson966a6a12016-11-29 12:02:15 +00002167 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002168 fb_helper_conn = fb_helper->connector_info[i];
2169 if (!fb_helper_conn->connector->has_tile)
2170 continue;
2171
2172 if (!modes[i] && (h_idx || v_idx)) {
2173 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2174 fb_helper_conn->connector->base.id);
2175 continue;
2176 }
2177 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2178 hoffset += modes[i]->hdisplay;
2179
2180 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2181 voffset += modes[i]->vdisplay;
2182 }
2183 offsets[idx].x = hoffset;
2184 offsets[idx].y = voffset;
2185 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2186 return 0;
2187}
2188
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002189static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002190 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002191 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002192 bool *enabled, int width, int height)
2193{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002194 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002195 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2196 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002197 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002198 int i;
2199
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002200retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002201 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002202 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002203
Chris Wilsonc96521e2016-11-27 17:09:10 +00002204 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002205 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002206
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002207 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002208 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002209 continue;
2210 }
2211
2212 /* first pass over all the untiled connectors */
2213 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2214 continue;
2215
2216 if (tile_pass == 1) {
2217 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2218 fb_helper_conn->connector->tile_v_loc != 0)
2219 continue;
2220
2221 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002222 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002223 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2224 /* if this tile_pass doesn't cover any of the tiles - keep going */
2225 continue;
2226
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002227 /*
2228 * find the tile offsets for this pass - need to find
2229 * all tiles left and above
2230 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002231 drm_get_tile_offsets(fb_helper, modes, offsets,
2232 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2233 }
Dave Airlie38651672010-03-30 05:34:13 +00002234 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002235 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002236
2237 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002238 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002239 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002240 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2241 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 +00002242 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002243 }
2244 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002245 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2246 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002247 break;
2248 }
2249 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2250 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002251 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002252 }
2253
2254 if ((conn_configured & mask) != mask) {
2255 tile_pass++;
2256 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002257 }
2258 return true;
2259}
2260
Dave Airlie8be48d92010-03-30 05:34:14 +00002261static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2262 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002263 struct drm_display_mode **modes,
2264 int n, int width, int height)
2265{
2266 int c, o;
2267 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002268 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002269 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002270 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002271 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002272 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002273
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002274 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002275 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002276
2277 fb_helper_conn = fb_helper->connector_info[n];
2278 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002279
2280 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002281 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002282 if (modes[n] == NULL)
2283 return best_score;
2284
Lyude255f0e72016-05-12 10:56:59 -04002285 crtcs = kzalloc(fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002286 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002287 if (!crtcs)
2288 return best_score;
2289
2290 my_score = 1;
2291 if (connector->status == connector_status_connected)
2292 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002293 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002294 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002295 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002296 my_score++;
2297
2298 connector_funcs = connector->helper_private;
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002299
2300 /*
2301 * If the DRM device implements atomic hooks and ->best_encoder() is
2302 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2303 * helper.
2304 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002305 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002306 !connector_funcs->best_encoder)
2307 encoder = drm_atomic_helper_best_encoder(connector);
2308 else
2309 encoder = connector_funcs->best_encoder(connector);
2310
Dave Airlie38651672010-03-30 05:34:13 +00002311 if (!encoder)
2312 goto out;
2313
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002314 /*
2315 * select a crtc for this connector and then attempt to configure
2316 * remaining connectors
2317 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002318 for (c = 0; c < fb_helper->crtc_count; c++) {
2319 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002320
Sachin Kamat96081cd2012-11-15 03:43:30 +00002321 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002322 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002323
2324 for (o = 0; o < n; o++)
2325 if (best_crtcs[o] == crtc)
2326 break;
2327
2328 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002329 /* ignore cloning unless only a single crtc */
2330 if (fb_helper->crtc_count > 1)
2331 continue;
2332
2333 if (!drm_mode_equal(modes[o], modes[n]))
2334 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002335 }
2336
2337 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002338 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2339 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002340 width, height);
2341 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002342 best_score = score;
2343 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002344 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002345 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002346 }
Dave Airlie38651672010-03-30 05:34:13 +00002347 }
2348out:
2349 kfree(crtcs);
2350 return best_score;
2351}
2352
Chris Wilson64e94402016-11-29 12:02:16 +00002353static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2354 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002355{
Dave Airlie8be48d92010-03-30 05:34:14 +00002356 struct drm_device *dev = fb_helper->dev;
2357 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002358 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002359 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002360 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002361 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002362
2363 DRM_DEBUG_KMS("\n");
Chris Wilson966a6a12016-11-29 12:02:15 +00002364 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002365 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002366
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002367 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002368 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002369 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002370 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002371 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002372 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002373 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002374 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002375 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002376 DRM_ERROR("Memory allocation failed\n");
2377 goto out;
2378 }
2379
Daniel Vettere13a0582017-07-05 06:56:29 +02002380 mutex_lock(&fb_helper->dev->mode_config.mutex);
2381 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2382 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002383 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002384
Jesse Barnes11e17a02013-02-19 13:31:39 -08002385 if (!(fb_helper->funcs->initial_config &&
2386 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002387 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002388 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002389 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2390 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2391 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002392
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002393 if (!drm_target_cloned(fb_helper, modes, offsets,
2394 enabled, width, height) &&
2395 !drm_target_preferred(fb_helper, modes, offsets,
2396 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002397 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002398
2399 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2400 width, height);
2401
2402 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002403 }
Daniel Vettere13a0582017-07-05 06:56:29 +02002404 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002405
Dave Airlie8be48d92010-03-30 05:34:14 +00002406 /* need to set the modesets up here for use later */
2407 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002408 for (i = 0; i < fb_helper->crtc_count; i++)
2409 drm_fb_helper_modeset_release(fb_helper,
2410 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002411
Chris Wilson966a6a12016-11-29 12:02:15 +00002412 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002413 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002414 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002415 struct drm_fb_offset *offset = &offsets[i];
Ville Syrjäläa2889602016-10-26 17:41:18 +03002416 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00002417
Dave Airlie8be48d92010-03-30 05:34:14 +00002418 if (mode && fb_crtc) {
Ville Syrjäläa2889602016-10-26 17:41:18 +03002419 struct drm_connector *connector =
2420 fb_helper->connector_info[i]->connector;
2421
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002422 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2423 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002424
Dave Airlie8be48d92010-03-30 05:34:14 +00002425 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002426 fb_crtc->x = offset->x;
2427 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002428 modeset->mode = drm_mode_duplicate(dev,
2429 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002430 drm_connector_get(connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002431 modeset->connectors[modeset->num_connectors++] = connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002432 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002433 modeset->x = offset->x;
2434 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002435 }
Dave Airlie38651672010-03-30 05:34:13 +00002436 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002437out:
Dave Airlie38651672010-03-30 05:34:13 +00002438 kfree(crtcs);
2439 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002440 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002441 kfree(enabled);
2442}
2443
2444/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002445 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002446 * @fb_helper: fb_helper device struct
2447 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002448 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002449 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002450 * At the moment, this is a cloned configuration across all heads with
2451 * a new framebuffer object as the backing store.
2452 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002453 * Note that this also registers the fbdev and so allows userspace to call into
2454 * the driver through the fbdev interfaces.
2455 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002456 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2457 * to let the driver allocate and initialize the fbdev info structure and the
2458 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002459 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2460 * values for the fbdev info structure.
2461 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002462 * HANG DEBUGGING:
2463 *
2464 * When you have fbcon support built-in or already loaded, this function will do
2465 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2466 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2467 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2468 * to consoles, not even serial console. This means when your driver crashes,
2469 * you will see absolutely nothing else but a system stuck in this function,
2470 * with no further output. Any kind of printk() you place within your own driver
2471 * or in the drm core modeset code will also never show up.
2472 *
2473 * Standard debug practice is to run the fbcon setup without taking the
2474 * console_lock as a hack, to be able to see backtraces and crashes on the
2475 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2476 * cmdline option.
2477 *
2478 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002479 * first modeset from userspace will crash in the same way, and is even easier
2480 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002481 * kernel cmdline option.
2482 *
Dave Airlie38651672010-03-30 05:34:13 +00002483 * RETURNS:
2484 * Zero if everything went ok, nonzero otherwise.
2485 */
Thierry Reding01934c22014-12-19 11:21:32 +01002486int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002487{
Dave Airlie8be48d92010-03-30 05:34:14 +00002488 struct drm_device *dev = fb_helper->dev;
Chris Wilson966a6a12016-11-29 12:02:15 +00002489 struct fb_info *info;
Chris Wilson966a6a12016-11-29 12:02:15 +00002490 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002491
Daniel Vetterf64c5572015-08-25 15:45:13 +02002492 if (!drm_fbdev_emulation)
2493 return 0;
2494
Thierry Redinge9827d82017-07-04 17:18:23 +02002495 mutex_lock(&fb_helper->lock);
Chris Wilson64e94402016-11-29 12:02:16 +00002496 drm_setup_crtcs(fb_helper,
2497 dev->mode_config.max_width,
2498 dev->mode_config.max_height);
Chris Wilson966a6a12016-11-29 12:02:15 +00002499 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Thierry Redinge9827d82017-07-04 17:18:23 +02002500 mutex_unlock(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002501 if (ret)
2502 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002503
Chris Wilson966a6a12016-11-29 12:02:15 +00002504 info = fb_helper->fbdev;
2505 info->var.pixclock = 0;
2506 ret = register_framebuffer(info);
2507 if (ret < 0)
2508 return ret;
2509
2510 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2511 info->node, info->fix.id);
2512
Chris Wilsona53ca632016-11-29 12:02:17 +00002513 mutex_lock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002514 if (list_empty(&kernel_fb_helper_list))
2515 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2516
2517 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +00002518 mutex_unlock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002519
2520 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002521}
Dave Airlie8be48d92010-03-30 05:34:14 +00002522EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002523
Chris Wilson73943712011-04-22 11:03:57 +01002524/**
2525 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002526 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01002527 * @fb_helper: the drm_fb_helper
2528 *
Chris Wilson73943712011-04-22 11:03:57 +01002529 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002530 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002531 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002532 * Called at runtime, takes the mode config locks to be able to check/change the
2533 * modeset configuration. Must be run from process context (which usually means
2534 * either the output polling work or a work item launched from the driver's
2535 * hotplug interrupt).
2536 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002537 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002538 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002539 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2540 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002541 *
Chris Wilson73943712011-04-22 11:03:57 +01002542 * RETURNS:
2543 * 0 on success and a non-zero error code otherwise.
2544 */
2545int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002546{
Thierry Redinge9827d82017-07-04 17:18:23 +02002547 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002548
Daniel Vetterf64c5572015-08-25 15:45:13 +02002549 if (!drm_fbdev_emulation)
2550 return 0;
2551
Thierry Redinge9827d82017-07-04 17:18:23 +02002552 mutex_lock(&fb_helper->lock);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002553 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002554 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002555 mutex_unlock(&fb_helper->lock);
2556 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002557 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002558
Dave Airlie38651672010-03-30 05:34:13 +00002559 DRM_DEBUG_KMS("\n");
2560
Chris Wilson64e94402016-11-29 12:02:16 +00002561 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
Thierry Redinge9827d82017-07-04 17:18:23 +02002562 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002563
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002564 drm_fb_helper_set_par(fb_helper->fbdev);
2565
2566 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002567}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002568EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002569
David Rientjes6a108a12011-01-20 14:44:16 -08002570/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002571 * but the module doesn't depend on any fb console symbols. At least
2572 * attempt to load fbcon to avoid leaving the system without a usable console.
2573 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002574int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002575{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002576#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07002577 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06002578 struct module *fbcon;
2579
2580 mutex_lock(&module_mutex);
2581 fbcon = find_module(name);
2582 mutex_unlock(&module_mutex);
2583
2584 if (!fbcon)
2585 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002586#endif
David Fries3ce05162010-12-12 12:39:22 -06002587 return 0;
2588}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002589EXPORT_SYMBOL(drm_fb_helper_modinit);