blob: 9efb17c3cbe8ccf1b758ad7e3932335b98066bff [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__) \
109 for (({ lockdep_assert_held(&(fbh)->dev->mode_config.mutex); }), \
110 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);
123 lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
Thierry Reding50021ff2017-03-29 16:43:53 +0200124
125 count = fb_helper->connector_count + 1;
126
127 if (count > fb_helper->connector_info_alloc_count) {
128 size_t size = count * sizeof(fb_conn);
129
130 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200131 if (!temp)
132 return -ENOMEM;
133
Thierry Reding50021ff2017-03-29 16:43:53 +0200134 fb_helper->connector_info_alloc_count = count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200135 fb_helper->connector_info = temp;
136 }
137
Thierry Reding50021ff2017-03-29 16:43:53 +0200138 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
139 if (!fb_conn)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200140 return -ENOMEM;
141
142 drm_connector_get(connector);
Thierry Reding50021ff2017-03-29 16:43:53 +0200143 fb_conn->connector = connector;
144 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
Thierry Redingaf2405a2017-07-04 17:18:21 +0200145
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200146 return 0;
147}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200148
149int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
150 struct drm_connector *connector)
151{
152 int err;
153
Thierry Redinge9827d82017-07-04 17:18:23 +0200154 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200155 mutex_lock(&fb_helper->dev->mode_config.mutex);
156
157 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
158
159 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Thierry Redinge9827d82017-07-04 17:18:23 +0200160 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200161
162 return err;
163}
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200164EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
165
Daniel Vetter207fd322013-01-20 22:13:14 +0100166/**
167 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
168 * emulation helper
169 * @fb_helper: fbdev initialized with drm_fb_helper_init
170 *
171 * This functions adds all the available connectors for use with the given
172 * fb_helper. This is a separate step to allow drivers to freely assign
173 * connectors to the fbdev, e.g. if some are reserved for special purposes or
174 * not adequate to be used for the fbcon.
175 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200176 * This function is protected against concurrent connector hotadds/removals
177 * using drm_fb_helper_add_one_connector() and
178 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100179 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000180int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000181{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000182 struct drm_device *dev = fb_helper->dev;
183 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100184 struct drm_connector_list_iter conn_iter;
185 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000186
Daniel Vetterf64c5572015-08-25 15:45:13 +0200187 if (!drm_fbdev_emulation)
188 return 0;
189
Thierry Redinge9827d82017-07-04 17:18:23 +0200190 mutex_lock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200191 mutex_lock(&dev->mode_config.mutex);
Thierry Redingb982dab2017-02-28 15:46:43 +0100192 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100193 drm_for_each_connector_iter(connector, &conn_iter) {
Thierry Redingaf2405a2017-07-04 17:18:21 +0200194 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100195 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000196 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000197 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100198 goto out;
199
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000200fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000201 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300202 struct drm_fb_helper_connector *fb_helper_connector =
203 fb_helper->connector_info[i];
204
Thierry Redingad093602017-02-28 15:46:39 +0100205 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300206
207 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000208 fb_helper->connector_info[i] = NULL;
209 }
210 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100211out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100212 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter169faec2015-07-09 23:44:27 +0200213 mutex_unlock(&dev->mode_config.mutex);
Thierry Redinge9827d82017-07-04 17:18:23 +0200214 mutex_unlock(&fb_helper->lock);
Daniel Vetter169faec2015-07-09 23:44:27 +0200215
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100216 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000217}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000218EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000219
Thierry Redingaf2405a2017-07-04 17:18:21 +0200220static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
221 struct drm_connector *connector)
Dave Airlie65c2a892014-06-05 14:01:30 +1000222{
223 struct drm_fb_helper_connector *fb_helper_connector;
224 int i, j;
225
Daniel Vetterf64c5572015-08-25 15:45:13 +0200226 if (!drm_fbdev_emulation)
227 return 0;
228
Thierry Redinge9827d82017-07-04 17:18:23 +0200229 lockdep_assert_held(&fb_helper->lock);
Dave Airlie65c2a892014-06-05 14:01:30 +1000230
Thierry Redinge9827d82017-07-04 17:18:23 +0200231 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie65c2a892014-06-05 14:01:30 +1000232 if (fb_helper->connector_info[i]->connector == connector)
233 break;
234 }
235
236 if (i == fb_helper->connector_count)
237 return -EINVAL;
238 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100239 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000240
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200241 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000242 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200243
Dave Airlie65c2a892014-06-05 14:01:30 +1000244 fb_helper->connector_count--;
245 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500246
Dave Airlie65c2a892014-06-05 14:01:30 +1000247 return 0;
248}
Thierry Redingaf2405a2017-07-04 17:18:21 +0200249
250int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
251 struct drm_connector *connector)
252{
253 int err;
254
Thierry Redinge9827d82017-07-04 17:18:23 +0200255 mutex_lock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200256 mutex_lock(&fb_helper->dev->mode_config.mutex);
257
258 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
259
260 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Thierry Redinge9827d82017-07-04 17:18:23 +0200261 mutex_unlock(&fb_helper->lock);
Thierry Redingaf2405a2017-07-04 17:18:21 +0200262
263 return err;
264}
Dave Airlie65c2a892014-06-05 14:01:30 +1000265EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
266
Jason Wessel99231022010-10-13 14:09:43 -0500267static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
268{
269 uint16_t *r_base, *g_base, *b_base;
270 int i;
271
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300272 if (helper->funcs->gamma_get == NULL)
273 return;
274
Jason Wessel99231022010-10-13 14:09:43 -0500275 r_base = crtc->gamma_store;
276 g_base = r_base + crtc->gamma_size;
277 b_base = g_base + crtc->gamma_size;
278
279 for (i = 0; i < crtc->gamma_size; i++)
280 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
281}
282
283static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
284{
285 uint16_t *r_base, *g_base, *b_base;
286
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200287 if (crtc->funcs->gamma_set == NULL)
288 return;
289
Jason Wessel99231022010-10-13 14:09:43 -0500290 r_base = crtc->gamma_store;
291 g_base = r_base + crtc->gamma_size;
292 b_base = g_base + crtc->gamma_size;
293
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200294 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
295 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500296}
297
Daniel Vetter207fd322013-01-20 22:13:14 +0100298/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100299 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100300 * @info: fbdev registered by the helper
301 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500302int drm_fb_helper_debug_enter(struct fb_info *info)
303{
304 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200305 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500306 int i;
307
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500308 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
309 for (i = 0; i < helper->crtc_count; i++) {
310 struct drm_mode_set *mode_set =
311 &helper->crtc_info[i].mode_set;
312
313 if (!mode_set->crtc->enabled)
314 continue;
315
316 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100317 if (funcs->mode_set_base_atomic == NULL)
318 continue;
319
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200320 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
321 continue;
322
Jason Wessel99231022010-10-13 14:09:43 -0500323 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500324 funcs->mode_set_base_atomic(mode_set->crtc,
325 mode_set->fb,
326 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500327 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500328 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500329 }
330 }
331
332 return 0;
333}
334EXPORT_SYMBOL(drm_fb_helper_debug_enter);
335
Daniel Vetter207fd322013-01-20 22:13:14 +0100336/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100337 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100338 * @info: fbdev registered by the helper
339 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500340int drm_fb_helper_debug_leave(struct fb_info *info)
341{
342 struct drm_fb_helper *helper = info->par;
343 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200344 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500345 struct drm_framebuffer *fb;
346 int i;
347
348 for (i = 0; i < helper->crtc_count; i++) {
349 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200350
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500351 crtc = mode_set->crtc;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200352 if (drm_drv_uses_atomic_modeset(crtc->dev))
353 continue;
354
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500355 funcs = crtc->helper_private;
Maarten Lankhorst7114d2e2017-07-03 13:51:06 +0200356 fb = crtc->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500357
358 if (!crtc->enabled)
359 continue;
360
361 if (!fb) {
362 DRM_ERROR("no fb to restore??\n");
363 continue;
364 }
365
Stefan Christ1b99b722016-11-14 00:03:11 +0100366 if (funcs->mode_set_base_atomic == NULL)
367 continue;
368
Jason Wessel99231022010-10-13 14:09:43 -0500369 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500370 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500371 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500372 }
373
374 return 0;
375}
376EXPORT_SYMBOL(drm_fb_helper_debug_leave);
377
Rob Clarkbbb1e522015-08-25 15:35:58 -0400378static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
379{
380 struct drm_device *dev = fb_helper->dev;
381 struct drm_plane *plane;
382 struct drm_atomic_state *state;
383 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200384 unsigned int plane_mask;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400385
386 state = drm_atomic_state_alloc(dev);
387 if (!state)
388 return -ENOMEM;
389
390 state->acquire_ctx = dev->mode_config.acquire_ctx;
391retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100392 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400393 drm_for_each_plane(plane, dev) {
394 struct drm_plane_state *plane_state;
395
396 plane_state = drm_atomic_get_plane_state(state, plane);
397 if (IS_ERR(plane_state)) {
398 ret = PTR_ERR(plane_state);
399 goto fail;
400 }
401
Robert Fossc2c446a2017-05-19 16:50:17 -0400402 plane_state->rotation = DRM_MODE_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400403
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100404 plane->old_fb = plane->fb;
405 plane_mask |= 1 << drm_plane_index(plane);
406
Rob Clarkbbb1e522015-08-25 15:35:58 -0400407 /* disable non-primary: */
408 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
409 continue;
410
411 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
412 if (ret != 0)
413 goto fail;
414 }
415
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200416 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400417 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
418
419 ret = __drm_atomic_helper_set_config(mode_set, state);
420 if (ret != 0)
421 goto fail;
422 }
423
424 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400425
426fail:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100427 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700428
Rob Clarkbbb1e522015-08-25 15:35:58 -0400429 if (ret == -EDEADLK)
430 goto backoff;
431
Chris Wilson08536952016-10-14 13:18:18 +0100432 drm_atomic_state_put(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400433 return ret;
434
435backoff:
436 drm_atomic_state_clear(state);
437 drm_atomic_legacy_backoff(state);
438
439 goto retry;
440}
441
Daniel Vetter71286452017-04-03 10:33:04 +0200442static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100443{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300444 struct drm_device *dev = fb_helper->dev;
445 struct drm_plane *plane;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300446 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100447
Daniel Vetter6295d602015-07-09 23:44:25 +0200448 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700449 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
450 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100451
Ville Syrjälä6686df82016-10-21 22:22:45 +0300452 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300453 drm_mode_plane_set_obj_prop(plane,
454 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400455 DRM_MODE_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530456 }
457
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100458 for (i = 0; i < fb_helper->crtc_count; i++) {
459 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300460 struct drm_crtc *crtc = mode_set->crtc;
461 int ret;
462
Alex Deucher03f9abb2015-09-30 14:47:37 -0400463 if (crtc->funcs->cursor_set2) {
464 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
465 if (ret)
Dave Airlie48f87dd2015-10-16 10:10:32 +1000466 return ret;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400467 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300468 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
469 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200470 return ret;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300471 }
472
Daniel Vetter2d13b672012-12-11 13:47:23 +0100473 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100474 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200475 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100476 }
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200477
478 return 0;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100479}
Rob Clark5ea1f752014-05-30 12:29:48 -0400480
Daniel Vetter71286452017-04-03 10:33:04 +0200481static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
482{
483 struct drm_device *dev = fb_helper->dev;
484
485 drm_warn_on_modeset_not_all_locked(dev);
486
487 if (drm_drv_uses_atomic_modeset(dev))
488 return restore_fbdev_mode_atomic(fb_helper);
489 else
490 return restore_fbdev_mode_legacy(fb_helper);
491}
492
Rob Clark5ea1f752014-05-30 12:29:48 -0400493/**
494 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
495 * @fb_helper: fbcon to restore
496 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100497 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400498 * when implementing an fbcon on top of kms using this helper. This ensures that
499 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200500 *
501 * RETURNS:
502 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400503 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200504int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400505{
506 struct drm_device *dev = fb_helper->dev;
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200507 bool do_delayed;
508 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000509
Daniel Vetterf64c5572015-08-25 15:45:13 +0200510 if (!drm_fbdev_emulation)
511 return -ENODEV;
512
Thierry Redinge9827d82017-07-04 17:18:23 +0200513 mutex_lock(&fb_helper->lock);
Rob Clark5ea1f752014-05-30 12:29:48 -0400514 drm_modeset_lock_all(dev);
Thierry Redinge9827d82017-07-04 17:18:23 +0200515
Rob Clark5ea1f752014-05-30 12:29:48 -0400516 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000517
518 do_delayed = fb_helper->delayed_hotplug;
519 if (do_delayed)
520 fb_helper->delayed_hotplug = false;
Thierry Redinge9827d82017-07-04 17:18:23 +0200521
Rob Clark5ea1f752014-05-30 12:29:48 -0400522 drm_modeset_unlock_all(dev);
Thierry Redinge9827d82017-07-04 17:18:23 +0200523 mutex_unlock(&fb_helper->lock);
Dave Airliee2809c72014-11-26 13:15:24 +1000524
525 if (do_delayed)
526 drm_fb_helper_hotplug_event(fb_helper);
Thierry Redinge9827d82017-07-04 17:18:23 +0200527
Rob Clark5ea1f752014-05-30 12:29:48 -0400528 return ret;
529}
530EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100531
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200532static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
533{
534 struct drm_device *dev = fb_helper->dev;
535 struct drm_crtc *crtc;
536 int bound = 0, crtcs_bound = 0;
537
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200538 /*
539 * Sometimes user space wants everything disabled, so don't steal the
540 * display if there's a master.
541 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200542 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200543 return false;
544
545 drm_for_each_crtc(crtc, dev) {
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200546 drm_modeset_lock(&crtc->mutex, NULL);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200547 if (crtc->primary->fb)
548 crtcs_bound++;
549 if (crtc->primary->fb == fb_helper->fb)
550 bound++;
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200551 drm_modeset_unlock(&crtc->mutex);
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200552 }
553
554 if (bound < crtcs_bound)
555 return false;
556
557 return true;
558}
559
560#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100561/*
562 * restore fbcon display for all kms driver's using this helper, used for sysrq
563 * and panic handling.
564 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530565static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000566{
Dave Airlie785b93e2009-08-28 15:46:53 +1000567 bool ret, error = false;
568 struct drm_fb_helper *helper;
569
570 if (list_empty(&kernel_fb_helper_list))
571 return false;
572
573 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200574 struct drm_device *dev = helper->dev;
575
576 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100577 continue;
578
Thierry Redinge9827d82017-07-04 17:18:23 +0200579 mutex_lock(&helper->lock);
Daniel Vetterdd908c82015-07-28 13:18:41 +0200580 drm_modeset_lock_all(dev);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200581 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100582 if (ret)
583 error = true;
Daniel Vettercb597bb2014-07-27 19:09:33 +0200584 drm_modeset_unlock_all(dev);
Thierry Redinge9827d82017-07-04 17:18:23 +0200585 mutex_unlock(&helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000586 }
587 return error;
588}
589
Dave Airlie785b93e2009-08-28 15:46:53 +1000590static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
591{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100592 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200593
Daniel Vetterd21bf462013-01-20 18:09:52 +0100594 ret = drm_fb_helper_force_kernel_mode();
595 if (ret == true)
596 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000597}
598static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
599
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700600static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000601{
602 schedule_work(&drm_fb_helper_restore_work);
603}
604
605static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
606 .handler = drm_fb_helper_sysrq,
607 .help_msg = "force-fb(V)",
608 .action_msg = "Restore framebuffer console",
609};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000610#else
611static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200612#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000613
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100614static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000615{
616 struct drm_fb_helper *fb_helper = info->par;
617 struct drm_device *dev = fb_helper->dev;
618 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700619 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700620 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000621
622 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100623 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000624 */
Thierry Redinge9827d82017-07-04 17:18:23 +0200625 mutex_lock(&fb_helper->lock);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100626 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +0200627 mutex_unlock(&fb_helper->lock);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100628 return;
629 }
630
Daniel Vetterbdac4a02017-07-04 17:18:24 +0200631 drm_modeset_lock_all(dev);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700632 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000633 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000634
Dave Airlie8be48d92010-03-30 05:34:14 +0000635 if (!crtc->enabled)
636 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000637
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100638 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000639 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700640 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200641 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500642 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100643 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700644 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000645 }
Daniel Vetter84849902012-12-02 00:28:11 +0100646 drm_modeset_unlock_all(dev);
Thierry Redinge9827d82017-07-04 17:18:23 +0200647 mutex_unlock(&fb_helper->lock);
Dave Airlie785b93e2009-08-28 15:46:53 +1000648}
649
Daniel Vetter207fd322013-01-20 22:13:14 +0100650/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100651 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100652 * @blank: desired blanking state
653 * @info: fbdev registered by the helper
654 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000655int drm_fb_helper_blank(int blank, struct fb_info *info)
656{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200657 if (oops_in_progress)
658 return -EBUSY;
659
Dave Airlie785b93e2009-08-28 15:46:53 +1000660 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000661 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000662 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100663 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000664 break;
James Simmons731b5a12009-10-29 20:39:07 +0000665 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000666 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100667 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000668 break;
James Simmons731b5a12009-10-29 20:39:07 +0000669 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000670 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100671 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000672 break;
James Simmons731b5a12009-10-29 20:39:07 +0000673 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000674 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100675 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000676 break;
James Simmons731b5a12009-10-29 20:39:07 +0000677 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000678 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100679 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000680 break;
681 }
682 return 0;
683}
684EXPORT_SYMBOL(drm_fb_helper_blank);
685
Ville Syrjäläa2889602016-10-26 17:41:18 +0300686static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
687 struct drm_mode_set *modeset)
688{
689 int i;
690
691 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100692 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300693 modeset->connectors[i] = NULL;
694 }
695 modeset->num_connectors = 0;
696
697 drm_mode_destroy(helper->dev, modeset->mode);
698 modeset->mode = NULL;
699
700 /* FIXME should hold a ref? */
701 modeset->fb = NULL;
702}
703
Dave Airlie785b93e2009-08-28 15:46:53 +1000704static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
705{
706 int i;
707
Dave Airlie6e86d582016-04-27 11:24:51 +1000708 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100709 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000710 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000711 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000712 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300713
Sascha Hauera1b77362012-02-01 11:38:22 +0100714 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300715 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
716
717 drm_fb_helper_modeset_release(helper, modeset);
718 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100719 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000720 kfree(helper->crtc_info);
721}
722
Noralf Trønnescfe63422016-08-23 13:54:06 +0200723static void drm_fb_helper_resume_worker(struct work_struct *work)
724{
725 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
726 resume_work);
727
728 console_lock();
729 fb_set_suspend(helper->fbdev, 0);
730 console_unlock();
731}
732
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200733static void drm_fb_helper_dirty_work(struct work_struct *work)
734{
735 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
736 dirty_work);
737 struct drm_clip_rect *clip = &helper->dirty_clip;
738 struct drm_clip_rect clip_copy;
739 unsigned long flags;
740
741 spin_lock_irqsave(&helper->dirty_lock, flags);
742 clip_copy = *clip;
743 clip->x1 = clip->y1 = ~0;
744 clip->x2 = clip->y2 = 0;
745 spin_unlock_irqrestore(&helper->dirty_lock, flags);
746
Takashi Iwai87d3b652016-10-20 17:05:30 +0200747 /* call dirty callback only when it has been really touched */
748 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
749 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200750}
751
Daniel Vetter207fd322013-01-20 22:13:14 +0100752/**
Thierry Reding10a23102014-06-27 17:19:24 +0200753 * drm_fb_helper_prepare - setup a drm_fb_helper structure
754 * @dev: DRM device
755 * @helper: driver-allocated fbdev helper structure to set up
756 * @funcs: pointer to structure of functions associate with this helper
757 *
758 * Sets up the bare minimum to make the framebuffer helper usable. This is
759 * useful to implement race-free initialization of the polling helpers.
760 */
761void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
762 const struct drm_fb_helper_funcs *funcs)
763{
764 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200765 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200766 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200767 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
768 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Redinge9827d82017-07-04 17:18:23 +0200769 mutex_init(&helper->lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200770 helper->funcs = funcs;
771 helper->dev = dev;
772}
773EXPORT_SYMBOL(drm_fb_helper_prepare);
774
775/**
Daniel Vettered84e252017-02-07 15:10:49 +0100776 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100777 * @dev: drm device
778 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100779 * @max_conn_count: max connector count
780 *
781 * This allocates the structures for the fbdev helper with the given limits.
782 * Note that this won't yet touch the hardware (through the driver interfaces)
783 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
784 * to allow driver writes more control over the exact init sequence.
785 *
Thierry Reding10a23102014-06-27 17:19:24 +0200786 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100787 *
788 * RETURNS:
789 * Zero if everything went ok, nonzero otherwise.
790 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000791int drm_fb_helper_init(struct drm_device *dev,
792 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200793 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000794{
Dave Airlie785b93e2009-08-28 15:46:53 +1000795 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200796 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000797 int i;
798
Daniel Vetterf64c5572015-08-25 15:45:13 +0200799 if (!drm_fbdev_emulation)
800 return 0;
801
Xiubo Li04cfe972014-03-10 09:33:58 +0800802 if (!max_conn_count)
803 return -EINVAL;
804
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200805 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000806 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000807 return -ENOMEM;
808
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200809 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000810 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
811 if (!fb_helper->connector_info) {
812 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000813 return -ENOMEM;
814 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000815 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000816 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000817
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200818 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000819 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000820 kcalloc(max_conn_count,
821 sizeof(struct drm_connector *),
822 GFP_KERNEL);
823
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200824 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000825 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000826 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000827 }
828
829 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200830 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000831 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000832 i++;
833 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100834
Dave Airlie785b93e2009-08-28 15:46:53 +1000835 return 0;
836out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000837 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000838 return -ENOMEM;
839}
Dave Airlie4abe3522010-03-30 05:34:18 +0000840EXPORT_SYMBOL(drm_fb_helper_init);
841
Archit Tanejab8017d62015-07-22 14:57:56 +0530842/**
843 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
844 * @fb_helper: driver-allocated fbdev helper
845 *
846 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100847 * by the driver within the fb_probe fb_helper callback function. Drivers do not
848 * need to release the allocated fb_info structure themselves, this is
849 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530850 *
851 * RETURNS:
852 * fb_info pointer if things went okay, pointer containing error code
853 * otherwise
854 */
855struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
856{
857 struct device *dev = fb_helper->dev->dev;
858 struct fb_info *info;
859 int ret;
860
861 info = framebuffer_alloc(0, dev);
862 if (!info)
863 return ERR_PTR(-ENOMEM);
864
865 ret = fb_alloc_cmap(&info->cmap, 256, 0);
866 if (ret)
867 goto err_release;
868
869 info->apertures = alloc_apertures(1);
870 if (!info->apertures) {
871 ret = -ENOMEM;
872 goto err_free_cmap;
873 }
874
875 fb_helper->fbdev = info;
876
877 return info;
878
879err_free_cmap:
880 fb_dealloc_cmap(&info->cmap);
881err_release:
882 framebuffer_release(info);
883 return ERR_PTR(ret);
884}
885EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
886
887/**
888 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
889 * @fb_helper: driver-allocated fbdev helper
890 *
891 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100892 * framebuffer device. This must be called before releasing all resources for
893 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530894 */
895void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
896{
897 if (fb_helper && fb_helper->fbdev)
898 unregister_framebuffer(fb_helper->fbdev);
899}
900EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
901
902/**
Daniel Vettered84e252017-02-07 15:10:49 +0100903 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
904 * @fb_helper: driver-allocated fbdev helper
905 *
906 * This cleans up all remaining resources associated with @fb_helper. Must be
907 * called after drm_fb_helper_unlink_fbi() was called.
908 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000909void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
910{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100911 struct fb_info *info;
912
913 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200914 return;
915
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100916 info = fb_helper->fbdev;
917 if (info) {
918 if (info->cmap.len)
919 fb_dealloc_cmap(&info->cmap);
920 framebuffer_release(info);
921 }
922 fb_helper->fbdev = NULL;
923
Chris Wilson24f76b22017-02-07 12:49:56 +0000924 cancel_work_sync(&fb_helper->resume_work);
Chris Wilsonf21b9a92017-02-07 12:49:55 +0000925 cancel_work_sync(&fb_helper->dirty_work);
926
Chris Wilsona53ca632016-11-29 12:02:17 +0000927 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000928 if (!list_empty(&fb_helper->kernel_fb_list)) {
929 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200930 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000931 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000932 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000933 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000934
Thierry Redinge9827d82017-07-04 17:18:23 +0200935 mutex_destroy(&fb_helper->lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000936 drm_fb_helper_crtc_free(fb_helper);
937
Dave Airlie4abe3522010-03-30 05:34:18 +0000938}
939EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000940
Archit Taneja47074ab2015-07-22 14:57:57 +0530941/**
942 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
943 * @fb_helper: driver-allocated fbdev helper
944 *
945 * A wrapper around unlink_framebuffer implemented by fbdev core
946 */
947void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
948{
949 if (fb_helper && fb_helper->fbdev)
950 unlink_framebuffer(fb_helper->fbdev);
951}
952EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
953
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200954static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
955 u32 width, u32 height)
956{
957 struct drm_fb_helper *helper = info->par;
958 struct drm_clip_rect *clip = &helper->dirty_clip;
959 unsigned long flags;
960
961 if (!helper->fb->funcs->dirty)
962 return;
963
964 spin_lock_irqsave(&helper->dirty_lock, flags);
965 clip->x1 = min_t(u32, clip->x1, x);
966 clip->y1 = min_t(u32, clip->y1, y);
967 clip->x2 = max_t(u32, clip->x2, x + width);
968 clip->y2 = max_t(u32, clip->y2, y + height);
969 spin_unlock_irqrestore(&helper->dirty_lock, flags);
970
971 schedule_work(&helper->dirty_work);
972}
973
974/**
975 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
976 * @info: fb_info struct pointer
977 * @pagelist: list of dirty mmap framebuffer pages
978 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100979 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200980 * callback function for flushing the fbdev mmap writes.
981 */
982void drm_fb_helper_deferred_io(struct fb_info *info,
983 struct list_head *pagelist)
984{
985 unsigned long start, end, min, max;
986 struct page *page;
987 u32 y1, y2;
988
989 min = ULONG_MAX;
990 max = 0;
991 list_for_each_entry(page, pagelist, lru) {
992 start = page->index << PAGE_SHIFT;
993 end = start + PAGE_SIZE - 1;
994 min = min(min, start);
995 max = max(max, end);
996 }
997
998 if (min < max) {
999 y1 = min / info->fix.line_length;
1000 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1001 info->var.yres);
1002 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1003 }
1004}
1005EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1006
Archit Tanejacbb1a822015-07-31 16:21:41 +05301007/**
1008 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1009 * @info: fb_info struct pointer
1010 * @buf: userspace buffer to read from framebuffer memory
1011 * @count: number of bytes to read from framebuffer memory
1012 * @ppos: read offset within framebuffer memory
1013 *
1014 * A wrapper around fb_sys_read implemented by fbdev core
1015 */
1016ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1017 size_t count, loff_t *ppos)
1018{
1019 return fb_sys_read(info, buf, count, ppos);
1020}
1021EXPORT_SYMBOL(drm_fb_helper_sys_read);
1022
1023/**
1024 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1025 * @info: fb_info struct pointer
1026 * @buf: userspace buffer to write to framebuffer memory
1027 * @count: number of bytes to write to framebuffer memory
1028 * @ppos: write offset within framebuffer memory
1029 *
1030 * A wrapper around fb_sys_write implemented by fbdev core
1031 */
1032ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1033 size_t count, loff_t *ppos)
1034{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001035 ssize_t ret;
1036
1037 ret = fb_sys_write(info, buf, count, ppos);
1038 if (ret > 0)
1039 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1040 info->var.yres);
1041
1042 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301043}
1044EXPORT_SYMBOL(drm_fb_helper_sys_write);
1045
Archit Taneja742547b2015-07-31 16:21:42 +05301046/**
1047 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1048 * @info: fbdev registered by the helper
1049 * @rect: info about rectangle to fill
1050 *
1051 * A wrapper around sys_fillrect implemented by fbdev core
1052 */
1053void drm_fb_helper_sys_fillrect(struct fb_info *info,
1054 const struct fb_fillrect *rect)
1055{
1056 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001057 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1058 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301059}
1060EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1061
1062/**
1063 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1064 * @info: fbdev registered by the helper
1065 * @area: info about area to copy
1066 *
1067 * A wrapper around sys_copyarea implemented by fbdev core
1068 */
1069void drm_fb_helper_sys_copyarea(struct fb_info *info,
1070 const struct fb_copyarea *area)
1071{
1072 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001073 drm_fb_helper_dirty(info, area->dx, area->dy,
1074 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301075}
1076EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1077
1078/**
1079 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1080 * @info: fbdev registered by the helper
1081 * @image: info about image to blit
1082 *
1083 * A wrapper around sys_imageblit implemented by fbdev core
1084 */
1085void drm_fb_helper_sys_imageblit(struct fb_info *info,
1086 const struct fb_image *image)
1087{
1088 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001089 drm_fb_helper_dirty(info, image->dx, image->dy,
1090 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301091}
1092EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1093
1094/**
1095 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1096 * @info: fbdev registered by the helper
1097 * @rect: info about rectangle to fill
1098 *
1099 * A wrapper around cfb_imageblit implemented by fbdev core
1100 */
1101void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1102 const struct fb_fillrect *rect)
1103{
1104 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001105 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1106 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301107}
1108EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1109
1110/**
1111 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1112 * @info: fbdev registered by the helper
1113 * @area: info about area to copy
1114 *
1115 * A wrapper around cfb_copyarea implemented by fbdev core
1116 */
1117void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1118 const struct fb_copyarea *area)
1119{
1120 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001121 drm_fb_helper_dirty(info, area->dx, area->dy,
1122 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301123}
1124EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1125
1126/**
1127 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1128 * @info: fbdev registered by the helper
1129 * @image: info about image to blit
1130 *
1131 * A wrapper around cfb_imageblit implemented by fbdev core
1132 */
1133void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1134 const struct fb_image *image)
1135{
1136 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001137 drm_fb_helper_dirty(info, image->dx, image->dy,
1138 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301139}
1140EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1141
Archit Tanejafdefa582015-07-31 16:21:43 +05301142/**
1143 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1144 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001145 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301146 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001147 * A wrapper around fb_set_suspend implemented by fbdev core.
1148 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1149 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301150 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001151void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301152{
1153 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001154 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301155}
1156EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1157
Noralf Trønnescfe63422016-08-23 13:54:06 +02001158/**
1159 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1160 * takes the console lock
1161 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001162 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001163 *
1164 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1165 * isn't available on resume, a worker is tasked with waiting for the lock
1166 * to become available. The console lock can be pretty contented on resume
1167 * due to all the printk activity.
1168 *
1169 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001170 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001171 *
1172 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1173 */
1174void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001175 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001176{
1177 if (!fb_helper || !fb_helper->fbdev)
1178 return;
1179
1180 /* make sure there's no pending/ongoing resume */
1181 flush_work(&fb_helper->resume_work);
1182
1183 if (suspend) {
1184 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1185 return;
1186
1187 console_lock();
1188
1189 } else {
1190 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1191 return;
1192
1193 if (!console_trylock()) {
1194 schedule_work(&fb_helper->resume_work);
1195 return;
1196 }
1197 }
1198
1199 fb_set_suspend(fb_helper->fbdev, suspend);
1200 console_unlock();
1201}
1202EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1203
Dave Airliec850cb72009-10-23 18:49:03 +10001204static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001205 u16 blue, u16 regno, struct fb_info *info)
1206{
1207 struct drm_fb_helper *fb_helper = info->par;
1208 struct drm_framebuffer *fb = fb_helper->fb;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001209
Dave Airliec850cb72009-10-23 18:49:03 +10001210 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1211 u32 *palette;
1212 u32 value;
1213 /* place color in psuedopalette */
1214 if (regno > 16)
1215 return -EINVAL;
1216 palette = (u32 *)info->pseudo_palette;
1217 red >>= (16 - info->var.red.length);
1218 green >>= (16 - info->var.green.length);
1219 blue >>= (16 - info->var.blue.length);
1220 value = (red << info->var.red.offset) |
1221 (green << info->var.green.offset) |
1222 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +00001223 if (info->var.transp.length > 0) {
1224 u32 mask = (1 << info->var.transp.length) - 1;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001225
Rob Clark9da12b6a2011-02-16 02:45:51 +00001226 mask <<= info->var.transp.offset;
1227 value |= mask;
1228 }
Dave Airliec850cb72009-10-23 18:49:03 +10001229 palette[regno] = value;
1230 return 0;
1231 }
1232
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001233 /*
1234 * The driver really shouldn't advertise pseudo/directcolor
1235 * visuals if it can't deal with the palette.
1236 */
1237 if (WARN_ON(!fb_helper->funcs->gamma_set ||
1238 !fb_helper->funcs->gamma_get))
1239 return -EINVAL;
1240
Ville Syrjälä272725c2016-12-14 23:32:20 +02001241 WARN_ON(fb->format->cpp[0] != 1);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001242
Daniel Vetterfef14802016-03-30 11:51:17 +02001243 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001244
Dave Airliec850cb72009-10-23 18:49:03 +10001245 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001246}
1247
Daniel Vetter207fd322013-01-20 22:13:14 +01001248/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001249 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
Daniel Vetter207fd322013-01-20 22:13:14 +01001250 * @cmap: cmap to set
1251 * @info: fbdev registered by the helper
1252 */
Dave Airlie068143d2009-10-05 09:58:02 +10001253int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1254{
1255 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001256 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +02001257 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +10001258 u16 *red, *green, *blue, *transp;
1259 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +01001260 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001261 int start;
1262
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001263 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001264 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001265
Thierry Redinge9827d82017-07-04 17:18:23 +02001266 mutex_lock(&fb_helper->lock);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001267 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001268 mutex_unlock(&fb_helper->lock);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001269 return -EBUSY;
1270 }
1271
Daniel Vetterbdac4a02017-07-04 17:18:24 +02001272 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001273 for (i = 0; i < fb_helper->crtc_count; i++) {
1274 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1275 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +10001276
1277 red = cmap->red;
1278 green = cmap->green;
1279 blue = cmap->blue;
1280 transp = cmap->transp;
1281 start = cmap->start;
1282
roel062ac622011-03-07 18:00:34 +01001283 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +10001284 u16 hred, hgreen, hblue, htransp = 0xffff;
1285
1286 hred = *red++;
1287 hgreen = *green++;
1288 hblue = *blue++;
1289
1290 if (transp)
1291 htransp = *transp++;
1292
Dave Airliec850cb72009-10-23 18:49:03 +10001293 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1294 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001295 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +10001296 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001297 if (crtc_funcs->load_lut)
1298 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +10001299 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001300 out:
1301 drm_modeset_unlock_all(dev);
Thierry Redinge9827d82017-07-04 17:18:23 +02001302 mutex_unlock(&fb_helper->lock);
Dave Airlie068143d2009-10-05 09:58:02 +10001303 return rc;
1304}
1305EXPORT_SYMBOL(drm_fb_helper_setcmap);
1306
Daniel Vetter207fd322013-01-20 22:13:14 +01001307/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001308 * drm_fb_helper_ioctl - legacy ioctl implementation
1309 * @info: fbdev registered by the helper
1310 * @cmd: ioctl command
1311 * @arg: ioctl argument
1312 *
1313 * A helper to implement the standard fbdev ioctl. Only
1314 * FBIO_WAITFORVSYNC is implemented for now.
1315 */
1316int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1317 unsigned long arg)
1318{
1319 struct drm_fb_helper *fb_helper = info->par;
1320 struct drm_device *dev = fb_helper->dev;
1321 struct drm_mode_set *mode_set;
1322 struct drm_crtc *crtc;
1323 int ret = 0;
1324
Thierry Redinge9827d82017-07-04 17:18:23 +02001325 mutex_lock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001326 if (!drm_fb_helper_is_bound(fb_helper)) {
1327 ret = -EBUSY;
1328 goto unlock;
1329 }
1330
1331 switch (cmd) {
1332 case FBIO_WAITFORVSYNC:
1333 /*
1334 * Only consider the first CRTC.
1335 *
1336 * This ioctl is supposed to take the CRTC number as
1337 * an argument, but in fbdev times, what that number
1338 * was supposed to be was quite unclear, different
1339 * drivers were passing that argument differently
1340 * (some by reference, some by value), and most of the
1341 * userspace applications were just hardcoding 0 as an
1342 * argument.
1343 *
1344 * The first CRTC should be the integrated panel on
1345 * most drivers, so this is the best choice we can
1346 * make. If we're not smart enough here, one should
1347 * just consider switch the userspace to KMS.
1348 */
1349 mode_set = &fb_helper->crtc_info[0].mode_set;
1350 crtc = mode_set->crtc;
1351
1352 /*
1353 * Only wait for a vblank event if the CRTC is
1354 * enabled, otherwise just don't do anythintg,
1355 * not even report an error.
1356 */
1357 ret = drm_crtc_vblank_get(crtc);
1358 if (!ret) {
1359 drm_crtc_wait_one_vblank(crtc);
1360 drm_crtc_vblank_put(crtc);
1361 }
1362
1363 ret = 0;
1364 goto unlock;
1365 default:
1366 ret = -ENOTTY;
1367 }
1368
1369unlock:
Thierry Redinge9827d82017-07-04 17:18:23 +02001370 mutex_unlock(&fb_helper->lock);
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001371 return ret;
1372}
1373EXPORT_SYMBOL(drm_fb_helper_ioctl);
1374
1375/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001376 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001377 * @var: screeninfo to check
1378 * @info: fbdev registered by the helper
1379 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001380int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1381 struct fb_info *info)
1382{
1383 struct drm_fb_helper *fb_helper = info->par;
1384 struct drm_framebuffer *fb = fb_helper->fb;
1385 int depth;
1386
Jason Wesself90ebd92010-08-05 09:22:32 -05001387 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001388 return -EINVAL;
1389
Stefan Agner865afb12016-10-11 16:15:04 -07001390 /*
1391 * Changes struct fb_var_screeninfo are currently not pushed back
1392 * to KMS, hence fail if different settings are requested.
1393 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001394 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001395 var->xres > fb->width || var->yres > fb->height ||
1396 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1397 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001398 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1399 var->xres, var->yres, var->bits_per_pixel,
1400 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001401 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001402 return -EINVAL;
1403 }
1404
1405 switch (var->bits_per_pixel) {
1406 case 16:
1407 depth = (var->green.length == 6) ? 16 : 15;
1408 break;
1409 case 32:
1410 depth = (var->transp.length > 0) ? 32 : 24;
1411 break;
1412 default:
1413 depth = var->bits_per_pixel;
1414 break;
1415 }
1416
1417 switch (depth) {
1418 case 8:
1419 var->red.offset = 0;
1420 var->green.offset = 0;
1421 var->blue.offset = 0;
1422 var->red.length = 8;
1423 var->green.length = 8;
1424 var->blue.length = 8;
1425 var->transp.length = 0;
1426 var->transp.offset = 0;
1427 break;
1428 case 15:
1429 var->red.offset = 10;
1430 var->green.offset = 5;
1431 var->blue.offset = 0;
1432 var->red.length = 5;
1433 var->green.length = 5;
1434 var->blue.length = 5;
1435 var->transp.length = 1;
1436 var->transp.offset = 15;
1437 break;
1438 case 16:
1439 var->red.offset = 11;
1440 var->green.offset = 5;
1441 var->blue.offset = 0;
1442 var->red.length = 5;
1443 var->green.length = 6;
1444 var->blue.length = 5;
1445 var->transp.length = 0;
1446 var->transp.offset = 0;
1447 break;
1448 case 24:
1449 var->red.offset = 16;
1450 var->green.offset = 8;
1451 var->blue.offset = 0;
1452 var->red.length = 8;
1453 var->green.length = 8;
1454 var->blue.length = 8;
1455 var->transp.length = 0;
1456 var->transp.offset = 0;
1457 break;
1458 case 32:
1459 var->red.offset = 16;
1460 var->green.offset = 8;
1461 var->blue.offset = 0;
1462 var->red.length = 8;
1463 var->green.length = 8;
1464 var->blue.length = 8;
1465 var->transp.length = 8;
1466 var->transp.offset = 24;
1467 break;
1468 default:
1469 return -EINVAL;
1470 }
1471 return 0;
1472}
1473EXPORT_SYMBOL(drm_fb_helper_check_var);
1474
Daniel Vetter207fd322013-01-20 22:13:14 +01001475/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001476 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001477 * @info: fbdev registered by the helper
1478 *
1479 * This will let fbcon do the mode init and is called at initialization time by
1480 * the fbdev core when registering the driver, and later on through the hotplug
1481 * callback.
1482 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001483int drm_fb_helper_set_par(struct fb_info *info)
1484{
1485 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001486 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001487
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001488 if (oops_in_progress)
1489 return -EBUSY;
1490
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001491 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001492 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001493 return -EINVAL;
1494 }
1495
Rob Clark5ea1f752014-05-30 12:29:48 -04001496 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001497
Dave Airlie785b93e2009-08-28 15:46:53 +10001498 return 0;
1499}
1500EXPORT_SYMBOL(drm_fb_helper_set_par);
1501
Rob Clark1edf0262015-08-25 15:35:59 -04001502static int pan_display_atomic(struct fb_var_screeninfo *var,
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001503 struct fb_info *info)
Rob Clark1edf0262015-08-25 15:35:59 -04001504{
1505 struct drm_fb_helper *fb_helper = info->par;
1506 struct drm_device *dev = fb_helper->dev;
1507 struct drm_atomic_state *state;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001508 struct drm_plane *plane;
Rob Clark1edf0262015-08-25 15:35:59 -04001509 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001510 unsigned int plane_mask;
Rob Clark1edf0262015-08-25 15:35:59 -04001511
1512 state = drm_atomic_state_alloc(dev);
1513 if (!state)
1514 return -ENOMEM;
1515
1516 state->acquire_ctx = dev->mode_config.acquire_ctx;
1517retry:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001518 plane_mask = 0;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001519 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001520 struct drm_mode_set *mode_set;
1521
1522 mode_set = &fb_helper->crtc_info[i].mode_set;
1523
1524 mode_set->x = var->xoffset;
1525 mode_set->y = var->yoffset;
1526
1527 ret = __drm_atomic_helper_set_config(mode_set, state);
1528 if (ret != 0)
1529 goto fail;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001530
1531 plane = mode_set->crtc->primary;
Matt Roper7118fd92015-12-18 17:27:01 -08001532 plane_mask |= (1 << drm_plane_index(plane));
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001533 plane->old_fb = plane->fb;
Rob Clark1edf0262015-08-25 15:35:59 -04001534 }
1535
1536 ret = drm_atomic_commit(state);
1537 if (ret != 0)
1538 goto fail;
1539
1540 info->var.xoffset = var->xoffset;
1541 info->var.yoffset = var->yoffset;
1542
Rob Clark1edf0262015-08-25 15:35:59 -04001543fail:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001544 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001545
Rob Clark1edf0262015-08-25 15:35:59 -04001546 if (ret == -EDEADLK)
1547 goto backoff;
1548
Chris Wilson08536952016-10-14 13:18:18 +01001549 drm_atomic_state_put(state);
Rob Clark1edf0262015-08-25 15:35:59 -04001550 return ret;
1551
1552backoff:
1553 drm_atomic_state_clear(state);
1554 drm_atomic_legacy_backoff(state);
1555
1556 goto retry;
1557}
1558
Daniel Vetter71286452017-04-03 10:33:04 +02001559static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001560 struct fb_info *info)
1561{
1562 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001563 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001564 int ret = 0;
1565 int i;
1566
Dave Airlie8be48d92010-03-30 05:34:14 +00001567 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001568 modeset = &fb_helper->crtc_info[i].mode_set;
1569
1570 modeset->x = var->xoffset;
1571 modeset->y = var->yoffset;
1572
1573 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001574 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001575 if (!ret) {
1576 info->var.xoffset = var->xoffset;
1577 info->var.yoffset = var->yoffset;
1578 }
1579 }
1580 }
Daniel Vetter71286452017-04-03 10:33:04 +02001581
1582 return ret;
1583}
1584
1585/**
1586 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1587 * @var: updated screen information
1588 * @info: fbdev registered by the helper
1589 */
1590int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1591 struct fb_info *info)
1592{
1593 struct drm_fb_helper *fb_helper = info->par;
1594 struct drm_device *dev = fb_helper->dev;
1595 int ret;
1596
1597 if (oops_in_progress)
1598 return -EBUSY;
1599
Thierry Redinge9827d82017-07-04 17:18:23 +02001600 mutex_lock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001601 if (!drm_fb_helper_is_bound(fb_helper)) {
Thierry Redinge9827d82017-07-04 17:18:23 +02001602 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001603 return -EBUSY;
1604 }
1605
Daniel Vetterbdac4a02017-07-04 17:18:24 +02001606 drm_modeset_lock_all(dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001607 if (drm_drv_uses_atomic_modeset(dev))
1608 ret = pan_display_atomic(var, info);
1609 else
1610 ret = pan_display_legacy(var, info);
Daniel Vetter84849902012-12-02 00:28:11 +01001611 drm_modeset_unlock_all(dev);
Thierry Redinge9827d82017-07-04 17:18:23 +02001612 mutex_unlock(&fb_helper->lock);
Daniel Vetter71286452017-04-03 10:33:04 +02001613
Dave Airlie785b93e2009-08-28 15:46:53 +10001614 return ret;
1615}
1616EXPORT_SYMBOL(drm_fb_helper_pan_display);
1617
Daniel Vetter8acf6582013-01-21 23:38:37 +01001618/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001619 * Allocates the backing storage and sets up the fbdev info structure through
1620 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1621 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001622 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001623static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1624 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001625{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001626 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001627 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001628 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001629 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001630 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001631
1632 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1633 sizes.surface_depth = 24;
1634 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001635 sizes.fb_width = (u32)-1;
1636 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001637
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001638 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001639 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001640 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001641
Dave Airlie785b93e2009-08-28 15:46:53 +10001642 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001643 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001644 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001645 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001646
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001647 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001648
1649 if (cmdline_mode->bpp_specified) {
1650 switch (cmdline_mode->bpp) {
1651 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001652 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001653 break;
1654 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001655 sizes.surface_depth = 15;
1656 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001657 break;
1658 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001659 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001660 break;
1661 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001662 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001663 break;
1664 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001665 sizes.surface_depth = 24;
1666 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001667 break;
1668 }
1669 break;
1670 }
1671 }
1672
Dave Airlie8be48d92010-03-30 05:34:14 +00001673 crtc_count = 0;
1674 for (i = 0; i < fb_helper->crtc_count; i++) {
1675 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001676 struct drm_mode_set *mode_set;
1677 int x, y, j;
1678 /* in case of tile group, are we the last tile vert or horiz?
1679 * If no tile group you are always the last one both vertically
1680 * and horizontally
1681 */
1682 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001683
Dave Airlie8be48d92010-03-30 05:34:14 +00001684 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001685 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001686
1687 if (!desired_mode)
1688 continue;
1689
1690 crtc_count++;
1691
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001692 x = fb_helper->crtc_info[i].x;
1693 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001694
1695 if (gamma_size == 0)
1696 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1697
1698 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1699 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001700
1701 for (j = 0; j < mode_set->num_connectors; j++) {
1702 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001703
Rob Clark0e3704c2015-03-11 10:23:14 -04001704 if (connector->has_tile) {
1705 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1706 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1707 /* cloning to multiple tiles is just crazy-talk, so: */
1708 break;
1709 }
1710 }
1711
1712 if (lasth)
1713 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1714 if (lastv)
1715 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001716 }
1717
Dave Airlie38651672010-03-30 05:34:13 +00001718 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001719 /*
1720 * hmm everyone went away - assume VGA cable just fell out
1721 * and will come back later.
1722 */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001723 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001724 sizes.fb_width = sizes.surface_width = 1024;
1725 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001726 }
1727
Xinliang Liu5f152572017-02-15 17:19:08 +01001728 /* Handle our overallocation */
1729 sizes.surface_height *= drm_fbdev_overalloc;
1730 sizes.surface_height /= 100;
1731
Dave Airlie38651672010-03-30 05:34:13 +00001732 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001733 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1734 if (ret < 0)
1735 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001736
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001737 /*
1738 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1739 * events, but at init time drm_setup_crtcs needs to be called before
1740 * the fb is allocated (since we need to figure out the desired size of
1741 * the fb before we can allocate it ...). Hence we need to fix things up
1742 * here again.
1743 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001744 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001745 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1746 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1747
Dave Airlie785b93e2009-08-28 15:46:53 +10001748 return 0;
1749}
Dave Airlie785b93e2009-08-28 15:46:53 +10001750
Daniel Vetter207fd322013-01-20 22:13:14 +01001751/**
1752 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1753 * @info: fbdev registered by the helper
1754 * @pitch: desired pitch
1755 * @depth: desired depth
1756 *
1757 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1758 * fbdev emulations. Drivers which support acceleration methods which impose
1759 * additional constraints need to set up their own limits.
1760 *
1761 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001762 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001763 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001764void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1765 uint32_t depth)
1766{
1767 info->fix.type = FB_TYPE_PACKED_PIXELS;
1768 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1769 FB_VISUAL_TRUECOLOR;
1770 info->fix.mmio_start = 0;
1771 info->fix.mmio_len = 0;
1772 info->fix.type_aux = 0;
1773 info->fix.xpanstep = 1; /* doing it in hw */
1774 info->fix.ypanstep = 1; /* doing it in hw */
1775 info->fix.ywrapstep = 0;
1776 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001777
1778 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001779}
1780EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1781
Daniel Vetter207fd322013-01-20 22:13:14 +01001782/**
1783 * drm_fb_helper_fill_var - initalizes variable fbdev information
1784 * @info: fbdev instance to set up
1785 * @fb_helper: fb helper instance to use as template
1786 * @fb_width: desired fb width
1787 * @fb_height: desired fb height
1788 *
1789 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001790 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001791 *
1792 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001793 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1794 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001795 */
Dave Airlie38651672010-03-30 05:34:13 +00001796void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001797 uint32_t fb_width, uint32_t fb_height)
1798{
Dave Airlie38651672010-03-30 05:34:13 +00001799 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001800
Dave Airlie38651672010-03-30 05:34:13 +00001801 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001802 info->var.xres_virtual = fb->width;
1803 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001804 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001805 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001806 info->var.xoffset = 0;
1807 info->var.yoffset = 0;
1808 info->var.activate = FB_ACTIVATE_NOW;
1809 info->var.height = -1;
1810 info->var.width = -1;
1811
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001812 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001813 case 8:
1814 info->var.red.offset = 0;
1815 info->var.green.offset = 0;
1816 info->var.blue.offset = 0;
1817 info->var.red.length = 8; /* 8bit DAC */
1818 info->var.green.length = 8;
1819 info->var.blue.length = 8;
1820 info->var.transp.offset = 0;
1821 info->var.transp.length = 0;
1822 break;
1823 case 15:
1824 info->var.red.offset = 10;
1825 info->var.green.offset = 5;
1826 info->var.blue.offset = 0;
1827 info->var.red.length = 5;
1828 info->var.green.length = 5;
1829 info->var.blue.length = 5;
1830 info->var.transp.offset = 15;
1831 info->var.transp.length = 1;
1832 break;
1833 case 16:
1834 info->var.red.offset = 11;
1835 info->var.green.offset = 5;
1836 info->var.blue.offset = 0;
1837 info->var.red.length = 5;
1838 info->var.green.length = 6;
1839 info->var.blue.length = 5;
1840 info->var.transp.offset = 0;
1841 break;
1842 case 24:
1843 info->var.red.offset = 16;
1844 info->var.green.offset = 8;
1845 info->var.blue.offset = 0;
1846 info->var.red.length = 8;
1847 info->var.green.length = 8;
1848 info->var.blue.length = 8;
1849 info->var.transp.offset = 0;
1850 info->var.transp.length = 0;
1851 break;
1852 case 32:
1853 info->var.red.offset = 16;
1854 info->var.green.offset = 8;
1855 info->var.blue.offset = 0;
1856 info->var.red.length = 8;
1857 info->var.green.length = 8;
1858 info->var.blue.length = 8;
1859 info->var.transp.offset = 24;
1860 info->var.transp.length = 8;
1861 break;
1862 default:
1863 break;
1864 }
1865
1866 info->var.xres = fb_width;
1867 info->var.yres = fb_height;
1868}
1869EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001870
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001871static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1872 uint32_t maxX,
1873 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001874{
1875 struct drm_connector *connector;
1876 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001877 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001878
Chris Wilson966a6a12016-11-29 12:02:15 +00001879 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001880 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001881 count += connector->funcs->fill_modes(connector, maxX, maxY);
1882 }
1883
1884 return count;
1885}
1886
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001887struct 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 +00001888{
1889 struct drm_display_mode *mode;
1890
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001891 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001892 if (mode->hdisplay > width ||
1893 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001894 continue;
1895 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1896 return mode;
1897 }
1898 return NULL;
1899}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001900EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001901
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001902static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001903{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001904 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001905}
1906
Vincent Abrioua09759e2017-01-06 17:44:43 +01001907struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00001908{
Chris Wilson1794d252011-04-17 07:43:32 +01001909 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001910 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001911 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001912
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001913 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001914 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001915 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001916
1917 /* attempt to find a matching mode in the list of modes
1918 * we have gotten so far, if not add a CVT mode that conforms
1919 */
1920 if (cmdline_mode->rb || cmdline_mode->margins)
1921 goto create_mode;
1922
Takashi Iwaic683f422014-03-19 14:53:13 +01001923 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001924again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001925 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001926 /* check width/height */
1927 if (mode->hdisplay != cmdline_mode->xres ||
1928 mode->vdisplay != cmdline_mode->yres)
1929 continue;
1930
1931 if (cmdline_mode->refresh_specified) {
1932 if (mode->vrefresh != cmdline_mode->refresh)
1933 continue;
1934 }
1935
1936 if (cmdline_mode->interlace) {
1937 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1938 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001939 } else if (prefer_non_interlace) {
1940 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1941 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001942 }
1943 return mode;
1944 }
1945
Takashi Iwaic683f422014-03-19 14:53:13 +01001946 if (prefer_non_interlace) {
1947 prefer_non_interlace = false;
1948 goto again;
1949 }
1950
Dave Airlie38651672010-03-30 05:34:13 +00001951create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001952 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1953 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001954 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001955 return mode;
1956}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001957EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001958
1959static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1960{
1961 bool enable;
1962
Sachin Kamat96081cd2012-11-15 03:43:30 +00001963 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001964 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001965 else
Dave Airlie38651672010-03-30 05:34:13 +00001966 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001967
Dave Airlie38651672010-03-30 05:34:13 +00001968 return enable;
1969}
1970
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001971static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1972 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001973{
1974 bool any_enabled = false;
1975 struct drm_connector *connector;
1976 int i = 0;
1977
Chris Wilson966a6a12016-11-29 12:02:15 +00001978 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001979 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001980 enabled[i] = drm_connector_enabled(connector, true);
1981 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1982 enabled[i] ? "yes" : "no");
1983 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001984 }
1985
1986 if (any_enabled)
1987 return;
1988
Chris Wilson966a6a12016-11-29 12:02:15 +00001989 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001990 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001991 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001992 }
1993}
1994
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001995static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1996 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001997 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001998 bool *enabled, int width, int height)
1999{
2000 int count, i, j;
2001 bool can_clone = false;
2002 struct drm_fb_helper_connector *fb_helper_conn;
2003 struct drm_display_mode *dmt_mode, *mode;
2004
2005 /* only contemplate cloning in the single crtc case */
2006 if (fb_helper->crtc_count > 1)
2007 return false;
2008
2009 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00002010 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002011 if (enabled[i])
2012 count++;
2013 }
2014
2015 /* only contemplate cloning if more than one connector is enabled */
2016 if (count <= 1)
2017 return false;
2018
2019 /* check the command line or if nothing common pick 1024x768 */
2020 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00002021 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002022 if (!enabled[i])
2023 continue;
2024 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01002025 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002026 if (!modes[i]) {
2027 can_clone = false;
2028 break;
2029 }
2030 for (j = 0; j < i; j++) {
2031 if (!enabled[j])
2032 continue;
2033 if (!drm_mode_equal(modes[j], modes[i]))
2034 can_clone = false;
2035 }
2036 }
2037
2038 if (can_clone) {
2039 DRM_DEBUG_KMS("can clone using command line\n");
2040 return true;
2041 }
2042
2043 /* try and find a 1024x768 mode on each connector */
2044 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002045 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002046
Chris Wilson966a6a12016-11-29 12:02:15 +00002047 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002048 if (!enabled[i])
2049 continue;
2050
2051 fb_helper_conn = fb_helper->connector_info[i];
2052 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2053 if (drm_mode_equal(mode, dmt_mode))
2054 modes[i] = mode;
2055 }
2056 if (!modes[i])
2057 can_clone = false;
2058 }
2059
2060 if (can_clone) {
2061 DRM_DEBUG_KMS("can clone using 1024x768\n");
2062 return true;
2063 }
2064 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2065 return false;
2066}
2067
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002068static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2069 struct drm_display_mode **modes,
2070 struct drm_fb_offset *offsets,
2071 int idx,
2072 int h_idx, int v_idx)
2073{
2074 struct drm_fb_helper_connector *fb_helper_conn;
2075 int i;
2076 int hoffset = 0, voffset = 0;
2077
Chris Wilson966a6a12016-11-29 12:02:15 +00002078 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002079 fb_helper_conn = fb_helper->connector_info[i];
2080 if (!fb_helper_conn->connector->has_tile)
2081 continue;
2082
2083 if (!modes[i] && (h_idx || v_idx)) {
2084 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2085 fb_helper_conn->connector->base.id);
2086 continue;
2087 }
2088 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2089 hoffset += modes[i]->hdisplay;
2090
2091 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2092 voffset += modes[i]->vdisplay;
2093 }
2094 offsets[idx].x = hoffset;
2095 offsets[idx].y = voffset;
2096 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2097 return 0;
2098}
2099
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002100static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002101 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002102 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002103 bool *enabled, int width, int height)
2104{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002105 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002106 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2107 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002108 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002109 int i;
2110
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002111retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002112 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002113 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002114
Chris Wilsonc96521e2016-11-27 17:09:10 +00002115 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002116 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002117
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002118 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002119 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002120 continue;
2121 }
2122
2123 /* first pass over all the untiled connectors */
2124 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2125 continue;
2126
2127 if (tile_pass == 1) {
2128 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2129 fb_helper_conn->connector->tile_v_loc != 0)
2130 continue;
2131
2132 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002133 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002134 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2135 /* if this tile_pass doesn't cover any of the tiles - keep going */
2136 continue;
2137
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002138 /*
2139 * find the tile offsets for this pass - need to find
2140 * all tiles left and above
2141 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002142 drm_get_tile_offsets(fb_helper, modes, offsets,
2143 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2144 }
Dave Airlie38651672010-03-30 05:34:13 +00002145 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002146 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002147
2148 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002149 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002150 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002151 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2152 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 +00002153 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002154 }
2155 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002156 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2157 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002158 break;
2159 }
2160 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2161 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002162 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002163 }
2164
2165 if ((conn_configured & mask) != mask) {
2166 tile_pass++;
2167 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002168 }
2169 return true;
2170}
2171
Dave Airlie8be48d92010-03-30 05:34:14 +00002172static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2173 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002174 struct drm_display_mode **modes,
2175 int n, int width, int height)
2176{
2177 int c, o;
2178 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002179 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002180 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002181 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002182 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002183 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002184
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002185 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002186 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002187
2188 fb_helper_conn = fb_helper->connector_info[n];
2189 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002190
2191 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002192 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002193 if (modes[n] == NULL)
2194 return best_score;
2195
Lyude255f0e72016-05-12 10:56:59 -04002196 crtcs = kzalloc(fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002197 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002198 if (!crtcs)
2199 return best_score;
2200
2201 my_score = 1;
2202 if (connector->status == connector_status_connected)
2203 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002204 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002205 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002206 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002207 my_score++;
2208
2209 connector_funcs = connector->helper_private;
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002210
2211 /*
2212 * If the DRM device implements atomic hooks and ->best_encoder() is
2213 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2214 * helper.
2215 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002216 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93fe2016-06-07 13:47:56 +02002217 !connector_funcs->best_encoder)
2218 encoder = drm_atomic_helper_best_encoder(connector);
2219 else
2220 encoder = connector_funcs->best_encoder(connector);
2221
Dave Airlie38651672010-03-30 05:34:13 +00002222 if (!encoder)
2223 goto out;
2224
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002225 /*
2226 * select a crtc for this connector and then attempt to configure
2227 * remaining connectors
2228 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002229 for (c = 0; c < fb_helper->crtc_count; c++) {
2230 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002231
Sachin Kamat96081cd2012-11-15 03:43:30 +00002232 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002233 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002234
2235 for (o = 0; o < n; o++)
2236 if (best_crtcs[o] == crtc)
2237 break;
2238
2239 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002240 /* ignore cloning unless only a single crtc */
2241 if (fb_helper->crtc_count > 1)
2242 continue;
2243
2244 if (!drm_mode_equal(modes[o], modes[n]))
2245 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002246 }
2247
2248 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002249 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2250 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002251 width, height);
2252 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002253 best_score = score;
2254 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002255 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002256 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002257 }
Dave Airlie38651672010-03-30 05:34:13 +00002258 }
2259out:
2260 kfree(crtcs);
2261 return best_score;
2262}
2263
Chris Wilson64e94402016-11-29 12:02:16 +00002264static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2265 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002266{
Dave Airlie8be48d92010-03-30 05:34:14 +00002267 struct drm_device *dev = fb_helper->dev;
2268 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002269 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002270 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002271 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002272 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002273
2274 DRM_DEBUG_KMS("\n");
Chris Wilson64e94402016-11-29 12:02:16 +00002275 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2276 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie38651672010-03-30 05:34:13 +00002277
Chris Wilson966a6a12016-11-29 12:02:15 +00002278 /* prevent concurrent modification of connector_count by hotplug */
Thierry Redinge9827d82017-07-04 17:18:23 +02002279 lockdep_assert_held(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002280 lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
2281
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002282 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002283 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002284 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002285 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002286 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002287 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002288 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002289 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002290 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002291 DRM_ERROR("Memory allocation failed\n");
2292 goto out;
2293 }
2294
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002295 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002296
Jesse Barnes11e17a02013-02-19 13:31:39 -08002297 if (!(fb_helper->funcs->initial_config &&
2298 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002299 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002300 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002301 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2302 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2303 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002304
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002305 if (!drm_target_cloned(fb_helper, modes, offsets,
2306 enabled, width, height) &&
2307 !drm_target_preferred(fb_helper, modes, offsets,
2308 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002309 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002310
2311 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2312 width, height);
2313
2314 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002315 }
Dave Airlie38651672010-03-30 05:34:13 +00002316
Dave Airlie8be48d92010-03-30 05:34:14 +00002317 /* need to set the modesets up here for use later */
2318 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002319 for (i = 0; i < fb_helper->crtc_count; i++)
2320 drm_fb_helper_modeset_release(fb_helper,
2321 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002322
Chris Wilson966a6a12016-11-29 12:02:15 +00002323 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002324 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002325 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002326 struct drm_fb_offset *offset = &offsets[i];
Ville Syrjäläa2889602016-10-26 17:41:18 +03002327 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00002328
Dave Airlie8be48d92010-03-30 05:34:14 +00002329 if (mode && fb_crtc) {
Ville Syrjäläa2889602016-10-26 17:41:18 +03002330 struct drm_connector *connector =
2331 fb_helper->connector_info[i]->connector;
2332
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002333 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2334 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002335
Dave Airlie8be48d92010-03-30 05:34:14 +00002336 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002337 fb_crtc->x = offset->x;
2338 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002339 modeset->mode = drm_mode_duplicate(dev,
2340 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002341 drm_connector_get(connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002342 modeset->connectors[modeset->num_connectors++] = connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002343 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002344 modeset->x = offset->x;
2345 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002346 }
Dave Airlie38651672010-03-30 05:34:13 +00002347 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002348out:
Dave Airlie38651672010-03-30 05:34:13 +00002349 kfree(crtcs);
2350 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002351 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002352 kfree(enabled);
2353}
2354
2355/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002356 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002357 * @fb_helper: fb_helper device struct
2358 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002359 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002360 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002361 * At the moment, this is a cloned configuration across all heads with
2362 * a new framebuffer object as the backing store.
2363 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002364 * Note that this also registers the fbdev and so allows userspace to call into
2365 * the driver through the fbdev interfaces.
2366 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002367 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2368 * to let the driver allocate and initialize the fbdev info structure and the
2369 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002370 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2371 * values for the fbdev info structure.
2372 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002373 * HANG DEBUGGING:
2374 *
2375 * When you have fbcon support built-in or already loaded, this function will do
2376 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2377 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2378 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2379 * to consoles, not even serial console. This means when your driver crashes,
2380 * you will see absolutely nothing else but a system stuck in this function,
2381 * with no further output. Any kind of printk() you place within your own driver
2382 * or in the drm core modeset code will also never show up.
2383 *
2384 * Standard debug practice is to run the fbcon setup without taking the
2385 * console_lock as a hack, to be able to see backtraces and crashes on the
2386 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2387 * cmdline option.
2388 *
2389 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002390 * first modeset from userspace will crash in the same way, and is even easier
2391 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002392 * kernel cmdline option.
2393 *
Dave Airlie38651672010-03-30 05:34:13 +00002394 * RETURNS:
2395 * Zero if everything went ok, nonzero otherwise.
2396 */
Thierry Reding01934c22014-12-19 11:21:32 +01002397int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002398{
Dave Airlie8be48d92010-03-30 05:34:14 +00002399 struct drm_device *dev = fb_helper->dev;
Chris Wilson966a6a12016-11-29 12:02:15 +00002400 struct fb_info *info;
Chris Wilson966a6a12016-11-29 12:02:15 +00002401 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002402
Daniel Vetterf64c5572015-08-25 15:45:13 +02002403 if (!drm_fbdev_emulation)
2404 return 0;
2405
Thierry Redinge9827d82017-07-04 17:18:23 +02002406 mutex_lock(&fb_helper->lock);
Daniel Vetter53f19042014-03-20 14:26:35 +01002407 mutex_lock(&dev->mode_config.mutex);
Chris Wilson64e94402016-11-29 12:02:16 +00002408 drm_setup_crtcs(fb_helper,
2409 dev->mode_config.max_width,
2410 dev->mode_config.max_height);
Chris Wilson966a6a12016-11-29 12:02:15 +00002411 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2412 mutex_unlock(&dev->mode_config.mutex);
Thierry Redinge9827d82017-07-04 17:18:23 +02002413 mutex_unlock(&fb_helper->lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002414 if (ret)
2415 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002416
Chris Wilson966a6a12016-11-29 12:02:15 +00002417 info = fb_helper->fbdev;
2418 info->var.pixclock = 0;
2419 ret = register_framebuffer(info);
2420 if (ret < 0)
2421 return ret;
2422
2423 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2424 info->node, info->fix.id);
2425
Chris Wilsona53ca632016-11-29 12:02:17 +00002426 mutex_lock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002427 if (list_empty(&kernel_fb_helper_list))
2428 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2429
2430 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +00002431 mutex_unlock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002432
2433 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002434}
Dave Airlie8be48d92010-03-30 05:34:14 +00002435EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002436
Chris Wilson73943712011-04-22 11:03:57 +01002437/**
2438 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002439 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01002440 * @fb_helper: the drm_fb_helper
2441 *
Chris Wilson73943712011-04-22 11:03:57 +01002442 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002443 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002444 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002445 * Called at runtime, takes the mode config locks to be able to check/change the
2446 * modeset configuration. Must be run from process context (which usually means
2447 * either the output polling work or a work item launched from the driver's
2448 * hotplug interrupt).
2449 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002450 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002451 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002452 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2453 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002454 *
Chris Wilson73943712011-04-22 11:03:57 +01002455 * RETURNS:
2456 * 0 on success and a non-zero error code otherwise.
2457 */
2458int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002459{
Chris Wilson73943712011-04-22 11:03:57 +01002460 struct drm_device *dev = fb_helper->dev;
Thierry Redinge9827d82017-07-04 17:18:23 +02002461 int err = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00002462
Daniel Vetterf64c5572015-08-25 15:45:13 +02002463 if (!drm_fbdev_emulation)
2464 return 0;
2465
Thierry Redinge9827d82017-07-04 17:18:23 +02002466 mutex_lock(&fb_helper->lock);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002467 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002468 fb_helper->delayed_hotplug = true;
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002469 mutex_unlock(&fb_helper->lock);
2470 return err;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002471 }
Thierry Redinge9827d82017-07-04 17:18:23 +02002472
Dave Airlie38651672010-03-30 05:34:13 +00002473 DRM_DEBUG_KMS("\n");
2474
Daniel Vetterbdac4a02017-07-04 17:18:24 +02002475 mutex_lock(&dev->mode_config.mutex);
Chris Wilson64e94402016-11-29 12:02:16 +00002476 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
Dave Airlie4abe3522010-03-30 05:34:18 +00002477
Chris Wilson64e94402016-11-29 12:02:16 +00002478 mutex_unlock(&dev->mode_config.mutex);
Thierry Redinge9827d82017-07-04 17:18:23 +02002479 mutex_unlock(&fb_helper->lock);
Daniel Vetter89ced122013-04-11 14:26:55 +00002480
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002481 drm_fb_helper_set_par(fb_helper->fbdev);
2482
2483 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002484}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002485EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002486
David Rientjes6a108a12011-01-20 14:44:16 -08002487/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002488 * but the module doesn't depend on any fb console symbols. At least
2489 * attempt to load fbcon to avoid leaving the system without a usable console.
2490 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002491int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002492{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002493#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
Kees Cook06324662017-05-08 15:59:05 -07002494 const char name[] = "fbcon";
David Fries3ce05162010-12-12 12:39:22 -06002495 struct module *fbcon;
2496
2497 mutex_lock(&module_mutex);
2498 fbcon = find_module(name);
2499 mutex_unlock(&module_mutex);
2500
2501 if (!fbcon)
2502 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002503#endif
David Fries3ce05162010-12-12 12:39:22 -06002504 return 0;
2505}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002506EXPORT_SYMBOL(drm_fb_helper_modinit);