blob: c0d1066ea4199656ca1b892f9d14ec465ff3d276 [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
Andy Shevchenko3b40a442010-02-02 14:40:32 -080032#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100033#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040035#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_fb_helper.h>
39#include <drm/drm_crtc_helper.h>
Rob Clarkbbb1e522015-08-25 15:35:58 -040040#include <drm/drm_atomic.h>
41#include <drm/drm_atomic_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100042
Daniel Vetterf64c5572015-08-25 15:45:13 +020043static bool drm_fbdev_emulation = true;
44module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
45MODULE_PARM_DESC(fbdev_emulation,
46 "Enable legacy fbdev emulation [default=true]");
47
Dave Airlie785b93e2009-08-28 15:46:53 +100048static LIST_HEAD(kernel_fb_helper_list);
49
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010050/**
51 * DOC: fbdev helpers
52 *
53 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020054 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010055 * helper functions used by many drivers to implement the kernel mode setting
56 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010057 *
Thierry Reding10a23102014-06-27 17:19:24 +020058 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
59 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
60 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
61 * default behaviour can override the third step with their own code.
62 * Teardown is done with drm_fb_helper_fini().
Daniel Vetter207fd322013-01-20 22:13:14 +010063 *
64 * At runtime drivers should restore the fbdev console by calling
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +020065 * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback.
66 * They should also notify the fb helper code from updates to the output
Daniel Vetter207fd322013-01-20 22:13:14 +010067 * configuration by calling drm_fb_helper_hotplug_event(). For easier
68 * integration with the output polling code in drm_crtc_helper.c the modeset
Thierry Reding83c617c2014-04-29 11:44:35 +020069 * code provides a ->output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010070 *
71 * All other functions exported by the fb helper library can be used to
72 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020073 *
74 * It is possible, though perhaps somewhat tricky, to implement race-free
75 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
76 * helper must be called first to initialize the minimum required to make
77 * hotplug detection work. Drivers also need to make sure to properly set up
78 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
79 * it is safe to enable interrupts and start processing hotplug events. At the
80 * same time, drivers should initialize all modeset objects such as CRTCs,
81 * encoders and connectors. To finish up the fbdev helper initialization, the
82 * drm_fb_helper_init() function is called. To probe for all attached displays
83 * and set up an initial configuration using the detected hardware, drivers
84 * should call drm_fb_helper_single_add_all_connectors() followed by
85 * drm_fb_helper_initial_config().
Noralf Trønneseaa434d2016-04-28 17:18:33 +020086 *
Noralf Trønnes2dad5512016-05-11 18:09:17 +020087 * If &drm_framebuffer_funcs ->dirty is set, the
88 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
89 * accumulate changes and schedule &drm_fb_helper ->dirty_work to run right
90 * away. This worker then calls the dirty() function ensuring that it will
91 * always run in process context since the fb_*() function could be running in
92 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
93 * callback it will also schedule dirty_work with the damage collected from the
94 * mmap page writes.
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010095 */
96
Daniel Vetter207fd322013-01-20 22:13:14 +010097/**
98 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
99 * emulation helper
100 * @fb_helper: fbdev initialized with drm_fb_helper_init
101 *
102 * This functions adds all the available connectors for use with the given
103 * fb_helper. This is a separate step to allow drivers to freely assign
104 * connectors to the fbdev, e.g. if some are reserved for special purposes or
105 * not adequate to be used for the fbcon.
106 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200107 * This function is protected against concurrent connector hotadds/removals
108 * using drm_fb_helper_add_one_connector() and
109 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100110 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000111int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000112{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000113 struct drm_device *dev = fb_helper->dev;
114 struct drm_connector *connector;
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100115 int i, ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000116
Daniel Vetterf64c5572015-08-25 15:45:13 +0200117 if (!drm_fbdev_emulation)
118 return 0;
119
Daniel Vetter169faec2015-07-09 23:44:27 +0200120 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter6295d602015-07-09 23:44:25 +0200121 drm_for_each_connector(connector, dev) {
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100122 ret = drm_fb_helper_add_one_connector(fb_helper, connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000123
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100124 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000125 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000126 }
Daniel Vetter169faec2015-07-09 23:44:27 +0200127 mutex_unlock(&dev->mode_config.mutex);
Dave Airlied50ba252009-09-23 14:44:08 +1000128 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000129fail:
130 for (i = 0; i < fb_helper->connector_count; i++) {
131 kfree(fb_helper->connector_info[i]);
132 fb_helper->connector_info[i] = NULL;
133 }
134 fb_helper->connector_count = 0;
Daniel Vetter169faec2015-07-09 23:44:27 +0200135 mutex_unlock(&dev->mode_config.mutex);
136
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100137 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000138}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000139EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000140
Dave Airlie65c2a892014-06-05 14:01:30 +1000141int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
142{
143 struct drm_fb_helper_connector **temp;
144 struct drm_fb_helper_connector *fb_helper_connector;
145
Daniel Vetterf64c5572015-08-25 15:45:13 +0200146 if (!drm_fbdev_emulation)
147 return 0;
148
Dave Airlie65c2a892014-06-05 14:01:30 +1000149 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
150 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
Damien Lespiau14f476f2014-08-08 19:15:20 +0100151 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL);
Dave Airlie65c2a892014-06-05 14:01:30 +1000152 if (!temp)
153 return -ENOMEM;
154
155 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
156 fb_helper->connector_info = temp;
157 }
158
159
160 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
161 if (!fb_helper_connector)
162 return -ENOMEM;
163
Dave Airlie6e86d582016-04-27 11:24:51 +1000164 drm_connector_reference(connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000165 fb_helper_connector->connector = connector;
166 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
167 return 0;
168}
169EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
170
171int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
172 struct drm_connector *connector)
173{
174 struct drm_fb_helper_connector *fb_helper_connector;
175 int i, j;
176
Daniel Vetterf64c5572015-08-25 15:45:13 +0200177 if (!drm_fbdev_emulation)
178 return 0;
179
Dave Airlie65c2a892014-06-05 14:01:30 +1000180 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
181
182 for (i = 0; i < fb_helper->connector_count; i++) {
183 if (fb_helper->connector_info[i]->connector == connector)
184 break;
185 }
186
187 if (i == fb_helper->connector_count)
188 return -EINVAL;
189 fb_helper_connector = fb_helper->connector_info[i];
Dave Airlie6e86d582016-04-27 11:24:51 +1000190 drm_connector_unreference(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000191
192 for (j = i + 1; j < fb_helper->connector_count; j++) {
193 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
194 }
195 fb_helper->connector_count--;
196 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500197
Dave Airlie65c2a892014-06-05 14:01:30 +1000198 return 0;
199}
200EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
201
Jason Wessel99231022010-10-13 14:09:43 -0500202static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
203{
204 uint16_t *r_base, *g_base, *b_base;
205 int i;
206
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300207 if (helper->funcs->gamma_get == NULL)
208 return;
209
Jason Wessel99231022010-10-13 14:09:43 -0500210 r_base = crtc->gamma_store;
211 g_base = r_base + crtc->gamma_size;
212 b_base = g_base + crtc->gamma_size;
213
214 for (i = 0; i < crtc->gamma_size; i++)
215 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
216}
217
218static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
219{
220 uint16_t *r_base, *g_base, *b_base;
221
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200222 if (crtc->funcs->gamma_set == NULL)
223 return;
224
Jason Wessel99231022010-10-13 14:09:43 -0500225 r_base = crtc->gamma_store;
226 g_base = r_base + crtc->gamma_size;
227 b_base = g_base + crtc->gamma_size;
228
Maarten Lankhorst7ea77282016-06-07 12:49:30 +0200229 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Jason Wessel99231022010-10-13 14:09:43 -0500230}
231
Daniel Vetter207fd322013-01-20 22:13:14 +0100232/**
233 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
234 * @info: fbdev registered by the helper
235 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500236int drm_fb_helper_debug_enter(struct fb_info *info)
237{
238 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200239 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500240 int i;
241
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500242 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
243 for (i = 0; i < helper->crtc_count; i++) {
244 struct drm_mode_set *mode_set =
245 &helper->crtc_info[i].mode_set;
246
247 if (!mode_set->crtc->enabled)
248 continue;
249
250 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500251 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500252 funcs->mode_set_base_atomic(mode_set->crtc,
253 mode_set->fb,
254 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500255 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500256 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500257 }
258 }
259
260 return 0;
261}
262EXPORT_SYMBOL(drm_fb_helper_debug_enter);
263
264/* Find the real fb for a given fb helper CRTC */
265static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
266{
267 struct drm_device *dev = crtc->dev;
268 struct drm_crtc *c;
269
Daniel Vetter6295d602015-07-09 23:44:25 +0200270 drm_for_each_crtc(c, dev) {
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500271 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700272 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500273 }
274
275 return NULL;
276}
277
Daniel Vetter207fd322013-01-20 22:13:14 +0100278/**
279 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
280 * @info: fbdev registered by the helper
281 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500282int drm_fb_helper_debug_leave(struct fb_info *info)
283{
284 struct drm_fb_helper *helper = info->par;
285 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200286 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500287 struct drm_framebuffer *fb;
288 int i;
289
290 for (i = 0; i < helper->crtc_count; i++) {
291 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
292 crtc = mode_set->crtc;
293 funcs = crtc->helper_private;
294 fb = drm_mode_config_fb(crtc);
295
296 if (!crtc->enabled)
297 continue;
298
299 if (!fb) {
300 DRM_ERROR("no fb to restore??\n");
301 continue;
302 }
303
Jason Wessel99231022010-10-13 14:09:43 -0500304 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500305 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500306 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500307 }
308
309 return 0;
310}
311EXPORT_SYMBOL(drm_fb_helper_debug_leave);
312
Rob Clarkbbb1e522015-08-25 15:35:58 -0400313static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
314{
315 struct drm_device *dev = fb_helper->dev;
316 struct drm_plane *plane;
317 struct drm_atomic_state *state;
318 int i, ret;
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100319 unsigned plane_mask;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400320
321 state = drm_atomic_state_alloc(dev);
322 if (!state)
323 return -ENOMEM;
324
325 state->acquire_ctx = dev->mode_config.acquire_ctx;
326retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100327 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400328 drm_for_each_plane(plane, dev) {
329 struct drm_plane_state *plane_state;
330
331 plane_state = drm_atomic_get_plane_state(state, plane);
332 if (IS_ERR(plane_state)) {
333 ret = PTR_ERR(plane_state);
334 goto fail;
335 }
336
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300337 plane_state->rotation = DRM_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400338
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100339 plane->old_fb = plane->fb;
340 plane_mask |= 1 << drm_plane_index(plane);
341
Rob Clarkbbb1e522015-08-25 15:35:58 -0400342 /* disable non-primary: */
343 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
344 continue;
345
346 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
347 if (ret != 0)
348 goto fail;
349 }
350
351 for(i = 0; i < fb_helper->crtc_count; i++) {
352 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
353
354 ret = __drm_atomic_helper_set_config(mode_set, state);
355 if (ret != 0)
356 goto fail;
357 }
358
359 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400360
361fail:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100362 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700363
Rob Clarkbbb1e522015-08-25 15:35:58 -0400364 if (ret == -EDEADLK)
365 goto backoff;
366
Matt Roper94284032015-09-21 17:21:48 -0700367 if (ret != 0)
368 drm_atomic_state_free(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400369
370 return ret;
371
372backoff:
373 drm_atomic_state_clear(state);
374 drm_atomic_legacy_backoff(state);
375
376 goto retry;
377}
378
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200379static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100380{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300381 struct drm_device *dev = fb_helper->dev;
382 struct drm_plane *plane;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300383 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100384
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300385 drm_warn_on_modeset_not_all_locked(dev);
386
Daniel Vetterd3a46182016-06-08 14:19:15 +0200387 if (dev->mode_config.funcs->atomic_commit)
Rob Clarkbbb1e522015-08-25 15:35:58 -0400388 return restore_fbdev_mode_atomic(fb_helper);
389
Daniel Vetter6295d602015-07-09 23:44:25 +0200390 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700391 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
392 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100393
Sonika Jindal9783de22014-08-05 11:26:57 +0530394 if (dev->mode_config.rotation_property) {
Thomas Wood3a5f87c2014-08-20 14:45:00 +0100395 drm_mode_plane_set_obj_prop(plane,
396 dev->mode_config.rotation_property,
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300397 DRM_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530398 }
399 }
400
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100401 for (i = 0; i < fb_helper->crtc_count; i++) {
402 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300403 struct drm_crtc *crtc = mode_set->crtc;
404 int ret;
405
Alex Deucher03f9abb2015-09-30 14:47:37 -0400406 if (crtc->funcs->cursor_set2) {
407 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
408 if (ret)
Dave Airlie48f87dd2015-10-16 10:10:32 +1000409 return ret;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400410 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300411 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
412 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200413 return ret;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300414 }
415
Daniel Vetter2d13b672012-12-11 13:47:23 +0100416 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100417 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200418 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100419 }
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200420
421 return 0;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100422}
Rob Clark5ea1f752014-05-30 12:29:48 -0400423
424/**
425 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
426 * @fb_helper: fbcon to restore
427 *
428 * This should be called from driver's drm ->lastclose callback
429 * when implementing an fbcon on top of kms using this helper. This ensures that
430 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200431 *
432 * RETURNS:
433 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400434 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200435int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400436{
437 struct drm_device *dev = fb_helper->dev;
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200438 bool do_delayed;
439 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000440
Daniel Vetterf64c5572015-08-25 15:45:13 +0200441 if (!drm_fbdev_emulation)
442 return -ENODEV;
443
Rob Clark5ea1f752014-05-30 12:29:48 -0400444 drm_modeset_lock_all(dev);
445 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000446
447 do_delayed = fb_helper->delayed_hotplug;
448 if (do_delayed)
449 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400450 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000451
452 if (do_delayed)
453 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400454 return ret;
455}
456EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100457
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200458static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
459{
460 struct drm_device *dev = fb_helper->dev;
461 struct drm_crtc *crtc;
462 int bound = 0, crtcs_bound = 0;
463
464 /* Sometimes user space wants everything disabled, so don't steal the
465 * display if there's a master. */
Chris Wilsonfa7d81b2016-06-22 08:46:12 +0100466 if (lockless_dereference(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200467 return false;
468
469 drm_for_each_crtc(crtc, dev) {
470 if (crtc->primary->fb)
471 crtcs_bound++;
472 if (crtc->primary->fb == fb_helper->fb)
473 bound++;
474 }
475
476 if (bound < crtcs_bound)
477 return false;
478
479 return true;
480}
481
482#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100483/*
484 * restore fbcon display for all kms driver's using this helper, used for sysrq
485 * and panic handling.
486 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530487static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000488{
Dave Airlie785b93e2009-08-28 15:46:53 +1000489 bool ret, error = false;
490 struct drm_fb_helper *helper;
491
492 if (list_empty(&kernel_fb_helper_list))
493 return false;
494
495 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200496 struct drm_device *dev = helper->dev;
497
498 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100499 continue;
500
Daniel Vetterdd908c82015-07-28 13:18:41 +0200501 drm_modeset_lock_all(dev);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200502 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100503 if (ret)
504 error = true;
Daniel Vettercb597bb2014-07-27 19:09:33 +0200505 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000506 }
507 return error;
508}
509
Dave Airlie785b93e2009-08-28 15:46:53 +1000510static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
511{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100512 bool ret;
513 ret = drm_fb_helper_force_kernel_mode();
514 if (ret == true)
515 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000516}
517static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
518
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700519static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000520{
521 schedule_work(&drm_fb_helper_restore_work);
522}
523
524static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
525 .handler = drm_fb_helper_sysrq,
526 .help_msg = "force-fb(V)",
527 .action_msg = "Restore framebuffer console",
528};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000529#else
530static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200531#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000532
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100533static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000534{
535 struct drm_fb_helper *fb_helper = info->par;
536 struct drm_device *dev = fb_helper->dev;
537 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700538 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700539 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000540
541 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100542 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000543 */
Daniel Vetter84849902012-12-02 00:28:11 +0100544 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100545 if (!drm_fb_helper_is_bound(fb_helper)) {
546 drm_modeset_unlock_all(dev);
547 return;
548 }
549
Jesse Barnese87b2c42009-09-17 18:14:41 -0700550 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000551 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000552
Dave Airlie8be48d92010-03-30 05:34:14 +0000553 if (!crtc->enabled)
554 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000555
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100556 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700557 for (j = 0; j < fb_helper->connector_count; j++) {
558 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200559 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500560 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100561 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700562 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000563 }
Daniel Vetter84849902012-12-02 00:28:11 +0100564 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000565}
566
Daniel Vetter207fd322013-01-20 22:13:14 +0100567/**
568 * drm_fb_helper_blank - implementation for ->fb_blank
569 * @blank: desired blanking state
570 * @info: fbdev registered by the helper
571 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000572int drm_fb_helper_blank(int blank, struct fb_info *info)
573{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200574 if (oops_in_progress)
575 return -EBUSY;
576
Dave Airlie785b93e2009-08-28 15:46:53 +1000577 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000578 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000579 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100580 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000581 break;
James Simmons731b5a12009-10-29 20:39:07 +0000582 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000583 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100584 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000585 break;
James Simmons731b5a12009-10-29 20:39:07 +0000586 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000587 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100588 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000589 break;
James Simmons731b5a12009-10-29 20:39:07 +0000590 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000591 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100592 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000593 break;
James Simmons731b5a12009-10-29 20:39:07 +0000594 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000595 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100596 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000597 break;
598 }
599 return 0;
600}
601EXPORT_SYMBOL(drm_fb_helper_blank);
602
603static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
604{
605 int i;
606
Dave Airlie6e86d582016-04-27 11:24:51 +1000607 for (i = 0; i < helper->connector_count; i++) {
608 drm_connector_unreference(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000609 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000610 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000611 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100612 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000613 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100614 if (helper->crtc_info[i].mode_set.mode)
615 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
616 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000617 kfree(helper->crtc_info);
618}
619
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200620static void drm_fb_helper_dirty_work(struct work_struct *work)
621{
622 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
623 dirty_work);
624 struct drm_clip_rect *clip = &helper->dirty_clip;
625 struct drm_clip_rect clip_copy;
626 unsigned long flags;
627
628 spin_lock_irqsave(&helper->dirty_lock, flags);
629 clip_copy = *clip;
630 clip->x1 = clip->y1 = ~0;
631 clip->x2 = clip->y2 = 0;
632 spin_unlock_irqrestore(&helper->dirty_lock, flags);
633
634 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
635}
636
Daniel Vetter207fd322013-01-20 22:13:14 +0100637/**
Thierry Reding10a23102014-06-27 17:19:24 +0200638 * drm_fb_helper_prepare - setup a drm_fb_helper structure
639 * @dev: DRM device
640 * @helper: driver-allocated fbdev helper structure to set up
641 * @funcs: pointer to structure of functions associate with this helper
642 *
643 * Sets up the bare minimum to make the framebuffer helper usable. This is
644 * useful to implement race-free initialization of the polling helpers.
645 */
646void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
647 const struct drm_fb_helper_funcs *funcs)
648{
649 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200650 spin_lock_init(&helper->dirty_lock);
651 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
652 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Reding10a23102014-06-27 17:19:24 +0200653 helper->funcs = funcs;
654 helper->dev = dev;
655}
656EXPORT_SYMBOL(drm_fb_helper_prepare);
657
658/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100659 * drm_fb_helper_init - initialize a drm_fb_helper structure
660 * @dev: drm device
661 * @fb_helper: driver-allocated fbdev helper structure to initialize
662 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
663 * @max_conn_count: max connector count
664 *
665 * This allocates the structures for the fbdev helper with the given limits.
666 * Note that this won't yet touch the hardware (through the driver interfaces)
667 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
668 * to allow driver writes more control over the exact init sequence.
669 *
Thierry Reding10a23102014-06-27 17:19:24 +0200670 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100671 *
672 * RETURNS:
673 * Zero if everything went ok, nonzero otherwise.
674 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000675int drm_fb_helper_init(struct drm_device *dev,
676 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000677 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000678{
Dave Airlie785b93e2009-08-28 15:46:53 +1000679 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000680 int i;
681
Daniel Vetterf64c5572015-08-25 15:45:13 +0200682 if (!drm_fbdev_emulation)
683 return 0;
684
Xiubo Li04cfe972014-03-10 09:33:58 +0800685 if (!max_conn_count)
686 return -EINVAL;
687
Dave Airlie4abe3522010-03-30 05:34:18 +0000688 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
689 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000690 return -ENOMEM;
691
Dave Airlie4abe3522010-03-30 05:34:18 +0000692 fb_helper->crtc_count = crtc_count;
693 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
694 if (!fb_helper->connector_info) {
695 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000696 return -ENOMEM;
697 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000698 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000699 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000700
701 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000702 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000703 kcalloc(max_conn_count,
704 sizeof(struct drm_connector *),
705 GFP_KERNEL);
706
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200707 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000708 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000709 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000710 }
711
712 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200713 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000714 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000715 i++;
716 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100717
Dave Airlie785b93e2009-08-28 15:46:53 +1000718 return 0;
719out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000720 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000721 return -ENOMEM;
722}
Dave Airlie4abe3522010-03-30 05:34:18 +0000723EXPORT_SYMBOL(drm_fb_helper_init);
724
Archit Tanejab8017d62015-07-22 14:57:56 +0530725/**
726 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
727 * @fb_helper: driver-allocated fbdev helper
728 *
729 * A helper to alloc fb_info and the members cmap and apertures. Called
730 * by the driver within the fb_probe fb_helper callback function.
731 *
732 * RETURNS:
733 * fb_info pointer if things went okay, pointer containing error code
734 * otherwise
735 */
736struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
737{
738 struct device *dev = fb_helper->dev->dev;
739 struct fb_info *info;
740 int ret;
741
742 info = framebuffer_alloc(0, dev);
743 if (!info)
744 return ERR_PTR(-ENOMEM);
745
746 ret = fb_alloc_cmap(&info->cmap, 256, 0);
747 if (ret)
748 goto err_release;
749
750 info->apertures = alloc_apertures(1);
751 if (!info->apertures) {
752 ret = -ENOMEM;
753 goto err_free_cmap;
754 }
755
756 fb_helper->fbdev = info;
757
758 return info;
759
760err_free_cmap:
761 fb_dealloc_cmap(&info->cmap);
762err_release:
763 framebuffer_release(info);
764 return ERR_PTR(ret);
765}
766EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
767
768/**
769 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
770 * @fb_helper: driver-allocated fbdev helper
771 *
772 * A wrapper around unregister_framebuffer, to release the fb_info
773 * framebuffer device
774 */
775void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
776{
777 if (fb_helper && fb_helper->fbdev)
778 unregister_framebuffer(fb_helper->fbdev);
779}
780EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
781
782/**
783 * drm_fb_helper_release_fbi - dealloc fb_info and its members
784 * @fb_helper: driver-allocated fbdev helper
785 *
786 * A helper to free memory taken by fb_info and the members cmap and
787 * apertures
788 */
789void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
790{
791 if (fb_helper) {
792 struct fb_info *info = fb_helper->fbdev;
793
794 if (info) {
795 if (info->cmap.len)
796 fb_dealloc_cmap(&info->cmap);
797 framebuffer_release(info);
798 }
799
800 fb_helper->fbdev = NULL;
801 }
802}
803EXPORT_SYMBOL(drm_fb_helper_release_fbi);
804
Dave Airlie4abe3522010-03-30 05:34:18 +0000805void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
806{
Daniel Vetterf64c5572015-08-25 15:45:13 +0200807 if (!drm_fbdev_emulation)
808 return;
809
Dave Airlie4abe3522010-03-30 05:34:18 +0000810 if (!list_empty(&fb_helper->kernel_fb_list)) {
811 list_del(&fb_helper->kernel_fb_list);
812 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000813 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
814 }
815 }
816
817 drm_fb_helper_crtc_free(fb_helper);
818
Dave Airlie4abe3522010-03-30 05:34:18 +0000819}
820EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000821
Archit Taneja47074ab2015-07-22 14:57:57 +0530822/**
823 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
824 * @fb_helper: driver-allocated fbdev helper
825 *
826 * A wrapper around unlink_framebuffer implemented by fbdev core
827 */
828void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
829{
830 if (fb_helper && fb_helper->fbdev)
831 unlink_framebuffer(fb_helper->fbdev);
832}
833EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
834
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200835static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
836 u32 width, u32 height)
837{
838 struct drm_fb_helper *helper = info->par;
839 struct drm_clip_rect *clip = &helper->dirty_clip;
840 unsigned long flags;
841
842 if (!helper->fb->funcs->dirty)
843 return;
844
845 spin_lock_irqsave(&helper->dirty_lock, flags);
846 clip->x1 = min_t(u32, clip->x1, x);
847 clip->y1 = min_t(u32, clip->y1, y);
848 clip->x2 = max_t(u32, clip->x2, x + width);
849 clip->y2 = max_t(u32, clip->y2, y + height);
850 spin_unlock_irqrestore(&helper->dirty_lock, flags);
851
852 schedule_work(&helper->dirty_work);
853}
854
855/**
856 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
857 * @info: fb_info struct pointer
858 * @pagelist: list of dirty mmap framebuffer pages
859 *
860 * This function is used as the &fb_deferred_io ->deferred_io
861 * callback function for flushing the fbdev mmap writes.
862 */
863void drm_fb_helper_deferred_io(struct fb_info *info,
864 struct list_head *pagelist)
865{
866 unsigned long start, end, min, max;
867 struct page *page;
868 u32 y1, y2;
869
870 min = ULONG_MAX;
871 max = 0;
872 list_for_each_entry(page, pagelist, lru) {
873 start = page->index << PAGE_SHIFT;
874 end = start + PAGE_SIZE - 1;
875 min = min(min, start);
876 max = max(max, end);
877 }
878
879 if (min < max) {
880 y1 = min / info->fix.line_length;
881 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
882 info->var.yres);
883 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
884 }
885}
886EXPORT_SYMBOL(drm_fb_helper_deferred_io);
887
Archit Tanejacbb1a822015-07-31 16:21:41 +0530888/**
889 * drm_fb_helper_sys_read - wrapper around fb_sys_read
890 * @info: fb_info struct pointer
891 * @buf: userspace buffer to read from framebuffer memory
892 * @count: number of bytes to read from framebuffer memory
893 * @ppos: read offset within framebuffer memory
894 *
895 * A wrapper around fb_sys_read implemented by fbdev core
896 */
897ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
898 size_t count, loff_t *ppos)
899{
900 return fb_sys_read(info, buf, count, ppos);
901}
902EXPORT_SYMBOL(drm_fb_helper_sys_read);
903
904/**
905 * drm_fb_helper_sys_write - wrapper around fb_sys_write
906 * @info: fb_info struct pointer
907 * @buf: userspace buffer to write to framebuffer memory
908 * @count: number of bytes to write to framebuffer memory
909 * @ppos: write offset within framebuffer memory
910 *
911 * A wrapper around fb_sys_write implemented by fbdev core
912 */
913ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
914 size_t count, loff_t *ppos)
915{
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200916 ssize_t ret;
917
918 ret = fb_sys_write(info, buf, count, ppos);
919 if (ret > 0)
920 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
921 info->var.yres);
922
923 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +0530924}
925EXPORT_SYMBOL(drm_fb_helper_sys_write);
926
Archit Taneja742547b2015-07-31 16:21:42 +0530927/**
928 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
929 * @info: fbdev registered by the helper
930 * @rect: info about rectangle to fill
931 *
932 * A wrapper around sys_fillrect implemented by fbdev core
933 */
934void drm_fb_helper_sys_fillrect(struct fb_info *info,
935 const struct fb_fillrect *rect)
936{
937 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200938 drm_fb_helper_dirty(info, rect->dx, rect->dy,
939 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +0530940}
941EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
942
943/**
944 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
945 * @info: fbdev registered by the helper
946 * @area: info about area to copy
947 *
948 * A wrapper around sys_copyarea implemented by fbdev core
949 */
950void drm_fb_helper_sys_copyarea(struct fb_info *info,
951 const struct fb_copyarea *area)
952{
953 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200954 drm_fb_helper_dirty(info, area->dx, area->dy,
955 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +0530956}
957EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
958
959/**
960 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
961 * @info: fbdev registered by the helper
962 * @image: info about image to blit
963 *
964 * A wrapper around sys_imageblit implemented by fbdev core
965 */
966void drm_fb_helper_sys_imageblit(struct fb_info *info,
967 const struct fb_image *image)
968{
969 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200970 drm_fb_helper_dirty(info, image->dx, image->dy,
971 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +0530972}
973EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
974
975/**
976 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
977 * @info: fbdev registered by the helper
978 * @rect: info about rectangle to fill
979 *
980 * A wrapper around cfb_imageblit implemented by fbdev core
981 */
982void drm_fb_helper_cfb_fillrect(struct fb_info *info,
983 const struct fb_fillrect *rect)
984{
985 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200986 drm_fb_helper_dirty(info, rect->dx, rect->dy,
987 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +0530988}
989EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
990
991/**
992 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
993 * @info: fbdev registered by the helper
994 * @area: info about area to copy
995 *
996 * A wrapper around cfb_copyarea implemented by fbdev core
997 */
998void drm_fb_helper_cfb_copyarea(struct fb_info *info,
999 const struct fb_copyarea *area)
1000{
1001 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001002 drm_fb_helper_dirty(info, area->dx, area->dy,
1003 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301004}
1005EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1006
1007/**
1008 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1009 * @info: fbdev registered by the helper
1010 * @image: info about image to blit
1011 *
1012 * A wrapper around cfb_imageblit implemented by fbdev core
1013 */
1014void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1015 const struct fb_image *image)
1016{
1017 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001018 drm_fb_helper_dirty(info, image->dx, image->dy,
1019 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301020}
1021EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1022
Archit Tanejafdefa582015-07-31 16:21:43 +05301023/**
1024 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1025 * @fb_helper: driver-allocated fbdev helper
1026 * @state: desired state, zero to resume, non-zero to suspend
1027 *
1028 * A wrapper around fb_set_suspend implemented by fbdev core
1029 */
1030void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
1031{
1032 if (fb_helper && fb_helper->fbdev)
1033 fb_set_suspend(fb_helper->fbdev, state);
1034}
1035EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1036
Dave Airliec850cb72009-10-23 18:49:03 +10001037static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001038 u16 blue, u16 regno, struct fb_info *info)
1039{
1040 struct drm_fb_helper *fb_helper = info->par;
1041 struct drm_framebuffer *fb = fb_helper->fb;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001042
Dave Airliec850cb72009-10-23 18:49:03 +10001043 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1044 u32 *palette;
1045 u32 value;
1046 /* place color in psuedopalette */
1047 if (regno > 16)
1048 return -EINVAL;
1049 palette = (u32 *)info->pseudo_palette;
1050 red >>= (16 - info->var.red.length);
1051 green >>= (16 - info->var.green.length);
1052 blue >>= (16 - info->var.blue.length);
1053 value = (red << info->var.red.offset) |
1054 (green << info->var.green.offset) |
1055 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +00001056 if (info->var.transp.length > 0) {
1057 u32 mask = (1 << info->var.transp.length) - 1;
1058 mask <<= info->var.transp.offset;
1059 value |= mask;
1060 }
Dave Airliec850cb72009-10-23 18:49:03 +10001061 palette[regno] = value;
1062 return 0;
1063 }
1064
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001065 /*
1066 * The driver really shouldn't advertise pseudo/directcolor
1067 * visuals if it can't deal with the palette.
1068 */
1069 if (WARN_ON(!fb_helper->funcs->gamma_set ||
1070 !fb_helper->funcs->gamma_get))
1071 return -EINVAL;
1072
Daniel Vetterfef14802016-03-30 11:51:17 +02001073 WARN_ON(fb->bits_per_pixel != 8);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001074
Daniel Vetterfef14802016-03-30 11:51:17 +02001075 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001076
Dave Airliec850cb72009-10-23 18:49:03 +10001077 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001078}
1079
Daniel Vetter207fd322013-01-20 22:13:14 +01001080/**
1081 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
1082 * @cmap: cmap to set
1083 * @info: fbdev registered by the helper
1084 */
Dave Airlie068143d2009-10-05 09:58:02 +10001085int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1086{
1087 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001088 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +02001089 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +10001090 u16 *red, *green, *blue, *transp;
1091 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +01001092 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001093 int start;
1094
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001095 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001096 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001097
1098 drm_modeset_lock_all(dev);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001099 if (!drm_fb_helper_is_bound(fb_helper)) {
1100 drm_modeset_unlock_all(dev);
1101 return -EBUSY;
1102 }
1103
Dave Airlie8be48d92010-03-30 05:34:14 +00001104 for (i = 0; i < fb_helper->crtc_count; i++) {
1105 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1106 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +10001107
1108 red = cmap->red;
1109 green = cmap->green;
1110 blue = cmap->blue;
1111 transp = cmap->transp;
1112 start = cmap->start;
1113
roel062ac622011-03-07 18:00:34 +01001114 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +10001115 u16 hred, hgreen, hblue, htransp = 0xffff;
1116
1117 hred = *red++;
1118 hgreen = *green++;
1119 hblue = *blue++;
1120
1121 if (transp)
1122 htransp = *transp++;
1123
Dave Airliec850cb72009-10-23 18:49:03 +10001124 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1125 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001126 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +10001127 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001128 if (crtc_funcs->load_lut)
1129 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +10001130 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001131 out:
1132 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +10001133 return rc;
1134}
1135EXPORT_SYMBOL(drm_fb_helper_setcmap);
1136
Daniel Vetter207fd322013-01-20 22:13:14 +01001137/**
1138 * drm_fb_helper_check_var - implementation for ->fb_check_var
1139 * @var: screeninfo to check
1140 * @info: fbdev registered by the helper
1141 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001142int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1143 struct fb_info *info)
1144{
1145 struct drm_fb_helper *fb_helper = info->par;
1146 struct drm_framebuffer *fb = fb_helper->fb;
1147 int depth;
1148
Jason Wesself90ebd92010-08-05 09:22:32 -05001149 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001150 return -EINVAL;
1151
1152 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +01001153 if (var->bits_per_pixel > fb->bits_per_pixel ||
1154 var->xres > fb->width || var->yres > fb->height ||
1155 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +10001156 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001157 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1158 var->xres, var->yres, var->bits_per_pixel,
1159 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +10001160 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +10001161 return -EINVAL;
1162 }
1163
1164 switch (var->bits_per_pixel) {
1165 case 16:
1166 depth = (var->green.length == 6) ? 16 : 15;
1167 break;
1168 case 32:
1169 depth = (var->transp.length > 0) ? 32 : 24;
1170 break;
1171 default:
1172 depth = var->bits_per_pixel;
1173 break;
1174 }
1175
1176 switch (depth) {
1177 case 8:
1178 var->red.offset = 0;
1179 var->green.offset = 0;
1180 var->blue.offset = 0;
1181 var->red.length = 8;
1182 var->green.length = 8;
1183 var->blue.length = 8;
1184 var->transp.length = 0;
1185 var->transp.offset = 0;
1186 break;
1187 case 15:
1188 var->red.offset = 10;
1189 var->green.offset = 5;
1190 var->blue.offset = 0;
1191 var->red.length = 5;
1192 var->green.length = 5;
1193 var->blue.length = 5;
1194 var->transp.length = 1;
1195 var->transp.offset = 15;
1196 break;
1197 case 16:
1198 var->red.offset = 11;
1199 var->green.offset = 5;
1200 var->blue.offset = 0;
1201 var->red.length = 5;
1202 var->green.length = 6;
1203 var->blue.length = 5;
1204 var->transp.length = 0;
1205 var->transp.offset = 0;
1206 break;
1207 case 24:
1208 var->red.offset = 16;
1209 var->green.offset = 8;
1210 var->blue.offset = 0;
1211 var->red.length = 8;
1212 var->green.length = 8;
1213 var->blue.length = 8;
1214 var->transp.length = 0;
1215 var->transp.offset = 0;
1216 break;
1217 case 32:
1218 var->red.offset = 16;
1219 var->green.offset = 8;
1220 var->blue.offset = 0;
1221 var->red.length = 8;
1222 var->green.length = 8;
1223 var->blue.length = 8;
1224 var->transp.length = 8;
1225 var->transp.offset = 24;
1226 break;
1227 default:
1228 return -EINVAL;
1229 }
1230 return 0;
1231}
1232EXPORT_SYMBOL(drm_fb_helper_check_var);
1233
Daniel Vetter207fd322013-01-20 22:13:14 +01001234/**
1235 * drm_fb_helper_set_par - implementation for ->fb_set_par
1236 * @info: fbdev registered by the helper
1237 *
1238 * This will let fbcon do the mode init and is called at initialization time by
1239 * the fbdev core when registering the driver, and later on through the hotplug
1240 * callback.
1241 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001242int drm_fb_helper_set_par(struct fb_info *info)
1243{
1244 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001245 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001246
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001247 if (oops_in_progress)
1248 return -EBUSY;
1249
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001250 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001251 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001252 return -EINVAL;
1253 }
1254
Rob Clark5ea1f752014-05-30 12:29:48 -04001255 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001256
Dave Airlie785b93e2009-08-28 15:46:53 +10001257 return 0;
1258}
1259EXPORT_SYMBOL(drm_fb_helper_set_par);
1260
Rob Clark1edf0262015-08-25 15:35:59 -04001261static int pan_display_atomic(struct fb_var_screeninfo *var,
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001262 struct fb_info *info)
Rob Clark1edf0262015-08-25 15:35:59 -04001263{
1264 struct drm_fb_helper *fb_helper = info->par;
1265 struct drm_device *dev = fb_helper->dev;
1266 struct drm_atomic_state *state;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001267 struct drm_plane *plane;
Rob Clark1edf0262015-08-25 15:35:59 -04001268 int i, ret;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001269 unsigned plane_mask;
Rob Clark1edf0262015-08-25 15:35:59 -04001270
1271 state = drm_atomic_state_alloc(dev);
1272 if (!state)
1273 return -ENOMEM;
1274
1275 state->acquire_ctx = dev->mode_config.acquire_ctx;
1276retry:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001277 plane_mask = 0;
Rob Clark1edf0262015-08-25 15:35:59 -04001278 for(i = 0; i < fb_helper->crtc_count; i++) {
1279 struct drm_mode_set *mode_set;
1280
1281 mode_set = &fb_helper->crtc_info[i].mode_set;
1282
1283 mode_set->x = var->xoffset;
1284 mode_set->y = var->yoffset;
1285
1286 ret = __drm_atomic_helper_set_config(mode_set, state);
1287 if (ret != 0)
1288 goto fail;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001289
1290 plane = mode_set->crtc->primary;
Matt Roper7118fd92015-12-18 17:27:01 -08001291 plane_mask |= (1 << drm_plane_index(plane));
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001292 plane->old_fb = plane->fb;
Rob Clark1edf0262015-08-25 15:35:59 -04001293 }
1294
1295 ret = drm_atomic_commit(state);
1296 if (ret != 0)
1297 goto fail;
1298
1299 info->var.xoffset = var->xoffset;
1300 info->var.yoffset = var->yoffset;
1301
Rob Clark1edf0262015-08-25 15:35:59 -04001302
1303fail:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001304 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001305
Rob Clark1edf0262015-08-25 15:35:59 -04001306 if (ret == -EDEADLK)
1307 goto backoff;
1308
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001309 if (ret != 0)
1310 drm_atomic_state_free(state);
Rob Clark1edf0262015-08-25 15:35:59 -04001311
1312 return ret;
1313
1314backoff:
1315 drm_atomic_state_clear(state);
1316 drm_atomic_legacy_backoff(state);
1317
1318 goto retry;
1319}
1320
Daniel Vetter207fd322013-01-20 22:13:14 +01001321/**
1322 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1323 * @var: updated screen information
1324 * @info: fbdev registered by the helper
1325 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001326int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1327 struct fb_info *info)
1328{
1329 struct drm_fb_helper *fb_helper = info->par;
1330 struct drm_device *dev = fb_helper->dev;
1331 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001332 int ret = 0;
1333 int i;
1334
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001335 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001336 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001337
1338 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +01001339 if (!drm_fb_helper_is_bound(fb_helper)) {
1340 drm_modeset_unlock_all(dev);
1341 return -EBUSY;
1342 }
1343
Daniel Vetterd3a46182016-06-08 14:19:15 +02001344 if (dev->mode_config.funcs->atomic_commit) {
Rob Clark1edf0262015-08-25 15:35:59 -04001345 ret = pan_display_atomic(var, info);
1346 goto unlock;
1347 }
1348
Dave Airlie8be48d92010-03-30 05:34:14 +00001349 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001350 modeset = &fb_helper->crtc_info[i].mode_set;
1351
1352 modeset->x = var->xoffset;
1353 modeset->y = var->yoffset;
1354
1355 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001356 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001357 if (!ret) {
1358 info->var.xoffset = var->xoffset;
1359 info->var.yoffset = var->yoffset;
1360 }
1361 }
1362 }
Rob Clark1edf0262015-08-25 15:35:59 -04001363unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001364 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +10001365 return ret;
1366}
1367EXPORT_SYMBOL(drm_fb_helper_pan_display);
1368
Daniel Vetter8acf6582013-01-21 23:38:37 +01001369/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001370 * Allocates the backing storage and sets up the fbdev info structure through
1371 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1372 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001373 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001374static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1375 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001376{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001377 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001378 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001379 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +10001380 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +00001381 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001382 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001383
1384 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1385 sizes.surface_depth = 24;
1386 sizes.surface_bpp = 32;
1387 sizes.fb_width = (unsigned)-1;
1388 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001389
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001390 /* if driver picks 8 or 16 by default use that
1391 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001392 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001393 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001394
Dave Airlie785b93e2009-08-28 15:46:53 +10001395 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001396 for (i = 0; i < fb_helper->connector_count; i++) {
1397 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001398 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001399
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001400 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001401
1402 if (cmdline_mode->bpp_specified) {
1403 switch (cmdline_mode->bpp) {
1404 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001405 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001406 break;
1407 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001408 sizes.surface_depth = 15;
1409 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001410 break;
1411 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001412 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001413 break;
1414 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001415 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001416 break;
1417 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001418 sizes.surface_depth = 24;
1419 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001420 break;
1421 }
1422 break;
1423 }
1424 }
1425
Dave Airlie8be48d92010-03-30 05:34:14 +00001426 crtc_count = 0;
1427 for (i = 0; i < fb_helper->crtc_count; i++) {
1428 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001429 struct drm_mode_set *mode_set;
1430 int x, y, j;
1431 /* in case of tile group, are we the last tile vert or horiz?
1432 * If no tile group you are always the last one both vertically
1433 * and horizontally
1434 */
1435 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001436
Dave Airlie8be48d92010-03-30 05:34:14 +00001437 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001438 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001439
1440 if (!desired_mode)
1441 continue;
1442
1443 crtc_count++;
1444
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001445 x = fb_helper->crtc_info[i].x;
1446 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001447
1448 if (gamma_size == 0)
1449 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1450
1451 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1452 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001453
1454 for (j = 0; j < mode_set->num_connectors; j++) {
1455 struct drm_connector *connector = mode_set->connectors[j];
1456 if (connector->has_tile) {
1457 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1458 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1459 /* cloning to multiple tiles is just crazy-talk, so: */
1460 break;
1461 }
1462 }
1463
1464 if (lasth)
1465 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1466 if (lastv)
1467 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001468 }
1469
Dave Airlie38651672010-03-30 05:34:13 +00001470 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001471 /* hmm everyone went away - assume VGA cable just fell out
1472 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001473 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001474 sizes.fb_width = sizes.surface_width = 1024;
1475 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001476 }
1477
Dave Airlie38651672010-03-30 05:34:13 +00001478 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001479 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1480 if (ret < 0)
1481 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001482
Dave Airlie38651672010-03-30 05:34:13 +00001483 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001484
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001485 /*
1486 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1487 * events, but at init time drm_setup_crtcs needs to be called before
1488 * the fb is allocated (since we need to figure out the desired size of
1489 * the fb before we can allocate it ...). Hence we need to fix things up
1490 * here again.
1491 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001492 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001493 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1494 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1495
Dave Airlie785b93e2009-08-28 15:46:53 +10001496
Daniel Vetter8acf6582013-01-21 23:38:37 +01001497 info->var.pixclock = 0;
1498 if (register_framebuffer(info) < 0)
1499 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001500
Daniel Vetter8acf6582013-01-21 23:38:37 +01001501 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1502 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001503
Dave Airlie785b93e2009-08-28 15:46:53 +10001504 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001505 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1506 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001507
1508 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001509
Dave Airlie785b93e2009-08-28 15:46:53 +10001510 return 0;
1511}
Dave Airlie785b93e2009-08-28 15:46:53 +10001512
Daniel Vetter207fd322013-01-20 22:13:14 +01001513/**
1514 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1515 * @info: fbdev registered by the helper
1516 * @pitch: desired pitch
1517 * @depth: desired depth
1518 *
1519 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1520 * fbdev emulations. Drivers which support acceleration methods which impose
1521 * additional constraints need to set up their own limits.
1522 *
1523 * Drivers should call this (or their equivalent setup code) from their
1524 * ->fb_probe callback.
1525 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001526void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1527 uint32_t depth)
1528{
1529 info->fix.type = FB_TYPE_PACKED_PIXELS;
1530 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1531 FB_VISUAL_TRUECOLOR;
1532 info->fix.mmio_start = 0;
1533 info->fix.mmio_len = 0;
1534 info->fix.type_aux = 0;
1535 info->fix.xpanstep = 1; /* doing it in hw */
1536 info->fix.ypanstep = 1; /* doing it in hw */
1537 info->fix.ywrapstep = 0;
1538 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001539
1540 info->fix.line_length = pitch;
1541 return;
1542}
1543EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1544
Daniel Vetter207fd322013-01-20 22:13:14 +01001545/**
1546 * drm_fb_helper_fill_var - initalizes variable fbdev information
1547 * @info: fbdev instance to set up
1548 * @fb_helper: fb helper instance to use as template
1549 * @fb_width: desired fb width
1550 * @fb_height: desired fb height
1551 *
1552 * Sets up the variable fbdev metainformation from the given fb helper instance
1553 * and the drm framebuffer allocated in fb_helper->fb.
1554 *
1555 * Drivers should call this (or their equivalent setup code) from their
1556 * ->fb_probe callback after having allocated the fbdev backing
1557 * storage framebuffer.
1558 */
Dave Airlie38651672010-03-30 05:34:13 +00001559void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001560 uint32_t fb_width, uint32_t fb_height)
1561{
Dave Airlie38651672010-03-30 05:34:13 +00001562 struct drm_framebuffer *fb = fb_helper->fb;
1563 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001564 info->var.xres_virtual = fb->width;
1565 info->var.yres_virtual = fb->height;
1566 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001567 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001568 info->var.xoffset = 0;
1569 info->var.yoffset = 0;
1570 info->var.activate = FB_ACTIVATE_NOW;
1571 info->var.height = -1;
1572 info->var.width = -1;
1573
1574 switch (fb->depth) {
1575 case 8:
1576 info->var.red.offset = 0;
1577 info->var.green.offset = 0;
1578 info->var.blue.offset = 0;
1579 info->var.red.length = 8; /* 8bit DAC */
1580 info->var.green.length = 8;
1581 info->var.blue.length = 8;
1582 info->var.transp.offset = 0;
1583 info->var.transp.length = 0;
1584 break;
1585 case 15:
1586 info->var.red.offset = 10;
1587 info->var.green.offset = 5;
1588 info->var.blue.offset = 0;
1589 info->var.red.length = 5;
1590 info->var.green.length = 5;
1591 info->var.blue.length = 5;
1592 info->var.transp.offset = 15;
1593 info->var.transp.length = 1;
1594 break;
1595 case 16:
1596 info->var.red.offset = 11;
1597 info->var.green.offset = 5;
1598 info->var.blue.offset = 0;
1599 info->var.red.length = 5;
1600 info->var.green.length = 6;
1601 info->var.blue.length = 5;
1602 info->var.transp.offset = 0;
1603 break;
1604 case 24:
1605 info->var.red.offset = 16;
1606 info->var.green.offset = 8;
1607 info->var.blue.offset = 0;
1608 info->var.red.length = 8;
1609 info->var.green.length = 8;
1610 info->var.blue.length = 8;
1611 info->var.transp.offset = 0;
1612 info->var.transp.length = 0;
1613 break;
1614 case 32:
1615 info->var.red.offset = 16;
1616 info->var.green.offset = 8;
1617 info->var.blue.offset = 0;
1618 info->var.red.length = 8;
1619 info->var.green.length = 8;
1620 info->var.blue.length = 8;
1621 info->var.transp.offset = 24;
1622 info->var.transp.length = 8;
1623 break;
1624 default:
1625 break;
1626 }
1627
1628 info->var.xres = fb_width;
1629 info->var.yres = fb_height;
1630}
1631EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001632
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001633static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1634 uint32_t maxX,
1635 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001636{
1637 struct drm_connector *connector;
1638 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001639 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001640
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001641 for (i = 0; i < fb_helper->connector_count; i++) {
1642 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001643 count += connector->funcs->fill_modes(connector, maxX, maxY);
1644 }
1645
1646 return count;
1647}
1648
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001649struct 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 +00001650{
1651 struct drm_display_mode *mode;
1652
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001653 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001654 if (mode->hdisplay > width ||
1655 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001656 continue;
1657 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1658 return mode;
1659 }
1660 return NULL;
1661}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001662EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001663
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001664static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001665{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001666 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001667}
1668
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001669struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001670 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001671{
Chris Wilson1794d252011-04-17 07:43:32 +01001672 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001673 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001674 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001675
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001676 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001677 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001678 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001679
1680 /* attempt to find a matching mode in the list of modes
1681 * we have gotten so far, if not add a CVT mode that conforms
1682 */
1683 if (cmdline_mode->rb || cmdline_mode->margins)
1684 goto create_mode;
1685
Takashi Iwaic683f422014-03-19 14:53:13 +01001686 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001687again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001688 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001689 /* check width/height */
1690 if (mode->hdisplay != cmdline_mode->xres ||
1691 mode->vdisplay != cmdline_mode->yres)
1692 continue;
1693
1694 if (cmdline_mode->refresh_specified) {
1695 if (mode->vrefresh != cmdline_mode->refresh)
1696 continue;
1697 }
1698
1699 if (cmdline_mode->interlace) {
1700 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1701 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001702 } else if (prefer_non_interlace) {
1703 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1704 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001705 }
1706 return mode;
1707 }
1708
Takashi Iwaic683f422014-03-19 14:53:13 +01001709 if (prefer_non_interlace) {
1710 prefer_non_interlace = false;
1711 goto again;
1712 }
1713
Dave Airlie38651672010-03-30 05:34:13 +00001714create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001715 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1716 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001717 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001718 return mode;
1719}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001720EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001721
1722static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1723{
1724 bool enable;
1725
Sachin Kamat96081cd2012-11-15 03:43:30 +00001726 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001727 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001728 else
Dave Airlie38651672010-03-30 05:34:13 +00001729 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001730
Dave Airlie38651672010-03-30 05:34:13 +00001731 return enable;
1732}
1733
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001734static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1735 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001736{
1737 bool any_enabled = false;
1738 struct drm_connector *connector;
1739 int i = 0;
1740
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001741 for (i = 0; i < fb_helper->connector_count; i++) {
1742 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001743 enabled[i] = drm_connector_enabled(connector, true);
1744 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1745 enabled[i] ? "yes" : "no");
1746 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001747 }
1748
1749 if (any_enabled)
1750 return;
1751
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001752 for (i = 0; i < fb_helper->connector_count; i++) {
1753 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001754 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001755 }
1756}
1757
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001758static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1759 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001760 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001761 bool *enabled, int width, int height)
1762{
1763 int count, i, j;
1764 bool can_clone = false;
1765 struct drm_fb_helper_connector *fb_helper_conn;
1766 struct drm_display_mode *dmt_mode, *mode;
1767
1768 /* only contemplate cloning in the single crtc case */
1769 if (fb_helper->crtc_count > 1)
1770 return false;
1771
1772 count = 0;
1773 for (i = 0; i < fb_helper->connector_count; i++) {
1774 if (enabled[i])
1775 count++;
1776 }
1777
1778 /* only contemplate cloning if more than one connector is enabled */
1779 if (count <= 1)
1780 return false;
1781
1782 /* check the command line or if nothing common pick 1024x768 */
1783 can_clone = true;
1784 for (i = 0; i < fb_helper->connector_count; i++) {
1785 if (!enabled[i])
1786 continue;
1787 fb_helper_conn = fb_helper->connector_info[i];
1788 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1789 if (!modes[i]) {
1790 can_clone = false;
1791 break;
1792 }
1793 for (j = 0; j < i; j++) {
1794 if (!enabled[j])
1795 continue;
1796 if (!drm_mode_equal(modes[j], modes[i]))
1797 can_clone = false;
1798 }
1799 }
1800
1801 if (can_clone) {
1802 DRM_DEBUG_KMS("can clone using command line\n");
1803 return true;
1804 }
1805
1806 /* try and find a 1024x768 mode on each connector */
1807 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001808 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001809
1810 for (i = 0; i < fb_helper->connector_count; i++) {
1811
1812 if (!enabled[i])
1813 continue;
1814
1815 fb_helper_conn = fb_helper->connector_info[i];
1816 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1817 if (drm_mode_equal(mode, dmt_mode))
1818 modes[i] = mode;
1819 }
1820 if (!modes[i])
1821 can_clone = false;
1822 }
1823
1824 if (can_clone) {
1825 DRM_DEBUG_KMS("can clone using 1024x768\n");
1826 return true;
1827 }
1828 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1829 return false;
1830}
1831
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001832static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1833 struct drm_display_mode **modes,
1834 struct drm_fb_offset *offsets,
1835 int idx,
1836 int h_idx, int v_idx)
1837{
1838 struct drm_fb_helper_connector *fb_helper_conn;
1839 int i;
1840 int hoffset = 0, voffset = 0;
1841
1842 for (i = 0; i < fb_helper->connector_count; i++) {
1843 fb_helper_conn = fb_helper->connector_info[i];
1844 if (!fb_helper_conn->connector->has_tile)
1845 continue;
1846
1847 if (!modes[i] && (h_idx || v_idx)) {
1848 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1849 fb_helper_conn->connector->base.id);
1850 continue;
1851 }
1852 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1853 hoffset += modes[i]->hdisplay;
1854
1855 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1856 voffset += modes[i]->vdisplay;
1857 }
1858 offsets[idx].x = hoffset;
1859 offsets[idx].y = voffset;
1860 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1861 return 0;
1862}
1863
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001864static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001865 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001866 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001867 bool *enabled, int width, int height)
1868{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001869 struct drm_fb_helper_connector *fb_helper_conn;
1870 int i;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001871 uint64_t conn_configured = 0, mask;
1872 int tile_pass = 0;
1873 mask = (1 << fb_helper->connector_count) - 1;
1874retry:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001875 for (i = 0; i < fb_helper->connector_count; i++) {
1876 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001877
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001878 if (conn_configured & (1 << i))
Dave Airlie38651672010-03-30 05:34:13 +00001879 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001880
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001881 if (enabled[i] == false) {
1882 conn_configured |= (1 << i);
1883 continue;
1884 }
1885
1886 /* first pass over all the untiled connectors */
1887 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1888 continue;
1889
1890 if (tile_pass == 1) {
1891 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1892 fb_helper_conn->connector->tile_v_loc != 0)
1893 continue;
1894
1895 } else {
1896 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1897 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1898 /* if this tile_pass doesn't cover any of the tiles - keep going */
1899 continue;
1900
1901 /* find the tile offsets for this pass - need
1902 to find all tiles left and above */
1903 drm_get_tile_offsets(fb_helper, modes, offsets,
1904 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1905 }
Dave Airlie38651672010-03-30 05:34:13 +00001906 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001907 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001908
1909 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001910 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001911 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001912 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1913 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 +00001914 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001915 }
1916 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001917 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1918 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001919 break;
1920 }
1921 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1922 "none");
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001923 conn_configured |= (1 << i);
1924 }
1925
1926 if ((conn_configured & mask) != mask) {
1927 tile_pass++;
1928 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00001929 }
1930 return true;
1931}
1932
Dave Airlie8be48d92010-03-30 05:34:14 +00001933static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1934 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001935 struct drm_display_mode **modes,
1936 int n, int width, int height)
1937{
1938 int c, o;
1939 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02001940 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00001941 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001942 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001943 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001944 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001945
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001946 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001947 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001948
1949 fb_helper_conn = fb_helper->connector_info[n];
1950 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001951
1952 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001953 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001954 if (modes[n] == NULL)
1955 return best_score;
1956
Lyude255f0e72016-05-12 10:56:59 -04001957 crtcs = kzalloc(fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00001958 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001959 if (!crtcs)
1960 return best_score;
1961
1962 my_score = 1;
1963 if (connector->status == connector_status_connected)
1964 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001965 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001966 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001967 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001968 my_score++;
1969
1970 connector_funcs = connector->helper_private;
Boris Brezillonc61b93f2016-06-07 13:47:56 +02001971
1972 /*
1973 * If the DRM device implements atomic hooks and ->best_encoder() is
1974 * NULL we fallback to the default drm_atomic_helper_best_encoder()
1975 * helper.
1976 */
1977 if (fb_helper->dev->mode_config.funcs->atomic_commit &&
1978 !connector_funcs->best_encoder)
1979 encoder = drm_atomic_helper_best_encoder(connector);
1980 else
1981 encoder = connector_funcs->best_encoder(connector);
1982
Dave Airlie38651672010-03-30 05:34:13 +00001983 if (!encoder)
1984 goto out;
1985
Dave Airlie38651672010-03-30 05:34:13 +00001986 /* select a crtc for this connector and then attempt to configure
1987 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001988 for (c = 0; c < fb_helper->crtc_count; c++) {
1989 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001990
Sachin Kamat96081cd2012-11-15 03:43:30 +00001991 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001992 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001993
1994 for (o = 0; o < n; o++)
1995 if (best_crtcs[o] == crtc)
1996 break;
1997
1998 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001999 /* ignore cloning unless only a single crtc */
2000 if (fb_helper->crtc_count > 1)
2001 continue;
2002
2003 if (!drm_mode_equal(modes[o], modes[n]))
2004 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002005 }
2006
2007 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002008 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2009 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002010 width, height);
2011 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002012 best_score = score;
2013 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002014 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002015 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002016 }
Dave Airlie38651672010-03-30 05:34:13 +00002017 }
2018out:
2019 kfree(crtcs);
2020 return best_score;
2021}
2022
Dave Airlie8be48d92010-03-30 05:34:14 +00002023static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002024{
Dave Airlie8be48d92010-03-30 05:34:14 +00002025 struct drm_device *dev = fb_helper->dev;
2026 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002027 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002028 struct drm_fb_offset *offsets;
Dave Airlie8be48d92010-03-30 05:34:14 +00002029 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00002030 bool *enabled;
2031 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002032 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002033
2034 DRM_DEBUG_KMS("\n");
2035
2036 width = dev->mode_config.max_width;
2037 height = dev->mode_config.max_height;
2038
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002039 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002040 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002041 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002042 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002043 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002044 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002045 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002046 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002047 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002048 DRM_ERROR("Memory allocation failed\n");
2049 goto out;
2050 }
2051
Dave Airlie38651672010-03-30 05:34:13 +00002052
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002053 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002054
Jesse Barnes11e17a02013-02-19 13:31:39 -08002055 if (!(fb_helper->funcs->initial_config &&
2056 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002057 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002058 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002059 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2060 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2061 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002062
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002063 if (!drm_target_cloned(fb_helper, modes, offsets,
2064 enabled, width, height) &&
2065 !drm_target_preferred(fb_helper, modes, offsets,
2066 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002067 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002068
2069 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2070 width, height);
2071
2072 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002073 }
Dave Airlie38651672010-03-30 05:34:13 +00002074
Dave Airlie8be48d92010-03-30 05:34:14 +00002075 /* need to set the modesets up here for use later */
2076 /* fill out the connector<->crtc mappings into the modesets */
2077 for (i = 0; i < fb_helper->crtc_count; i++) {
2078 modeset = &fb_helper->crtc_info[i].mode_set;
2079 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002080 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002081 }
Dave Airlie38651672010-03-30 05:34:13 +00002082
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002083 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00002084 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002085 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002086 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002087 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00002088
Dave Airlie8be48d92010-03-30 05:34:14 +00002089 if (mode && fb_crtc) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002090 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2091 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Dave Airlie8be48d92010-03-30 05:34:14 +00002092 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002093 fb_crtc->x = offset->x;
2094 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002095 if (modeset->mode)
2096 drm_mode_destroy(dev, modeset->mode);
2097 modeset->mode = drm_mode_duplicate(dev,
2098 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002099 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002100 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002101 modeset->x = offset->x;
2102 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002103 }
Dave Airlie38651672010-03-30 05:34:13 +00002104 }
2105
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002106 /* Clear out any old modes if there are no more connected outputs. */
2107 for (i = 0; i < fb_helper->crtc_count; i++) {
2108 modeset = &fb_helper->crtc_info[i].mode_set;
2109 if (modeset->num_connectors == 0) {
2110 BUG_ON(modeset->fb);
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002111 if (modeset->mode)
2112 drm_mode_destroy(dev, modeset->mode);
2113 modeset->mode = NULL;
2114 }
2115 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002116out:
Dave Airlie38651672010-03-30 05:34:13 +00002117 kfree(crtcs);
2118 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002119 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002120 kfree(enabled);
2121}
2122
2123/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002124 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002125 * @fb_helper: fb_helper device struct
2126 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002127 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002128 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002129 * At the moment, this is a cloned configuration across all heads with
2130 * a new framebuffer object as the backing store.
2131 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002132 * Note that this also registers the fbdev and so allows userspace to call into
2133 * the driver through the fbdev interfaces.
2134 *
2135 * This function will call down into the ->fb_probe callback to let
2136 * the driver allocate and initialize the fbdev info structure and the drm
2137 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2138 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2139 * values for the fbdev info structure.
2140 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002141 * HANG DEBUGGING:
2142 *
2143 * When you have fbcon support built-in or already loaded, this function will do
2144 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2145 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2146 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2147 * to consoles, not even serial console. This means when your driver crashes,
2148 * you will see absolutely nothing else but a system stuck in this function,
2149 * with no further output. Any kind of printk() you place within your own driver
2150 * or in the drm core modeset code will also never show up.
2151 *
2152 * Standard debug practice is to run the fbcon setup without taking the
2153 * console_lock as a hack, to be able to see backtraces and crashes on the
2154 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2155 * cmdline option.
2156 *
2157 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002158 * first modeset from userspace will crash in the same way, and is even easier
2159 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002160 * kernel cmdline option.
2161 *
Dave Airlie38651672010-03-30 05:34:13 +00002162 * RETURNS:
2163 * Zero if everything went ok, nonzero otherwise.
2164 */
Thierry Reding01934c22014-12-19 11:21:32 +01002165int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002166{
Dave Airlie8be48d92010-03-30 05:34:14 +00002167 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00002168 int count = 0;
2169
Daniel Vetterf64c5572015-08-25 15:45:13 +02002170 if (!drm_fbdev_emulation)
2171 return 0;
2172
Daniel Vetter53f19042014-03-20 14:26:35 +01002173 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002174 count = drm_fb_helper_probe_connector_modes(fb_helper,
2175 dev->mode_config.max_width,
2176 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01002177 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00002178 /*
2179 * we shouldn't end up with no modes here.
2180 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00002181 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00002182 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00002183
Dave Airlie8be48d92010-03-30 05:34:14 +00002184 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00002185
Dave Airlie4abe3522010-03-30 05:34:18 +00002186 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00002187}
Dave Airlie8be48d92010-03-30 05:34:14 +00002188EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002189
Chris Wilson73943712011-04-22 11:03:57 +01002190/**
2191 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002192 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01002193 * @fb_helper: the drm_fb_helper
2194 *
Chris Wilson73943712011-04-22 11:03:57 +01002195 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002196 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002197 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002198 * Called at runtime, takes the mode config locks to be able to check/change the
2199 * modeset configuration. Must be run from process context (which usually means
2200 * either the output polling work or a work item launched from the driver's
2201 * hotplug interrupt).
2202 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002203 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002204 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002205 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2206 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002207 *
Chris Wilson73943712011-04-22 11:03:57 +01002208 * RETURNS:
2209 * 0 on success and a non-zero error code otherwise.
2210 */
2211int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002212{
Chris Wilson73943712011-04-22 11:03:57 +01002213 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01002214 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00002215
Daniel Vetterf64c5572015-08-25 15:45:13 +02002216 if (!drm_fbdev_emulation)
2217 return 0;
2218
Daniel Vetter89ced122013-04-11 14:26:55 +00002219 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002220 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002221 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00002222 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01002223 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002224 }
Dave Airlie38651672010-03-30 05:34:13 +00002225 DRM_DEBUG_KMS("\n");
2226
Dave Airlie4abe3522010-03-30 05:34:18 +00002227 max_width = fb_helper->fb->width;
2228 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00002229
Lespiau, Damien51bbd272013-09-28 16:24:05 +01002230 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00002231 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2232
2233 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00002234 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01002235 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002236 drm_fb_helper_set_par(fb_helper->fbdev);
2237
2238 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002239}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002240EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002241
David Rientjes6a108a12011-01-20 14:44:16 -08002242/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002243 * but the module doesn't depend on any fb console symbols. At least
2244 * attempt to load fbcon to avoid leaving the system without a usable console.
2245 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002246int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002247{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002248#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002249 const char *name = "fbcon";
2250 struct module *fbcon;
2251
2252 mutex_lock(&module_mutex);
2253 fbcon = find_module(name);
2254 mutex_unlock(&module_mutex);
2255
2256 if (!fbcon)
2257 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002258#endif
David Fries3ce05162010-12-12 12:39:22 -06002259 return 0;
2260}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002261EXPORT_SYMBOL(drm_fb_helper_modinit);