blob: cf775a4449c1a7b7f6e13bc5a050ea6117ff779c [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>
Dave Airlie785b93e2009-08-28 15:46:53 +100035#include <linux/fb.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_crtc_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100041
42static LIST_HEAD(kernel_fb_helper_list);
43
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010044/**
45 * DOC: fbdev helpers
46 *
47 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020048 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010049 * helper functions used by many drivers to implement the kernel mode setting
50 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010051 *
Thierry Reding10a23102014-06-27 17:19:24 +020052 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
53 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
54 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
55 * default behaviour can override the third step with their own code.
56 * Teardown is done with drm_fb_helper_fini().
Daniel Vetter207fd322013-01-20 22:13:14 +010057 *
58 * At runtime drivers should restore the fbdev console by calling
59 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
60 * should also notify the fb helper code from updates to the output
61 * configuration by calling drm_fb_helper_hotplug_event(). For easier
62 * integration with the output polling code in drm_crtc_helper.c the modeset
Thierry Reding83c617c2014-04-29 11:44:35 +020063 * code provides a ->output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010064 *
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020067 *
68 * It is possible, though perhaps somewhat tricky, to implement race-free
69 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
70 * helper must be called first to initialize the minimum required to make
71 * hotplug detection work. Drivers also need to make sure to properly set up
72 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
73 * it is safe to enable interrupts and start processing hotplug events. At the
74 * same time, drivers should initialize all modeset objects such as CRTCs,
75 * encoders and connectors. To finish up the fbdev helper initialization, the
76 * drm_fb_helper_init() function is called. To probe for all attached displays
77 * and set up an initial configuration using the detected hardware, drivers
78 * should call drm_fb_helper_single_add_all_connectors() followed by
79 * drm_fb_helper_initial_config().
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010080 */
81
Daniel Vetter207fd322013-01-20 22:13:14 +010082/**
83 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
84 * emulation helper
85 * @fb_helper: fbdev initialized with drm_fb_helper_init
86 *
87 * This functions adds all the available connectors for use with the given
88 * fb_helper. This is a separate step to allow drivers to freely assign
89 * connectors to the fbdev, e.g. if some are reserved for special purposes or
90 * not adequate to be used for the fbcon.
91 *
92 * Since this is part of the initial setup before the fbdev is published, no
93 * locking is required.
94 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +000095int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100096{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000097 struct drm_device *dev = fb_helper->dev;
98 struct drm_connector *connector;
99 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000100
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000101 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
102 struct drm_fb_helper_connector *fb_helper_connector;
103
104 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
105 if (!fb_helper_connector)
106 goto fail;
107
108 fb_helper_connector->connector = connector;
109 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
110 }
Dave Airlied50ba252009-09-23 14:44:08 +1000111 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000112fail:
113 for (i = 0; i < fb_helper->connector_count; i++) {
114 kfree(fb_helper->connector_info[i]);
115 fb_helper->connector_info[i] = NULL;
116 }
117 fb_helper->connector_count = 0;
118 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +1000119}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000120EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000121
Dave Airlie65c2a892014-06-05 14:01:30 +1000122int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
123{
124 struct drm_fb_helper_connector **temp;
125 struct drm_fb_helper_connector *fb_helper_connector;
126
127 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
128 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
Damien Lespiau14f476f2014-08-08 19:15:20 +0100129 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 +1000130 if (!temp)
131 return -ENOMEM;
132
133 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
134 fb_helper->connector_info = temp;
135 }
136
137
138 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
139 if (!fb_helper_connector)
140 return -ENOMEM;
141
142 fb_helper_connector->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
144 return 0;
145}
146EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
147
148int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
149 struct drm_connector *connector)
150{
151 struct drm_fb_helper_connector *fb_helper_connector;
152 int i, j;
153
154 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
155
156 for (i = 0; i < fb_helper->connector_count; i++) {
157 if (fb_helper->connector_info[i]->connector == connector)
158 break;
159 }
160
161 if (i == fb_helper->connector_count)
162 return -EINVAL;
163 fb_helper_connector = fb_helper->connector_info[i];
164
165 for (j = i + 1; j < fb_helper->connector_count; j++) {
166 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
167 }
168 fb_helper->connector_count--;
169 kfree(fb_helper_connector);
170 return 0;
171}
172EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
173
Jason Wessel99231022010-10-13 14:09:43 -0500174static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
175{
176 uint16_t *r_base, *g_base, *b_base;
177 int i;
178
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300179 if (helper->funcs->gamma_get == NULL)
180 return;
181
Jason Wessel99231022010-10-13 14:09:43 -0500182 r_base = crtc->gamma_store;
183 g_base = r_base + crtc->gamma_size;
184 b_base = g_base + crtc->gamma_size;
185
186 for (i = 0; i < crtc->gamma_size; i++)
187 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
188}
189
190static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
191{
192 uint16_t *r_base, *g_base, *b_base;
193
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200194 if (crtc->funcs->gamma_set == NULL)
195 return;
196
Jason Wessel99231022010-10-13 14:09:43 -0500197 r_base = crtc->gamma_store;
198 g_base = r_base + crtc->gamma_size;
199 b_base = g_base + crtc->gamma_size;
200
201 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
202}
203
Daniel Vetter207fd322013-01-20 22:13:14 +0100204/**
205 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
206 * @info: fbdev registered by the helper
207 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500208int drm_fb_helper_debug_enter(struct fb_info *info)
209{
210 struct drm_fb_helper *helper = info->par;
211 struct drm_crtc_helper_funcs *funcs;
212 int i;
213
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500214 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
215 for (i = 0; i < helper->crtc_count; i++) {
216 struct drm_mode_set *mode_set =
217 &helper->crtc_info[i].mode_set;
218
219 if (!mode_set->crtc->enabled)
220 continue;
221
222 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500223 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500224 funcs->mode_set_base_atomic(mode_set->crtc,
225 mode_set->fb,
226 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500227 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500228 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500229 }
230 }
231
232 return 0;
233}
234EXPORT_SYMBOL(drm_fb_helper_debug_enter);
235
236/* Find the real fb for a given fb helper CRTC */
237static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
238{
239 struct drm_device *dev = crtc->dev;
240 struct drm_crtc *c;
241
242 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
243 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700244 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500245 }
246
247 return NULL;
248}
249
Daniel Vetter207fd322013-01-20 22:13:14 +0100250/**
251 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
252 * @info: fbdev registered by the helper
253 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500254int drm_fb_helper_debug_leave(struct fb_info *info)
255{
256 struct drm_fb_helper *helper = info->par;
257 struct drm_crtc *crtc;
258 struct drm_crtc_helper_funcs *funcs;
259 struct drm_framebuffer *fb;
260 int i;
261
262 for (i = 0; i < helper->crtc_count; i++) {
263 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
264 crtc = mode_set->crtc;
265 funcs = crtc->helper_private;
266 fb = drm_mode_config_fb(crtc);
267
268 if (!crtc->enabled)
269 continue;
270
271 if (!fb) {
272 DRM_ERROR("no fb to restore??\n");
273 continue;
274 }
275
Jason Wessel99231022010-10-13 14:09:43 -0500276 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500277 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500278 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500279 }
280
281 return 0;
282}
283EXPORT_SYMBOL(drm_fb_helper_debug_leave);
284
Rob Clark5ea1f752014-05-30 12:29:48 -0400285static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100286{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300287 struct drm_device *dev = fb_helper->dev;
288 struct drm_plane *plane;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100289 bool error = false;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300290 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100291
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300292 drm_warn_on_modeset_not_all_locked(dev);
293
Sonika Jindal9783de22014-08-05 11:26:57 +0530294 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Matt Ropere27dde32014-04-01 15:22:30 -0700295 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
296 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100297
Sonika Jindal9783de22014-08-05 11:26:57 +0530298 if (dev->mode_config.rotation_property) {
Thomas Wood3a5f87c2014-08-20 14:45:00 +0100299 drm_mode_plane_set_obj_prop(plane,
300 dev->mode_config.rotation_property,
301 BIT(DRM_ROTATE_0));
Sonika Jindal9783de22014-08-05 11:26:57 +0530302 }
303 }
304
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100305 for (i = 0; i < fb_helper->crtc_count; i++) {
306 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300307 struct drm_crtc *crtc = mode_set->crtc;
308 int ret;
309
310 if (crtc->funcs->cursor_set) {
311 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
312 if (ret)
313 error = true;
314 }
315
Daniel Vetter2d13b672012-12-11 13:47:23 +0100316 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100317 if (ret)
318 error = true;
319 }
320 return error;
321}
Rob Clark5ea1f752014-05-30 12:29:48 -0400322/**
323 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
324 * @fb_helper: fbcon to restore
325 *
326 * This should be called from driver's drm ->lastclose callback
327 * when implementing an fbcon on top of kms using this helper. This ensures that
328 * the user isn't greeted with a black screen when e.g. X dies.
329 *
330 * Use this variant if you need to bypass locking (panic), or already
331 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
332 */
333static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
334{
335 return restore_fbdev_mode(fb_helper);
336}
337
338/**
339 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
340 * @fb_helper: fbcon to restore
341 *
342 * This should be called from driver's drm ->lastclose callback
343 * when implementing an fbcon on top of kms using this helper. This ensures that
344 * the user isn't greeted with a black screen when e.g. X dies.
345 */
346bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
347{
348 struct drm_device *dev = fb_helper->dev;
349 bool ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000350 bool do_delayed = false;
351
Rob Clark5ea1f752014-05-30 12:29:48 -0400352 drm_modeset_lock_all(dev);
353 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000354
355 do_delayed = fb_helper->delayed_hotplug;
356 if (do_delayed)
357 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400358 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000359
360 if (do_delayed)
361 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400362 return ret;
363}
364EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100365
Daniel Vetterd21bf462013-01-20 18:09:52 +0100366/*
367 * restore fbcon display for all kms driver's using this helper, used for sysrq
368 * and panic handling.
369 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530370static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000371{
Dave Airlie785b93e2009-08-28 15:46:53 +1000372 bool ret, error = false;
373 struct drm_fb_helper *helper;
374
375 if (list_empty(&kernel_fb_helper_list))
376 return false;
377
378 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200379 struct drm_device *dev = helper->dev;
380
381 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100382 continue;
383
Daniel Vettercb597bb2014-07-27 19:09:33 +0200384 /*
385 * NOTE: Use trylock mode to avoid deadlocks and sleeping in
386 * panic context.
Rob Clark51fd3712013-11-19 12:10:12 -0500387 */
Daniel Vettercb597bb2014-07-27 19:09:33 +0200388 if (__drm_modeset_lock_all(dev, true) != 0) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200389 error = true;
390 continue;
391 }
392
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100393 ret = drm_fb_helper_restore_fbdev_mode(helper);
394 if (ret)
395 error = true;
Thierry Redingb77f0762014-04-29 11:44:32 +0200396
Daniel Vettercb597bb2014-07-27 19:09:33 +0200397 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000398 }
399 return error;
400}
401
Daniel Vetter43c8a842013-01-20 18:18:07 +0100402static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
Dave Airlie785b93e2009-08-28 15:46:53 +1000403 void *panic_str)
404{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700405 /*
406 * It's a waste of time and effort to switch back to text console
407 * if the kernel should reboot before panic messages can be seen.
408 */
409 if (panic_timeout < 0)
410 return 0;
411
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000412 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000413 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000414}
Dave Airlie785b93e2009-08-28 15:46:53 +1000415
416static struct notifier_block paniced = {
417 .notifier_call = drm_fb_helper_panic,
418};
419
Daniel Vetter20c60c32012-12-17 12:13:23 +0100420static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
421{
422 struct drm_device *dev = fb_helper->dev;
423 struct drm_crtc *crtc;
424 int bound = 0, crtcs_bound = 0;
425
Paulo Zanoni520edd12013-11-27 18:24:08 -0200426 /* Sometimes user space wants everything disabled, so don't steal the
427 * display if there's a master. */
428 if (dev->primary->master)
429 return false;
430
Daniel Vetter20c60c32012-12-17 12:13:23 +0100431 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700432 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100433 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700434 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100435 bound++;
436 }
437
438 if (bound < crtcs_bound)
439 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200440
Daniel Vetter20c60c32012-12-17 12:13:23 +0100441 return true;
442}
443
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200444#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000445static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
446{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100447 bool ret;
448 ret = drm_fb_helper_force_kernel_mode();
449 if (ret == true)
450 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000451}
452static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
453
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700454static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000455{
456 schedule_work(&drm_fb_helper_restore_work);
457}
458
459static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
460 .handler = drm_fb_helper_sysrq,
461 .help_msg = "force-fb(V)",
462 .action_msg = "Restore framebuffer console",
463};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000464#else
465static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200466#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000467
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100468static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000469{
470 struct drm_fb_helper *fb_helper = info->par;
471 struct drm_device *dev = fb_helper->dev;
472 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700473 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700474 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000475
476 /*
Daniel Vetter1b1d5392013-01-24 16:42:07 +0100477 * fbdev->blank can be called from irq context in case of a panic.
478 * Since we already have our own special panic handler which will
479 * restore the fbdev console mode completely, just bail out early.
480 */
481 if (oops_in_progress)
482 return;
483
484 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100485 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000486 */
Daniel Vetter84849902012-12-02 00:28:11 +0100487 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100488 if (!drm_fb_helper_is_bound(fb_helper)) {
489 drm_modeset_unlock_all(dev);
490 return;
491 }
492
Jesse Barnese87b2c42009-09-17 18:14:41 -0700493 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000494 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000495
Dave Airlie8be48d92010-03-30 05:34:14 +0000496 if (!crtc->enabled)
497 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000498
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100499 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700500 for (j = 0; j < fb_helper->connector_count; j++) {
501 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200502 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500503 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100504 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700505 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000506 }
Daniel Vetter84849902012-12-02 00:28:11 +0100507 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000508}
509
Daniel Vetter207fd322013-01-20 22:13:14 +0100510/**
511 * drm_fb_helper_blank - implementation for ->fb_blank
512 * @blank: desired blanking state
513 * @info: fbdev registered by the helper
514 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000515int drm_fb_helper_blank(int blank, struct fb_info *info)
516{
517 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000518 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000519 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100520 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000521 break;
James Simmons731b5a12009-10-29 20:39:07 +0000522 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000523 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100524 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000525 break;
James Simmons731b5a12009-10-29 20:39:07 +0000526 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000527 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100528 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000529 break;
James Simmons731b5a12009-10-29 20:39:07 +0000530 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000531 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100532 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000533 break;
James Simmons731b5a12009-10-29 20:39:07 +0000534 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000535 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100536 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000537 break;
538 }
539 return 0;
540}
541EXPORT_SYMBOL(drm_fb_helper_blank);
542
543static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
544{
545 int i;
546
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000547 for (i = 0; i < helper->connector_count; i++)
548 kfree(helper->connector_info[i]);
549 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100550 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000551 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100552 if (helper->crtc_info[i].mode_set.mode)
553 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
554 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000555 kfree(helper->crtc_info);
556}
557
Daniel Vetter207fd322013-01-20 22:13:14 +0100558/**
Thierry Reding10a23102014-06-27 17:19:24 +0200559 * drm_fb_helper_prepare - setup a drm_fb_helper structure
560 * @dev: DRM device
561 * @helper: driver-allocated fbdev helper structure to set up
562 * @funcs: pointer to structure of functions associate with this helper
563 *
564 * Sets up the bare minimum to make the framebuffer helper usable. This is
565 * useful to implement race-free initialization of the polling helpers.
566 */
567void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
568 const struct drm_fb_helper_funcs *funcs)
569{
570 INIT_LIST_HEAD(&helper->kernel_fb_list);
571 helper->funcs = funcs;
572 helper->dev = dev;
573}
574EXPORT_SYMBOL(drm_fb_helper_prepare);
575
576/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100577 * drm_fb_helper_init - initialize a drm_fb_helper structure
578 * @dev: drm device
579 * @fb_helper: driver-allocated fbdev helper structure to initialize
580 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
581 * @max_conn_count: max connector count
582 *
583 * This allocates the structures for the fbdev helper with the given limits.
584 * Note that this won't yet touch the hardware (through the driver interfaces)
585 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
586 * to allow driver writes more control over the exact init sequence.
587 *
Thierry Reding10a23102014-06-27 17:19:24 +0200588 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100589 *
590 * RETURNS:
591 * Zero if everything went ok, nonzero otherwise.
592 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000593int drm_fb_helper_init(struct drm_device *dev,
594 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000595 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000596{
Dave Airlie785b93e2009-08-28 15:46:53 +1000597 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000598 int i;
599
Xiubo Li04cfe972014-03-10 09:33:58 +0800600 if (!max_conn_count)
601 return -EINVAL;
602
Dave Airlie4abe3522010-03-30 05:34:18 +0000603 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
604 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000605 return -ENOMEM;
606
Dave Airlie4abe3522010-03-30 05:34:18 +0000607 fb_helper->crtc_count = crtc_count;
608 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
609 if (!fb_helper->connector_info) {
610 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000611 return -ENOMEM;
612 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000613 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000614 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000615
616 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000617 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000618 kcalloc(max_conn_count,
619 sizeof(struct drm_connector *),
620 GFP_KERNEL);
621
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200622 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000623 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000624 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000625 }
626
627 i = 0;
628 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000629 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000630 i++;
631 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100632
Dave Airlie785b93e2009-08-28 15:46:53 +1000633 return 0;
634out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000635 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000636 return -ENOMEM;
637}
Dave Airlie4abe3522010-03-30 05:34:18 +0000638EXPORT_SYMBOL(drm_fb_helper_init);
639
640void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
641{
642 if (!list_empty(&fb_helper->kernel_fb_list)) {
643 list_del(&fb_helper->kernel_fb_list);
644 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000645 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000646 atomic_notifier_chain_unregister(&panic_notifier_list,
647 &paniced);
648 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
649 }
650 }
651
652 drm_fb_helper_crtc_free(fb_helper);
653
Dave Airlie4abe3522010-03-30 05:34:18 +0000654}
655EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000656
Dave Airliec850cb72009-10-23 18:49:03 +1000657static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000658 u16 blue, u16 regno, struct fb_info *info)
659{
660 struct drm_fb_helper *fb_helper = info->par;
661 struct drm_framebuffer *fb = fb_helper->fb;
662 int pindex;
663
Dave Airliec850cb72009-10-23 18:49:03 +1000664 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
665 u32 *palette;
666 u32 value;
667 /* place color in psuedopalette */
668 if (regno > 16)
669 return -EINVAL;
670 palette = (u32 *)info->pseudo_palette;
671 red >>= (16 - info->var.red.length);
672 green >>= (16 - info->var.green.length);
673 blue >>= (16 - info->var.blue.length);
674 value = (red << info->var.red.offset) |
675 (green << info->var.green.offset) |
676 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000677 if (info->var.transp.length > 0) {
678 u32 mask = (1 << info->var.transp.length) - 1;
679 mask <<= info->var.transp.offset;
680 value |= mask;
681 }
Dave Airliec850cb72009-10-23 18:49:03 +1000682 palette[regno] = value;
683 return 0;
684 }
685
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300686 /*
687 * The driver really shouldn't advertise pseudo/directcolor
688 * visuals if it can't deal with the palette.
689 */
690 if (WARN_ON(!fb_helper->funcs->gamma_set ||
691 !fb_helper->funcs->gamma_get))
692 return -EINVAL;
693
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000694 pindex = regno;
695
696 if (fb->bits_per_pixel == 16) {
697 pindex = regno << 3;
698
699 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000700 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000701 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000702 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000703
704 if (fb->depth == 16) {
705 u16 r, g, b;
706 int i;
707 if (regno < 32) {
708 for (i = 0; i < 8; i++)
709 fb_helper->funcs->gamma_set(crtc, red,
710 green, blue, pindex + i);
711 }
712
713 fb_helper->funcs->gamma_get(crtc, &r,
714 &g, &b,
715 pindex >> 1);
716
717 for (i = 0; i < 4; i++)
718 fb_helper->funcs->gamma_set(crtc, r,
719 green, b,
720 (pindex >> 1) + i);
721 }
722 }
723
724 if (fb->depth != 16)
725 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000726 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000727}
728
Daniel Vetter207fd322013-01-20 22:13:14 +0100729/**
730 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
731 * @cmap: cmap to set
732 * @info: fbdev registered by the helper
733 */
Dave Airlie068143d2009-10-05 09:58:02 +1000734int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
735{
736 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300737 struct drm_device *dev = fb_helper->dev;
Dave Airlie8be48d92010-03-30 05:34:14 +0000738 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000739 u16 *red, *green, *blue, *transp;
740 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100741 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000742 int start;
743
Rui Wang9aa609e2014-12-15 11:28:26 -0800744 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
745 return -EBUSY;
746 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300747 if (!drm_fb_helper_is_bound(fb_helper)) {
748 drm_modeset_unlock_all(dev);
749 return -EBUSY;
750 }
751
Dave Airlie8be48d92010-03-30 05:34:14 +0000752 for (i = 0; i < fb_helper->crtc_count; i++) {
753 crtc = fb_helper->crtc_info[i].mode_set.crtc;
754 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000755
756 red = cmap->red;
757 green = cmap->green;
758 blue = cmap->blue;
759 transp = cmap->transp;
760 start = cmap->start;
761
roel062ac622011-03-07 18:00:34 +0100762 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000763 u16 hred, hgreen, hblue, htransp = 0xffff;
764
765 hred = *red++;
766 hgreen = *green++;
767 hblue = *blue++;
768
769 if (transp)
770 htransp = *transp++;
771
Dave Airliec850cb72009-10-23 18:49:03 +1000772 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
773 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300774 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000775 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300776 if (crtc_funcs->load_lut)
777 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +1000778 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300779 out:
780 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +1000781 return rc;
782}
783EXPORT_SYMBOL(drm_fb_helper_setcmap);
784
Daniel Vetter207fd322013-01-20 22:13:14 +0100785/**
786 * drm_fb_helper_check_var - implementation for ->fb_check_var
787 * @var: screeninfo to check
788 * @info: fbdev registered by the helper
789 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000790int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
791 struct fb_info *info)
792{
793 struct drm_fb_helper *fb_helper = info->par;
794 struct drm_framebuffer *fb = fb_helper->fb;
795 int depth;
796
Jason Wesself90ebd92010-08-05 09:22:32 -0500797 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000798 return -EINVAL;
799
800 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100801 if (var->bits_per_pixel > fb->bits_per_pixel ||
802 var->xres > fb->width || var->yres > fb->height ||
803 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000804 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100805 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
806 var->xres, var->yres, var->bits_per_pixel,
807 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000808 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000809 return -EINVAL;
810 }
811
812 switch (var->bits_per_pixel) {
813 case 16:
814 depth = (var->green.length == 6) ? 16 : 15;
815 break;
816 case 32:
817 depth = (var->transp.length > 0) ? 32 : 24;
818 break;
819 default:
820 depth = var->bits_per_pixel;
821 break;
822 }
823
824 switch (depth) {
825 case 8:
826 var->red.offset = 0;
827 var->green.offset = 0;
828 var->blue.offset = 0;
829 var->red.length = 8;
830 var->green.length = 8;
831 var->blue.length = 8;
832 var->transp.length = 0;
833 var->transp.offset = 0;
834 break;
835 case 15:
836 var->red.offset = 10;
837 var->green.offset = 5;
838 var->blue.offset = 0;
839 var->red.length = 5;
840 var->green.length = 5;
841 var->blue.length = 5;
842 var->transp.length = 1;
843 var->transp.offset = 15;
844 break;
845 case 16:
846 var->red.offset = 11;
847 var->green.offset = 5;
848 var->blue.offset = 0;
849 var->red.length = 5;
850 var->green.length = 6;
851 var->blue.length = 5;
852 var->transp.length = 0;
853 var->transp.offset = 0;
854 break;
855 case 24:
856 var->red.offset = 16;
857 var->green.offset = 8;
858 var->blue.offset = 0;
859 var->red.length = 8;
860 var->green.length = 8;
861 var->blue.length = 8;
862 var->transp.length = 0;
863 var->transp.offset = 0;
864 break;
865 case 32:
866 var->red.offset = 16;
867 var->green.offset = 8;
868 var->blue.offset = 0;
869 var->red.length = 8;
870 var->green.length = 8;
871 var->blue.length = 8;
872 var->transp.length = 8;
873 var->transp.offset = 24;
874 break;
875 default:
876 return -EINVAL;
877 }
878 return 0;
879}
880EXPORT_SYMBOL(drm_fb_helper_check_var);
881
Daniel Vetter207fd322013-01-20 22:13:14 +0100882/**
883 * drm_fb_helper_set_par - implementation for ->fb_set_par
884 * @info: fbdev registered by the helper
885 *
886 * This will let fbcon do the mode init and is called at initialization time by
887 * the fbdev core when registering the driver, and later on through the hotplug
888 * callback.
889 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000890int drm_fb_helper_set_par(struct fb_info *info)
891{
892 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +1000893 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +1000894
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100895 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000896 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000897 return -EINVAL;
898 }
899
Rob Clark5ea1f752014-05-30 12:29:48 -0400900 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000901
Dave Airlie785b93e2009-08-28 15:46:53 +1000902 return 0;
903}
904EXPORT_SYMBOL(drm_fb_helper_set_par);
905
Daniel Vetter207fd322013-01-20 22:13:14 +0100906/**
907 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
908 * @var: updated screen information
909 * @info: fbdev registered by the helper
910 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000911int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
912 struct fb_info *info)
913{
914 struct drm_fb_helper *fb_helper = info->par;
915 struct drm_device *dev = fb_helper->dev;
916 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +1000917 int ret = 0;
918 int i;
919
Rui Wang9aa609e2014-12-15 11:28:26 -0800920 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
921 return -EBUSY;
922 }
Daniel Vetter20c60c32012-12-17 12:13:23 +0100923 if (!drm_fb_helper_is_bound(fb_helper)) {
924 drm_modeset_unlock_all(dev);
925 return -EBUSY;
926 }
927
Dave Airlie8be48d92010-03-30 05:34:14 +0000928 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000929 modeset = &fb_helper->crtc_info[i].mode_set;
930
931 modeset->x = var->xoffset;
932 modeset->y = var->yoffset;
933
934 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +0100935 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000936 if (!ret) {
937 info->var.xoffset = var->xoffset;
938 info->var.yoffset = var->yoffset;
939 }
940 }
941 }
Daniel Vetter84849902012-12-02 00:28:11 +0100942 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000943 return ret;
944}
945EXPORT_SYMBOL(drm_fb_helper_pan_display);
946
Daniel Vetter8acf6582013-01-21 23:38:37 +0100947/*
Daniel Vetter207fd322013-01-20 22:13:14 +0100948 * Allocates the backing storage and sets up the fbdev info structure through
949 * the ->fb_probe callback and then registers the fbdev and sets up the panic
950 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +0100951 */
Daniel Vetterde1ace52013-01-20 21:50:49 +0100952static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
953 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000954{
Daniel Vetter8acf6582013-01-21 23:38:37 +0100955 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000956 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000957 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000958 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000959 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000960 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000961
962 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
963 sizes.surface_depth = 24;
964 sizes.surface_bpp = 32;
965 sizes.fb_width = (unsigned)-1;
966 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000967
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000968 /* if driver picks 8 or 16 by default use that
969 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000970 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +0000971 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000972
Dave Airlie785b93e2009-08-28 15:46:53 +1000973 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000974 for (i = 0; i < fb_helper->connector_count; i++) {
975 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100976 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000977
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200978 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000979
980 if (cmdline_mode->bpp_specified) {
981 switch (cmdline_mode->bpp) {
982 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000983 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000984 break;
985 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000986 sizes.surface_depth = 15;
987 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000988 break;
989 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000990 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000991 break;
992 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000993 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000994 break;
995 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000996 sizes.surface_depth = 24;
997 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000998 break;
999 }
1000 break;
1001 }
1002 }
1003
Dave Airlie8be48d92010-03-30 05:34:14 +00001004 crtc_count = 0;
1005 for (i = 0; i < fb_helper->crtc_count; i++) {
1006 struct drm_display_mode *desired_mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001007 int x, y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001008 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001009 x = fb_helper->crtc_info[i].x;
1010 y = fb_helper->crtc_info[i].y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001011 if (desired_mode) {
1012 if (gamma_size == 0)
1013 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001014 if (desired_mode->hdisplay + x < sizes.fb_width)
1015 sizes.fb_width = desired_mode->hdisplay + x;
1016 if (desired_mode->vdisplay + y < sizes.fb_height)
1017 sizes.fb_height = desired_mode->vdisplay + y;
1018 if (desired_mode->hdisplay + x > sizes.surface_width)
1019 sizes.surface_width = desired_mode->hdisplay + x;
1020 if (desired_mode->vdisplay + y > sizes.surface_height)
1021 sizes.surface_height = desired_mode->vdisplay + y;
Dave Airlie785b93e2009-08-28 15:46:53 +10001022 crtc_count++;
1023 }
1024 }
1025
Dave Airlie38651672010-03-30 05:34:13 +00001026 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001027 /* hmm everyone went away - assume VGA cable just fell out
1028 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001029 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001030 sizes.fb_width = sizes.surface_width = 1024;
1031 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001032 }
1033
Dave Airlie38651672010-03-30 05:34:13 +00001034 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001035 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1036 if (ret < 0)
1037 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001038
Dave Airlie38651672010-03-30 05:34:13 +00001039 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001040
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001041 /*
1042 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1043 * events, but at init time drm_setup_crtcs needs to be called before
1044 * the fb is allocated (since we need to figure out the desired size of
1045 * the fb before we can allocate it ...). Hence we need to fix things up
1046 * here again.
1047 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001048 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001049 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1050 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1051
Dave Airlie785b93e2009-08-28 15:46:53 +10001052
Daniel Vetter8acf6582013-01-21 23:38:37 +01001053 info->var.pixclock = 0;
1054 if (register_framebuffer(info) < 0)
1055 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001056
Daniel Vetter8acf6582013-01-21 23:38:37 +01001057 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1058 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001059
1060 /* Switch back to kernel console on panic */
1061 /* multi card linked list maybe */
1062 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001063 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001064 atomic_notifier_chain_register(&panic_notifier_list,
1065 &paniced);
1066 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1067 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001068
1069 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001070
Dave Airlie785b93e2009-08-28 15:46:53 +10001071 return 0;
1072}
Dave Airlie785b93e2009-08-28 15:46:53 +10001073
Daniel Vetter207fd322013-01-20 22:13:14 +01001074/**
1075 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1076 * @info: fbdev registered by the helper
1077 * @pitch: desired pitch
1078 * @depth: desired depth
1079 *
1080 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1081 * fbdev emulations. Drivers which support acceleration methods which impose
1082 * additional constraints need to set up their own limits.
1083 *
1084 * Drivers should call this (or their equivalent setup code) from their
1085 * ->fb_probe callback.
1086 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001087void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1088 uint32_t depth)
1089{
1090 info->fix.type = FB_TYPE_PACKED_PIXELS;
1091 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1092 FB_VISUAL_TRUECOLOR;
1093 info->fix.mmio_start = 0;
1094 info->fix.mmio_len = 0;
1095 info->fix.type_aux = 0;
1096 info->fix.xpanstep = 1; /* doing it in hw */
1097 info->fix.ypanstep = 1; /* doing it in hw */
1098 info->fix.ywrapstep = 0;
1099 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001100
1101 info->fix.line_length = pitch;
1102 return;
1103}
1104EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1105
Daniel Vetter207fd322013-01-20 22:13:14 +01001106/**
1107 * drm_fb_helper_fill_var - initalizes variable fbdev information
1108 * @info: fbdev instance to set up
1109 * @fb_helper: fb helper instance to use as template
1110 * @fb_width: desired fb width
1111 * @fb_height: desired fb height
1112 *
1113 * Sets up the variable fbdev metainformation from the given fb helper instance
1114 * and the drm framebuffer allocated in fb_helper->fb.
1115 *
1116 * Drivers should call this (or their equivalent setup code) from their
1117 * ->fb_probe callback after having allocated the fbdev backing
1118 * storage framebuffer.
1119 */
Dave Airlie38651672010-03-30 05:34:13 +00001120void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001121 uint32_t fb_width, uint32_t fb_height)
1122{
Dave Airlie38651672010-03-30 05:34:13 +00001123 struct drm_framebuffer *fb = fb_helper->fb;
1124 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001125 info->var.xres_virtual = fb->width;
1126 info->var.yres_virtual = fb->height;
1127 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001128 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001129 info->var.xoffset = 0;
1130 info->var.yoffset = 0;
1131 info->var.activate = FB_ACTIVATE_NOW;
1132 info->var.height = -1;
1133 info->var.width = -1;
1134
1135 switch (fb->depth) {
1136 case 8:
1137 info->var.red.offset = 0;
1138 info->var.green.offset = 0;
1139 info->var.blue.offset = 0;
1140 info->var.red.length = 8; /* 8bit DAC */
1141 info->var.green.length = 8;
1142 info->var.blue.length = 8;
1143 info->var.transp.offset = 0;
1144 info->var.transp.length = 0;
1145 break;
1146 case 15:
1147 info->var.red.offset = 10;
1148 info->var.green.offset = 5;
1149 info->var.blue.offset = 0;
1150 info->var.red.length = 5;
1151 info->var.green.length = 5;
1152 info->var.blue.length = 5;
1153 info->var.transp.offset = 15;
1154 info->var.transp.length = 1;
1155 break;
1156 case 16:
1157 info->var.red.offset = 11;
1158 info->var.green.offset = 5;
1159 info->var.blue.offset = 0;
1160 info->var.red.length = 5;
1161 info->var.green.length = 6;
1162 info->var.blue.length = 5;
1163 info->var.transp.offset = 0;
1164 break;
1165 case 24:
1166 info->var.red.offset = 16;
1167 info->var.green.offset = 8;
1168 info->var.blue.offset = 0;
1169 info->var.red.length = 8;
1170 info->var.green.length = 8;
1171 info->var.blue.length = 8;
1172 info->var.transp.offset = 0;
1173 info->var.transp.length = 0;
1174 break;
1175 case 32:
1176 info->var.red.offset = 16;
1177 info->var.green.offset = 8;
1178 info->var.blue.offset = 0;
1179 info->var.red.length = 8;
1180 info->var.green.length = 8;
1181 info->var.blue.length = 8;
1182 info->var.transp.offset = 24;
1183 info->var.transp.length = 8;
1184 break;
1185 default:
1186 break;
1187 }
1188
1189 info->var.xres = fb_width;
1190 info->var.yres = fb_height;
1191}
1192EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001193
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001194static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1195 uint32_t maxX,
1196 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001197{
1198 struct drm_connector *connector;
1199 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001200 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001201
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001202 for (i = 0; i < fb_helper->connector_count; i++) {
1203 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001204 count += connector->funcs->fill_modes(connector, maxX, maxY);
1205 }
1206
1207 return count;
1208}
1209
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001210struct 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 +00001211{
1212 struct drm_display_mode *mode;
1213
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001214 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001215 if (mode->hdisplay > width ||
1216 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001217 continue;
1218 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1219 return mode;
1220 }
1221 return NULL;
1222}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001223EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001224
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001225static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001226{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001227 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001228}
1229
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001230struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001231 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001232{
Chris Wilson1794d252011-04-17 07:43:32 +01001233 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001234 struct drm_display_mode *mode = NULL;
Takashi Iwaic683f422014-03-19 14:53:13 +01001235 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001236
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001237 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001238 if (cmdline_mode->specified == false)
1239 return mode;
1240
1241 /* attempt to find a matching mode in the list of modes
1242 * we have gotten so far, if not add a CVT mode that conforms
1243 */
1244 if (cmdline_mode->rb || cmdline_mode->margins)
1245 goto create_mode;
1246
Takashi Iwaic683f422014-03-19 14:53:13 +01001247 prefer_non_interlace = !cmdline_mode->interlace;
1248 again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001249 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001250 /* check width/height */
1251 if (mode->hdisplay != cmdline_mode->xres ||
1252 mode->vdisplay != cmdline_mode->yres)
1253 continue;
1254
1255 if (cmdline_mode->refresh_specified) {
1256 if (mode->vrefresh != cmdline_mode->refresh)
1257 continue;
1258 }
1259
1260 if (cmdline_mode->interlace) {
1261 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1262 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001263 } else if (prefer_non_interlace) {
1264 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1265 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001266 }
1267 return mode;
1268 }
1269
Takashi Iwaic683f422014-03-19 14:53:13 +01001270 if (prefer_non_interlace) {
1271 prefer_non_interlace = false;
1272 goto again;
1273 }
1274
Dave Airlie38651672010-03-30 05:34:13 +00001275create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001276 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1277 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001278 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001279 return mode;
1280}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001281EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001282
1283static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1284{
1285 bool enable;
1286
Sachin Kamat96081cd2012-11-15 03:43:30 +00001287 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001288 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001289 else
Dave Airlie38651672010-03-30 05:34:13 +00001290 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001291
Dave Airlie38651672010-03-30 05:34:13 +00001292 return enable;
1293}
1294
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001295static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1296 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001297{
1298 bool any_enabled = false;
1299 struct drm_connector *connector;
1300 int i = 0;
1301
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001302 for (i = 0; i < fb_helper->connector_count; i++) {
1303 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001304 enabled[i] = drm_connector_enabled(connector, true);
1305 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1306 enabled[i] ? "yes" : "no");
1307 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001308 }
1309
1310 if (any_enabled)
1311 return;
1312
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001313 for (i = 0; i < fb_helper->connector_count; i++) {
1314 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001315 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001316 }
1317}
1318
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001319static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1320 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001321 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001322 bool *enabled, int width, int height)
1323{
1324 int count, i, j;
1325 bool can_clone = false;
1326 struct drm_fb_helper_connector *fb_helper_conn;
1327 struct drm_display_mode *dmt_mode, *mode;
1328
1329 /* only contemplate cloning in the single crtc case */
1330 if (fb_helper->crtc_count > 1)
1331 return false;
1332
1333 count = 0;
1334 for (i = 0; i < fb_helper->connector_count; i++) {
1335 if (enabled[i])
1336 count++;
1337 }
1338
1339 /* only contemplate cloning if more than one connector is enabled */
1340 if (count <= 1)
1341 return false;
1342
1343 /* check the command line or if nothing common pick 1024x768 */
1344 can_clone = true;
1345 for (i = 0; i < fb_helper->connector_count; i++) {
1346 if (!enabled[i])
1347 continue;
1348 fb_helper_conn = fb_helper->connector_info[i];
1349 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1350 if (!modes[i]) {
1351 can_clone = false;
1352 break;
1353 }
1354 for (j = 0; j < i; j++) {
1355 if (!enabled[j])
1356 continue;
1357 if (!drm_mode_equal(modes[j], modes[i]))
1358 can_clone = false;
1359 }
1360 }
1361
1362 if (can_clone) {
1363 DRM_DEBUG_KMS("can clone using command line\n");
1364 return true;
1365 }
1366
1367 /* try and find a 1024x768 mode on each connector */
1368 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001369 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001370
1371 for (i = 0; i < fb_helper->connector_count; i++) {
1372
1373 if (!enabled[i])
1374 continue;
1375
1376 fb_helper_conn = fb_helper->connector_info[i];
1377 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1378 if (drm_mode_equal(mode, dmt_mode))
1379 modes[i] = mode;
1380 }
1381 if (!modes[i])
1382 can_clone = false;
1383 }
1384
1385 if (can_clone) {
1386 DRM_DEBUG_KMS("can clone using 1024x768\n");
1387 return true;
1388 }
1389 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1390 return false;
1391}
1392
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001393static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1394 struct drm_display_mode **modes,
1395 struct drm_fb_offset *offsets,
1396 int idx,
1397 int h_idx, int v_idx)
1398{
1399 struct drm_fb_helper_connector *fb_helper_conn;
1400 int i;
1401 int hoffset = 0, voffset = 0;
1402
1403 for (i = 0; i < fb_helper->connector_count; i++) {
1404 fb_helper_conn = fb_helper->connector_info[i];
1405 if (!fb_helper_conn->connector->has_tile)
1406 continue;
1407
1408 if (!modes[i] && (h_idx || v_idx)) {
1409 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1410 fb_helper_conn->connector->base.id);
1411 continue;
1412 }
1413 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1414 hoffset += modes[i]->hdisplay;
1415
1416 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1417 voffset += modes[i]->vdisplay;
1418 }
1419 offsets[idx].x = hoffset;
1420 offsets[idx].y = voffset;
1421 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1422 return 0;
1423}
1424
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001425static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001426 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001427 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001428 bool *enabled, int width, int height)
1429{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001430 struct drm_fb_helper_connector *fb_helper_conn;
1431 int i;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001432 uint64_t conn_configured = 0, mask;
1433 int tile_pass = 0;
1434 mask = (1 << fb_helper->connector_count) - 1;
1435retry:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001436 for (i = 0; i < fb_helper->connector_count; i++) {
1437 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001438
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001439 if (conn_configured & (1 << i))
Dave Airlie38651672010-03-30 05:34:13 +00001440 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001441
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001442 if (enabled[i] == false) {
1443 conn_configured |= (1 << i);
1444 continue;
1445 }
1446
1447 /* first pass over all the untiled connectors */
1448 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1449 continue;
1450
1451 if (tile_pass == 1) {
1452 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1453 fb_helper_conn->connector->tile_v_loc != 0)
1454 continue;
1455
1456 } else {
1457 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1458 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1459 /* if this tile_pass doesn't cover any of the tiles - keep going */
1460 continue;
1461
1462 /* find the tile offsets for this pass - need
1463 to find all tiles left and above */
1464 drm_get_tile_offsets(fb_helper, modes, offsets,
1465 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1466 }
Dave Airlie38651672010-03-30 05:34:13 +00001467 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001468 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001469
1470 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001471 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001472 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001473 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1474 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 +00001475 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001476 }
1477 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001478 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1479 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001480 break;
1481 }
1482 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1483 "none");
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001484 conn_configured |= (1 << i);
1485 }
1486
1487 if ((conn_configured & mask) != mask) {
1488 tile_pass++;
1489 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00001490 }
1491 return true;
1492}
1493
Dave Airlie8be48d92010-03-30 05:34:14 +00001494static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1495 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001496 struct drm_display_mode **modes,
1497 int n, int width, int height)
1498{
1499 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001500 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001501 struct drm_connector *connector;
1502 struct drm_connector_helper_funcs *connector_funcs;
1503 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001504 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001505 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001506 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001507
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001508 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001509 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001510
1511 fb_helper_conn = fb_helper->connector_info[n];
1512 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001513
1514 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001515 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001516 if (modes[n] == NULL)
1517 return best_score;
1518
Dave Airlie8be48d92010-03-30 05:34:14 +00001519 crtcs = kzalloc(dev->mode_config.num_connector *
1520 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001521 if (!crtcs)
1522 return best_score;
1523
1524 my_score = 1;
1525 if (connector->status == connector_status_connected)
1526 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001527 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001528 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001529 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001530 my_score++;
1531
1532 connector_funcs = connector->helper_private;
1533 encoder = connector_funcs->best_encoder(connector);
1534 if (!encoder)
1535 goto out;
1536
Dave Airlie38651672010-03-30 05:34:13 +00001537 /* select a crtc for this connector and then attempt to configure
1538 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001539 for (c = 0; c < fb_helper->crtc_count; c++) {
1540 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001541
Sachin Kamat96081cd2012-11-15 03:43:30 +00001542 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001543 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001544
1545 for (o = 0; o < n; o++)
1546 if (best_crtcs[o] == crtc)
1547 break;
1548
1549 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001550 /* ignore cloning unless only a single crtc */
1551 if (fb_helper->crtc_count > 1)
1552 continue;
1553
1554 if (!drm_mode_equal(modes[o], modes[n]))
1555 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001556 }
1557
1558 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001559 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1560 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001561 width, height);
1562 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001563 best_score = score;
1564 memcpy(best_crtcs, crtcs,
1565 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001566 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001567 }
Dave Airlie38651672010-03-30 05:34:13 +00001568 }
1569out:
1570 kfree(crtcs);
1571 return best_score;
1572}
1573
Dave Airlie8be48d92010-03-30 05:34:14 +00001574static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001575{
Dave Airlie8be48d92010-03-30 05:34:14 +00001576 struct drm_device *dev = fb_helper->dev;
1577 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001578 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001579 struct drm_fb_offset *offsets;
Dave Airlie8be48d92010-03-30 05:34:14 +00001580 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001581 bool *enabled;
1582 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001583 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001584
1585 DRM_DEBUG_KMS("\n");
1586
1587 width = dev->mode_config.max_width;
1588 height = dev->mode_config.max_height;
1589
Dave Airlie38651672010-03-30 05:34:13 +00001590 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001591 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001592 modes = kcalloc(dev->mode_config.num_connector,
1593 sizeof(struct drm_display_mode *), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001594 offsets = kcalloc(dev->mode_config.num_connector,
1595 sizeof(struct drm_fb_offset), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001596 enabled = kcalloc(dev->mode_config.num_connector,
1597 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001598 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001599 DRM_ERROR("Memory allocation failed\n");
1600 goto out;
1601 }
1602
Dave Airlie38651672010-03-30 05:34:13 +00001603
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001604 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001605
Jesse Barnes11e17a02013-02-19 13:31:39 -08001606 if (!(fb_helper->funcs->initial_config &&
1607 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001608 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08001609 enabled, width, height))) {
1610 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1611 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001612 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08001613
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001614 if (!drm_target_cloned(fb_helper, modes, offsets,
1615 enabled, width, height) &&
1616 !drm_target_preferred(fb_helper, modes, offsets,
1617 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001618 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001619
1620 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1621 width, height);
1622
1623 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001624 }
Dave Airlie38651672010-03-30 05:34:13 +00001625
Dave Airlie8be48d92010-03-30 05:34:14 +00001626 /* need to set the modesets up here for use later */
1627 /* fill out the connector<->crtc mappings into the modesets */
1628 for (i = 0; i < fb_helper->crtc_count; i++) {
1629 modeset = &fb_helper->crtc_info[i].mode_set;
1630 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001631 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001632 }
Dave Airlie38651672010-03-30 05:34:13 +00001633
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001634 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001635 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001636 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001637 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001638 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001639
Dave Airlie8be48d92010-03-30 05:34:14 +00001640 if (mode && fb_crtc) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001641 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1642 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Dave Airlie8be48d92010-03-30 05:34:14 +00001643 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001644 fb_crtc->x = offset->x;
1645 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001646 if (modeset->mode)
1647 drm_mode_destroy(dev, modeset->mode);
1648 modeset->mode = drm_mode_duplicate(dev,
1649 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001650 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001651 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001652 modeset->x = offset->x;
1653 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00001654 }
Dave Airlie38651672010-03-30 05:34:13 +00001655 }
1656
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001657 /* Clear out any old modes if there are no more connected outputs. */
1658 for (i = 0; i < fb_helper->crtc_count; i++) {
1659 modeset = &fb_helper->crtc_info[i].mode_set;
1660 if (modeset->num_connectors == 0) {
1661 BUG_ON(modeset->fb);
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001662 if (modeset->mode)
1663 drm_mode_destroy(dev, modeset->mode);
1664 modeset->mode = NULL;
1665 }
1666 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001667out:
Dave Airlie38651672010-03-30 05:34:13 +00001668 kfree(crtcs);
1669 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001670 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00001671 kfree(enabled);
1672}
1673
1674/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001675 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001676 * @fb_helper: fb_helper device struct
1677 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001678 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001679 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001680 * At the moment, this is a cloned configuration across all heads with
1681 * a new framebuffer object as the backing store.
1682 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001683 * Note that this also registers the fbdev and so allows userspace to call into
1684 * the driver through the fbdev interfaces.
1685 *
1686 * This function will call down into the ->fb_probe callback to let
1687 * the driver allocate and initialize the fbdev info structure and the drm
1688 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1689 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1690 * values for the fbdev info structure.
1691 *
Dave Airlie38651672010-03-30 05:34:13 +00001692 * RETURNS:
1693 * Zero if everything went ok, nonzero otherwise.
1694 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001695bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001696{
Dave Airlie8be48d92010-03-30 05:34:14 +00001697 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001698 int count = 0;
1699
Daniel Vetter53f19042014-03-20 14:26:35 +01001700 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001701 count = drm_fb_helper_probe_connector_modes(fb_helper,
1702 dev->mode_config.max_width,
1703 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001704 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001705 /*
1706 * we shouldn't end up with no modes here.
1707 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001708 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001709 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001710
Dave Airlie8be48d92010-03-30 05:34:14 +00001711 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001712
Dave Airlie4abe3522010-03-30 05:34:18 +00001713 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001714}
Dave Airlie8be48d92010-03-30 05:34:14 +00001715EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001716
Chris Wilson73943712011-04-22 11:03:57 +01001717/**
1718 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001719 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001720 * @fb_helper: the drm_fb_helper
1721 *
Chris Wilson73943712011-04-22 11:03:57 +01001722 * Scan the connectors attached to the fb_helper and try to put together a
1723 * setup after *notification of a change in output configuration.
1724 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001725 * Called at runtime, takes the mode config locks to be able to check/change the
1726 * modeset configuration. Must be run from process context (which usually means
1727 * either the output polling work or a work item launched from the driver's
1728 * hotplug interrupt).
1729 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001730 * Note that drivers may call this even before calling
1731 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1732 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1733 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001734 *
Chris Wilson73943712011-04-22 11:03:57 +01001735 * RETURNS:
1736 * 0 on success and a non-zero error code otherwise.
1737 */
1738int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001739{
Chris Wilson73943712011-04-22 11:03:57 +01001740 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001741 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001742
Daniel Vetter89ced122013-04-11 14:26:55 +00001743 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001744 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001745 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001746 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001747 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001748 }
Dave Airlie38651672010-03-30 05:34:13 +00001749 DRM_DEBUG_KMS("\n");
1750
Dave Airlie4abe3522010-03-30 05:34:18 +00001751 max_width = fb_helper->fb->width;
1752 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001753
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001754 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00001755 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1756
1757 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001758 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001759 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01001760 drm_fb_helper_set_par(fb_helper->fbdev);
1761
1762 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00001763}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001764EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001765
David Rientjes6a108a12011-01-20 14:44:16 -08001766/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001767 * but the module doesn't depend on any fb console symbols. At least
1768 * attempt to load fbcon to avoid leaving the system without a usable console.
1769 */
David Rientjes6a108a12011-01-20 14:44:16 -08001770#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001771static int __init drm_fb_helper_modinit(void)
1772{
1773 const char *name = "fbcon";
1774 struct module *fbcon;
1775
1776 mutex_lock(&module_mutex);
1777 fbcon = find_module(name);
1778 mutex_unlock(&module_mutex);
1779
1780 if (!fbcon)
1781 request_module_nowait(name);
1782 return 0;
1783}
1784
1785module_init(drm_fb_helper_modinit);
1786#endif