Merge tag 'dm-3.19-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/device...
[linux-2.6-block.git] / drivers / gpu / drm / drm_fb_helper.c
CommitLineData
785b93ef
DA
1/*
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 */
d56b1b9d
SK
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
3b40a443 32#include <linux/kernel.h>
785b93ef 33#include <linux/sysrq.h>
5a0e3ad6 34#include <linux/slab.h>
785b93ef 35#include <linux/fb.h>
e0cd3608 36#include <linux/module.h>
760285e7
DH
37#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_crtc_helper.h>
785b93ef
DA
41
42static LIST_HEAD(kernel_fb_helper_list);
43
d0ddc033
DV
44/**
45 * DOC: fbdev helpers
46 *
47 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
83c617c5 48 * mode setting driver. They can be used mostly independently from the crtc
d0ddc033
DV
49 * helper functions used by many drivers to implement the kernel mode setting
50 * interfaces.
207fd329 51 *
10a23102
TR
52 * 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().
207fd329
DV
57 *
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
83c617c5 63 * code provides a ->output_poll_changed callback.
207fd329
DV
64 *
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
10a23102
TR
67 *
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().
d0ddc033
DV
80 */
81
207fd329
DV
82/**
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 */
0b4c0f3f 95int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
d50ba256 96{
0b4c0f3f
DA
97 struct drm_device *dev = fb_helper->dev;
98 struct drm_connector *connector;
99 int i;
d50ba256 100
0b4c0f3f
DA
101 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;
d50ba256 107
0b4c0f3f
DA
108 fb_helper_connector->connector = connector;
109 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
110 }
d50ba256 111 return 0;
0b4c0f3f
DA
112fail:
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;
d50ba256 119}
0b4c0f3f 120EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
d50ba256 121
65c2a89c
DA
122int 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) {
14f476fa 129 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL);
65c2a89c
DA
130 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
99231028
JW
174static 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
04c0c569
VS
179 if (helper->funcs->gamma_get == NULL)
180 return;
181
99231028
JW
182 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
ebe0f244
LP
194 if (crtc->funcs->gamma_set == NULL)
195 return;
196
99231028
JW
197 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
207fd329
DV
204/**
205 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
206 * @info: fbdev registered by the helper
207 */
1a7aba7f
JB
208int 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
1a7aba7f
JB
214 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;
99231028 223 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
1a7aba7f
JB
224 funcs->mode_set_base_atomic(mode_set->crtc,
225 mode_set->fb,
226 mode_set->x,
413d45d3 227 mode_set->y,
21c74a8e 228 ENTER_ATOMIC_MODE_SET);
1a7aba7f
JB
229 }
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)
f4510a27 244 return c->primary->fb;
1a7aba7f
JB
245 }
246
247 return NULL;
248}
249
207fd329
DV
250/**
251 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
252 * @info: fbdev registered by the helper
253 */
1a7aba7f
JB
254int 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
99231028 276 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
1a7aba7f 277 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
21c74a8e 278 crtc->y, LEAVE_ATOMIC_MODE_SET);
1a7aba7f
JB
279 }
280
281 return 0;
282}
283EXPORT_SYMBOL(drm_fb_helper_debug_leave);
284
5ea1f752 285static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
e8e7a2b8 286{
3858bc5d
VS
287 struct drm_device *dev = fb_helper->dev;
288 struct drm_plane *plane;
e8e7a2b8 289 bool error = false;
3858bc5d
VS
290 int i;
291
292 drm_warn_on_modeset_not_all_locked(dev);
6aed8ec3 293
9783de20 294 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
e27dde3e
MR
295 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
296 drm_plane_force_disable(plane);
6aed8ec3 297
9783de20 298 if (dev->mode_config.rotation_property) {
3a5f87c2
TW
299 drm_mode_plane_set_obj_prop(plane,
300 dev->mode_config.rotation_property,
301 BIT(DRM_ROTATE_0));
9783de20
SJ
302 }
303 }
304
e8e7a2b8
DA
305 for (i = 0; i < fb_helper->crtc_count; i++) {
306 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
3858bc5d
VS
307 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
2d13b679 316 ret = drm_mode_set_config_internal(mode_set);
e8e7a2b8
DA
317 if (ret)
318 error = true;
319 }
320 return error;
321}
5ea1f752
RC
322/**
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;
e2809c7d
DA
350 bool do_delayed = false;
351
5ea1f752
RC
352 drm_modeset_lock_all(dev);
353 ret = restore_fbdev_mode(fb_helper);
e2809c7d
DA
354
355 do_delayed = fb_helper->delayed_hotplug;
356 if (do_delayed)
357 fb_helper->delayed_hotplug = false;
5ea1f752 358 drm_modeset_unlock_all(dev);
e2809c7d
DA
359
360 if (do_delayed)
361 drm_fb_helper_hotplug_event(fb_helper);
5ea1f752
RC
362 return ret;
363}
364EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
e8e7a2b8 365
d21bf469
DV
366/*
367 * restore fbcon display for all kms driver's using this helper, used for sysrq
368 * and panic handling.
369 */
78b9c353 370static bool drm_fb_helper_force_kernel_mode(void)
785b93ef 371{
785b93ef
DA
372 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) {
b77f0765
TR
379 struct drm_device *dev = helper->dev;
380
381 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
382 continue;
383
cb597bb3
DV
384 /*
385 * NOTE: Use trylock mode to avoid deadlocks and sleeping in
386 * panic context.
51fd371b 387 */
cb597bb3 388 if (__drm_modeset_lock_all(dev, true) != 0) {
b77f0765 389 error = true;
e8e7a2b8 390 continue;
b77f0765 391 }
e8e7a2b8
DA
392
393 ret = drm_fb_helper_restore_fbdev_mode(helper);
394 if (ret)
395 error = true;
b77f0765 396
cb597bb3 397 drm_modeset_unlock_all(dev);
785b93ef
DA
398 }
399 return error;
400}
401
43c8a849 402static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
785b93ef
DA
403 void *panic_str)
404{
d68752cf
HD
405 /*
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
d56b1b9d 412 pr_err("panic occurred, switching back to text console\n");
785b93ef 413 return drm_fb_helper_force_kernel_mode();
785b93ef 414}
785b93ef
DA
415
416static struct notifier_block paniced = {
417 .notifier_call = drm_fb_helper_panic,
418};
419
20c60c35
DV
420static 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
520edd13
PZ
426 /* 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
20c60c35 431 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
f4510a27 432 if (crtc->primary->fb)
20c60c35 433 crtcs_bound++;
f4510a27 434 if (crtc->primary->fb == fb_helper->fb)
20c60c35
DV
435 bound++;
436 }
437
438 if (bound < crtcs_bound)
439 return false;
520edd13 440
20c60c35
DV
441 return true;
442}
443
bea1d35b 444#ifdef CONFIG_MAGIC_SYSRQ
785b93ef
DA
445static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
446{
d21bf469
DV
447 bool ret;
448 ret = drm_fb_helper_force_kernel_mode();
449 if (ret == true)
450 DRM_ERROR("Failed to restore crtc configuration\n");
785b93ef
DA
451}
452static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
453
1495cc9d 454static void drm_fb_helper_sysrq(int dummy1)
785b93ef
DA
455{
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};
b8c40d62
RD
464#else
465static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
bea1d35b 466#endif
785b93ef 467
3a8148c5 468static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
785b93ef
DA
469{
470 struct drm_fb_helper *fb_helper = info->par;
471 struct drm_device *dev = fb_helper->dev;
472 struct drm_crtc *crtc;
023eb571 473 struct drm_connector *connector;
023eb571 474 int i, j;
785b93ef 475
928c2f0c
DV
476 /*
477 * fbdev->blank can be called from irq context in case of a panic.
1b1d5397
DV
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
785b93ef 484 /*
3a8148c5 485 * For each CRTC in this fb, turn the connectors on/off.
785b93ef 486 */
84849903 487 drm_modeset_lock_all(dev);
20c60c35
DV
488 if (!drm_fb_helper_is_bound(fb_helper)) {
489 drm_modeset_unlock_all(dev);
490 return;
491 }
492
e87b2c42 493 for (i = 0; i < fb_helper->crtc_count; i++) {
8be48d92 494 crtc = fb_helper->crtc_info[i].mode_set.crtc;
785b93ef 495
8be48d92
DA
496 if (!crtc->enabled)
497 continue;
498
3a8148c5 499 /* Walk the connectors & encoders on this fb turning them on/off */
023eb571
JB
500 for (j = 0; j < fb_helper->connector_count; j++) {
501 connector = fb_helper->connector_info[j]->connector;
e04190e0 502 connector->funcs->dpms(connector, dpms_mode);
58495563 503 drm_object_property_set_value(&connector->base,
3a8148c5 504 dev->mode_config.dpms_property, dpms_mode);
785b93ef 505 }
785b93ef 506 }
84849903 507 drm_modeset_unlock_all(dev);
785b93ef
DA
508}
509
207fd329
DV
510/**
511 * drm_fb_helper_blank - implementation for ->fb_blank
512 * @blank: desired blanking state
513 * @info: fbdev registered by the helper
514 */
785b93ef
DA
515int drm_fb_helper_blank(int blank, struct fb_info *info)
516{
517 switch (blank) {
731b5a15 518 /* Display: On; HSync: On, VSync: On */
785b93ef 519 case FB_BLANK_UNBLANK:
3a8148c5 520 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
785b93ef 521 break;
731b5a15 522 /* Display: Off; HSync: On, VSync: On */
785b93ef 523 case FB_BLANK_NORMAL:
3a8148c5 524 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
785b93ef 525 break;
731b5a15 526 /* Display: Off; HSync: Off, VSync: On */
785b93ef 527 case FB_BLANK_HSYNC_SUSPEND:
3a8148c5 528 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
785b93ef 529 break;
731b5a15 530 /* Display: Off; HSync: On, VSync: Off */
785b93ef 531 case FB_BLANK_VSYNC_SUSPEND:
3a8148c5 532 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
785b93ef 533 break;
731b5a15 534 /* Display: Off; HSync: Off, VSync: Off */
785b93ef 535 case FB_BLANK_POWERDOWN:
3a8148c5 536 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
785b93ef
DA
537 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
0b4c0f3f
DA
547 for (i = 0; i < helper->connector_count; i++)
548 kfree(helper->connector_info[i]);
549 kfree(helper->connector_info);
a1b7736d 550 for (i = 0; i < helper->crtc_count; i++) {
785b93ef 551 kfree(helper->crtc_info[i].mode_set.connectors);
a1b7736d
SH
552 if (helper->crtc_info[i].mode_set.mode)
553 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
554 }
785b93ef
DA
555 kfree(helper->crtc_info);
556}
557
10a23102
TR
558/**
559 * 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
207fd329
DV
576/**
577 * 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 *
10a23102 588 * Drivers must call drm_fb_helper_prepare() before calling this function.
207fd329
DV
589 *
590 * RETURNS:
591 * Zero if everything went ok, nonzero otherwise.
592 */
4abe3520
DA
593int drm_fb_helper_init(struct drm_device *dev,
594 struct drm_fb_helper *fb_helper,
eb1f8e4f 595 int crtc_count, int max_conn_count)
785b93ef 596{
785b93ef 597 struct drm_crtc *crtc;
785b93ef
DA
598 int i;
599
04cfe97e
XL
600 if (!max_conn_count)
601 return -EINVAL;
602
4abe3520
DA
603 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
604 if (!fb_helper->crtc_info)
785b93ef
DA
605 return -ENOMEM;
606
4abe3520
DA
607 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);
0b4c0f3f
DA
611 return -ENOMEM;
612 }
65c2a89c 613 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
4abe3520 614 fb_helper->connector_count = 0;
785b93ef
DA
615
616 for (i = 0; i < crtc_count; i++) {
4abe3520 617 fb_helper->crtc_info[i].mode_set.connectors =
785b93ef
DA
618 kcalloc(max_conn_count,
619 sizeof(struct drm_connector *),
620 GFP_KERNEL);
621
4a1b0714 622 if (!fb_helper->crtc_info[i].mode_set.connectors)
785b93ef 623 goto out_free;
4abe3520 624 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
785b93ef
DA
625 }
626
627 i = 0;
628 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4abe3520 629 fb_helper->crtc_info[i].mode_set.crtc = crtc;
785b93ef
DA
630 i++;
631 }
e9ad3181 632
785b93ef
DA
633 return 0;
634out_free:
4abe3520 635 drm_fb_helper_crtc_free(fb_helper);
785b93ef
DA
636 return -ENOMEM;
637}
4abe3520
DA
638EXPORT_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)) {
d56b1b9d 645 pr_info("drm: unregistered panic notifier\n");
4abe3520
DA
646 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
4abe3520
DA
654}
655EXPORT_SYMBOL(drm_fb_helper_fini);
785b93ef 656
c850cb78 657static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
b8c00ac5
DA
658 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
c850cb78
DA
664 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);
9da12b6a
RC
677 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 }
c850cb78
DA
682 palette[regno] = value;
683 return 0;
684 }
685
04c0c569
VS
686 /*
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
b8c00ac5
DA
694 pindex = regno;
695
696 if (fb->bits_per_pixel == 16) {
697 pindex = regno << 3;
698
699 if (fb->depth == 16 && regno > 63)
c850cb78 700 return -EINVAL;
b8c00ac5 701 if (fb->depth == 15 && regno > 31)
c850cb78 702 return -EINVAL;
b8c00ac5
DA
703
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);
c850cb78 726 return 0;
b8c00ac5
DA
727}
728
207fd329
DV
729/**
730 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
731 * @cmap: cmap to set
732 * @info: fbdev registered by the helper
733 */
068143d3
DA
734int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
735{
736 struct drm_fb_helper *fb_helper = info->par;
8391a3d5 737 struct drm_device *dev = fb_helper->dev;
8be48d92 738 struct drm_crtc_helper_funcs *crtc_funcs;
068143d3
DA
739 u16 *red, *green, *blue, *transp;
740 struct drm_crtc *crtc;
062ac622 741 int i, j, rc = 0;
068143d3
DA
742 int start;
743
9aa609e1
RW
744 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
745 return -EBUSY;
746 }
8391a3d5
VS
747 if (!drm_fb_helper_is_bound(fb_helper)) {
748 drm_modeset_unlock_all(dev);
749 return -EBUSY;
750 }
751
8be48d92
DA
752 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;
068143d3
DA
755
756 red = cmap->red;
757 green = cmap->green;
758 blue = cmap->blue;
759 transp = cmap->transp;
760 start = cmap->start;
761
062ac622 762 for (j = 0; j < cmap->len; j++) {
068143d3
DA
763 u16 hred, hgreen, hblue, htransp = 0xffff;
764
765 hred = *red++;
766 hgreen = *green++;
767 hblue = *blue++;
768
769 if (transp)
770 htransp = *transp++;
771
c850cb78
DA
772 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
773 if (rc)
8391a3d5 774 goto out;
068143d3 775 }
04c0c569
VS
776 if (crtc_funcs->load_lut)
777 crtc_funcs->load_lut(crtc);
068143d3 778 }
8391a3d5
VS
779 out:
780 drm_modeset_unlock_all(dev);
068143d3
DA
781 return rc;
782}
783EXPORT_SYMBOL(drm_fb_helper_setcmap);
784
207fd329
DV
785/**
786 * drm_fb_helper_check_var - implementation for ->fb_check_var
787 * @var: screeninfo to check
788 * @info: fbdev registered by the helper
789 */
785b93ef
DA
790int 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
f90ebd9e 797 if (var->pixclock != 0 || in_dbg_master())
785b93ef
DA
798 return -EINVAL;
799
800 /* Need to resize the fb object !!! */
62fb376e
CW
801 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) {
509c7d83 804 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
62fb376e
CW
805 "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,
509c7d83 808 fb->width, fb->height, fb->bits_per_pixel);
785b93ef
DA
809 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
207fd329
DV
882/**
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 */
785b93ef
DA
890int drm_fb_helper_set_par(struct fb_info *info)
891{
892 struct drm_fb_helper *fb_helper = info->par;
785b93ef 893 struct fb_var_screeninfo *var = &info->var;
785b93ef 894
5349ef31 895 if (var->pixclock != 0) {
172e91f5 896 DRM_ERROR("PIXEL CLOCK SET\n");
785b93ef
DA
897 return -EINVAL;
898 }
899
5ea1f752 900 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
4abe3520 901
785b93ef
DA
902 return 0;
903}
904EXPORT_SYMBOL(drm_fb_helper_set_par);
905
207fd329
DV
906/**
907 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
908 * @var: updated screen information
909 * @info: fbdev registered by the helper
910 */
785b93ef
DA
911int 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;
785b93ef
DA
917 int ret = 0;
918 int i;
919
9aa609e1
RW
920 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
921 return -EBUSY;
922 }
20c60c35
DV
923 if (!drm_fb_helper_is_bound(fb_helper)) {
924 drm_modeset_unlock_all(dev);
925 return -EBUSY;
926 }
927
8be48d92 928 for (i = 0; i < fb_helper->crtc_count; i++) {
785b93ef
DA
929 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) {
2d13b679 935 ret = drm_mode_set_config_internal(modeset);
785b93ef
DA
936 if (!ret) {
937 info->var.xoffset = var->xoffset;
938 info->var.yoffset = var->yoffset;
939 }
940 }
941 }
84849903 942 drm_modeset_unlock_all(dev);
785b93ef
DA
943 return ret;
944}
945EXPORT_SYMBOL(drm_fb_helper_pan_display);
946
8acf658a 947/*
207fd329
DV
948 * 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.
8acf658a 951 */
de1ace5b
DV
952static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
953 int preferred_bpp)
785b93ef 954{
8acf658a 955 int ret = 0;
785b93ef 956 int crtc_count = 0;
4abe3520 957 int i;
785b93ef 958 struct fb_info *info;
38651674 959 struct drm_fb_helper_surface_size sizes;
8be48d92 960 int gamma_size = 0;
38651674
DA
961
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;
785b93ef 967
b8c00ac5
DA
968 /* if driver picks 8 or 16 by default use that
969 for both depth/bpp */
96081cdf 970 if (preferred_bpp != sizes.surface_bpp)
38651674 971 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
96081cdf 972
785b93ef 973 /* first up get a count of crtcs now in use and new min/maxes width/heights */
0b4c0f3f
DA
974 for (i = 0; i < fb_helper->connector_count; i++) {
975 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1794d257 976 struct drm_cmdline_mode *cmdline_mode;
8ef8678c 977
eaf99c74 978 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
d50ba256
DA
979
980 if (cmdline_mode->bpp_specified) {
981 switch (cmdline_mode->bpp) {
982 case 8:
38651674 983 sizes.surface_depth = sizes.surface_bpp = 8;
d50ba256
DA
984 break;
985 case 15:
38651674
DA
986 sizes.surface_depth = 15;
987 sizes.surface_bpp = 16;
d50ba256
DA
988 break;
989 case 16:
38651674 990 sizes.surface_depth = sizes.surface_bpp = 16;
d50ba256
DA
991 break;
992 case 24:
38651674 993 sizes.surface_depth = sizes.surface_bpp = 24;
d50ba256
DA
994 break;
995 case 32:
38651674
DA
996 sizes.surface_depth = 24;
997 sizes.surface_bpp = 32;
d50ba256
DA
998 break;
999 }
1000 break;
1001 }
1002 }
1003
8be48d92
DA
1004 crtc_count = 0;
1005 for (i = 0; i < fb_helper->crtc_count; i++) {
1006 struct drm_display_mode *desired_mode;
b0ee9e7f 1007 int x, y;
8be48d92 1008 desired_mode = fb_helper->crtc_info[i].desired_mode;
b0ee9e7f
DA
1009 x = fb_helper->crtc_info[i].x;
1010 y = fb_helper->crtc_info[i].y;
8be48d92
DA
1011 if (desired_mode) {
1012 if (gamma_size == 0)
1013 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
b0ee9e7f
DA
1014 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;
785b93ef
DA
1022 crtc_count++;
1023 }
1024 }
1025
38651674 1026 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
785b93ef
DA
1027 /* hmm everyone went away - assume VGA cable just fell out
1028 and will come back later. */
eb1f8e4f 1029 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
19b4b445
DA
1030 sizes.fb_width = sizes.surface_width = 1024;
1031 sizes.fb_height = sizes.surface_height = 768;
785b93ef
DA
1032 }
1033
38651674 1034 /* push down into drivers */
8acf658a
DV
1035 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1036 if (ret < 0)
1037 return ret;
785b93ef 1038
38651674 1039 info = fb_helper->fbdev;
785b93ef 1040
7e53f3a4
DV
1041 /*
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 */
96081cdf 1048 for (i = 0; i < fb_helper->crtc_count; i++)
7e53f3a4
DV
1049 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1050 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1051
785b93ef 1052
8acf658a
DV
1053 info->var.pixclock = 0;
1054 if (register_framebuffer(info) < 0)
1055 return -EINVAL;
38651674 1056
8acf658a
DV
1057 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1058 info->node, info->fix.id);
785b93ef
DA
1059
1060 /* Switch back to kernel console on panic */
1061 /* multi card linked list maybe */
1062 if (list_empty(&kernel_fb_helper_list)) {
d56b1b9d 1063 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
785b93ef
DA
1064 atomic_notifier_chain_register(&panic_notifier_list,
1065 &paniced);
1066 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1067 }
8acf658a
DV
1068
1069 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
38651674 1070
785b93ef
DA
1071 return 0;
1072}
785b93ef 1073
207fd329
DV
1074/**
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 */
3632ef89
DA
1087void 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;
3632ef89
DA
1100
1101 info->fix.line_length = pitch;
1102 return;
1103}
1104EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1105
207fd329
DV
1106/**
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 */
38651674 1120void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
785b93ef
DA
1121 uint32_t fb_width, uint32_t fb_height)
1122{
38651674
DA
1123 struct drm_framebuffer *fb = fb_helper->fb;
1124 info->pseudo_palette = fb_helper->pseudo_palette;
785b93ef
DA
1125 info->var.xres_virtual = fb->width;
1126 info->var.yres_virtual = fb->height;
1127 info->var.bits_per_pixel = fb->bits_per_pixel;
57084d05 1128 info->var.accel_flags = FB_ACCELF_TEXT;
785b93ef
DA
1129 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);
38651674 1193
0b4c0f3f
DA
1194static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1195 uint32_t maxX,
1196 uint32_t maxY)
38651674
DA
1197{
1198 struct drm_connector *connector;
1199 int count = 0;
0b4c0f3f 1200 int i;
38651674 1201
0b4c0f3f
DA
1202 for (i = 0; i < fb_helper->connector_count; i++) {
1203 connector = fb_helper->connector_info[i]->connector;
38651674
DA
1204 count += connector->funcs->fill_modes(connector, maxX, maxY);
1205 }
1206
1207 return count;
1208}
1209
2f1046f3 1210struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
38651674
DA
1211{
1212 struct drm_display_mode *mode;
1213
0b4c0f3f 1214 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
9d3de138
DV
1215 if (mode->hdisplay > width ||
1216 mode->vdisplay > height)
38651674
DA
1217 continue;
1218 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1219 return mode;
1220 }
1221 return NULL;
1222}
2f1046f3 1223EXPORT_SYMBOL(drm_has_preferred_mode);
38651674 1224
0b4c0f3f 1225static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
38651674 1226{
eaf99c74 1227 return fb_connector->connector->cmdline_mode.specified;
38651674
DA
1228}
1229
2f1046f3 1230struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
0b4c0f3f 1231 int width, int height)
38651674 1232{
1794d257 1233 struct drm_cmdline_mode *cmdline_mode;
38651674 1234 struct drm_display_mode *mode = NULL;
c683f427 1235 bool prefer_non_interlace;
38651674 1236
eaf99c74 1237 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
38651674
DA
1238 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
c683f427
TI
1247 prefer_non_interlace = !cmdline_mode->interlace;
1248 again:
0b4c0f3f 1249 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
38651674
DA
1250 /* 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;
c683f427
TI
1263 } else if (prefer_non_interlace) {
1264 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1265 continue;
38651674
DA
1266 }
1267 return mode;
1268 }
1269
c683f427
TI
1270 if (prefer_non_interlace) {
1271 prefer_non_interlace = false;
1272 goto again;
1273 }
1274
38651674 1275create_mode:
1794d257
CW
1276 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1277 cmdline_mode);
0b4c0f3f 1278 list_add(&mode->head, &fb_helper_conn->connector->modes);
38651674
DA
1279 return mode;
1280}
2f1046f3 1281EXPORT_SYMBOL(drm_pick_cmdline_mode);
38651674
DA
1282
1283static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1284{
1285 bool enable;
1286
96081cdf 1287 if (strict)
38651674 1288 enable = connector->status == connector_status_connected;
96081cdf 1289 else
38651674 1290 enable = connector->status != connector_status_disconnected;
96081cdf 1291
38651674
DA
1292 return enable;
1293}
1294
0b4c0f3f
DA
1295static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1296 bool *enabled)
38651674
DA
1297{
1298 bool any_enabled = false;
1299 struct drm_connector *connector;
1300 int i = 0;
1301
0b4c0f3f
DA
1302 for (i = 0; i < fb_helper->connector_count; i++) {
1303 connector = fb_helper->connector_info[i]->connector;
38651674
DA
1304 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];
38651674
DA
1308 }
1309
1310 if (any_enabled)
1311 return;
1312
0b4c0f3f
DA
1313 for (i = 0; i < fb_helper->connector_count; i++) {
1314 connector = fb_helper->connector_info[i]->connector;
38651674 1315 enabled[i] = drm_connector_enabled(connector, false);
38651674
DA
1316 }
1317}
1318
1d42bbc8
DA
1319static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1320 struct drm_display_mode **modes,
b0ee9e7f 1321 struct drm_fb_offset *offsets,
1d42bbc8
DA
1322 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;
f6e252ba 1369 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1d42bbc8
DA
1370
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
b0ee9e7f
DA
1393static 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
0b4c0f3f 1425static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
38651674 1426 struct drm_display_mode **modes,
b0ee9e7f 1427 struct drm_fb_offset *offsets,
38651674
DA
1428 bool *enabled, int width, int height)
1429{
0b4c0f3f
DA
1430 struct drm_fb_helper_connector *fb_helper_conn;
1431 int i;
b0ee9e7f
DA
1432 uint64_t conn_configured = 0, mask;
1433 int tile_pass = 0;
1434 mask = (1 << fb_helper->connector_count) - 1;
1435retry:
0b4c0f3f
DA
1436 for (i = 0; i < fb_helper->connector_count; i++) {
1437 fb_helper_conn = fb_helper->connector_info[i];
38651674 1438
b0ee9e7f
DA
1439 if (conn_configured & (1 << i))
1440 continue;
1441
1442 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)
38651674 1449 continue;
38651674 1450
b0ee9e7f
DA
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 }
38651674 1467 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
0b4c0f3f 1468 fb_helper_conn->connector->base.id);
38651674
DA
1469
1470 /* got for command line mode first */
0b4c0f3f 1471 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
38651674 1472 if (!modes[i]) {
b0ee9e7f
DA
1473 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);
0b4c0f3f 1475 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
38651674
DA
1476 }
1477 /* No preferred modes, pick one off the list */
0b4c0f3f
DA
1478 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1479 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
38651674
DA
1480 break;
1481 }
1482 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1483 "none");
b0ee9e7f
DA
1484 conn_configured |= (1 << i);
1485 }
1486
1487 if ((conn_configured & mask) != mask) {
1488 tile_pass++;
1489 goto retry;
38651674
DA
1490 }
1491 return true;
1492}
1493
8be48d92
DA
1494static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1495 struct drm_fb_helper_crtc **best_crtcs,
38651674
DA
1496 struct drm_display_mode **modes,
1497 int n, int width, int height)
1498{
1499 int c, o;
8be48d92 1500 struct drm_device *dev = fb_helper->dev;
38651674
DA
1501 struct drm_connector *connector;
1502 struct drm_connector_helper_funcs *connector_funcs;
1503 struct drm_encoder *encoder;
38651674 1504 int my_score, best_score, score;
8be48d92 1505 struct drm_fb_helper_crtc **crtcs, *crtc;
0b4c0f3f 1506 struct drm_fb_helper_connector *fb_helper_conn;
38651674 1507
0b4c0f3f 1508 if (n == fb_helper->connector_count)
38651674 1509 return 0;
0b4c0f3f
DA
1510
1511 fb_helper_conn = fb_helper->connector_info[n];
1512 connector = fb_helper_conn->connector;
38651674
DA
1513
1514 best_crtcs[n] = NULL;
8be48d92 1515 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
38651674
DA
1516 if (modes[n] == NULL)
1517 return best_score;
1518
8be48d92
DA
1519 crtcs = kzalloc(dev->mode_config.num_connector *
1520 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1521 if (!crtcs)
1522 return best_score;
1523
1524 my_score = 1;
1525 if (connector->status == connector_status_connected)
1526 my_score++;
0b4c0f3f 1527 if (drm_has_cmdline_mode(fb_helper_conn))
38651674 1528 my_score++;
0b4c0f3f 1529 if (drm_has_preferred_mode(fb_helper_conn, width, height))
38651674
DA
1530 my_score++;
1531
1532 connector_funcs = connector->helper_private;
1533 encoder = connector_funcs->best_encoder(connector);
1534 if (!encoder)
1535 goto out;
1536
38651674
DA
1537 /* select a crtc for this connector and then attempt to configure
1538 remaining connectors */
8be48d92
DA
1539 for (c = 0; c < fb_helper->crtc_count; c++) {
1540 crtc = &fb_helper->crtc_info[c];
38651674 1541
96081cdf 1542 if ((encoder->possible_crtcs & (1 << c)) == 0)
38651674 1543 continue;
38651674
DA
1544
1545 for (o = 0; o < n; o++)
1546 if (best_crtcs[o] == crtc)
1547 break;
1548
1549 if (o < n) {
1d42bbc8
DA
1550 /* 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;
38651674
DA
1556 }
1557
1558 crtcs[n] = crtc;
8be48d92
DA
1559 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1560 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
38651674
DA
1561 width, height);
1562 if (score > best_score) {
38651674
DA
1563 best_score = score;
1564 memcpy(best_crtcs, crtcs,
1565 dev->mode_config.num_connector *
8be48d92 1566 sizeof(struct drm_fb_helper_crtc *));
38651674 1567 }
38651674
DA
1568 }
1569out:
1570 kfree(crtcs);
1571 return best_score;
1572}
1573
8be48d92 1574static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
38651674 1575{
8be48d92
DA
1576 struct drm_device *dev = fb_helper->dev;
1577 struct drm_fb_helper_crtc **crtcs;
38651674 1578 struct drm_display_mode **modes;
b0ee9e7f 1579 struct drm_fb_offset *offsets;
8be48d92 1580 struct drm_mode_set *modeset;
38651674
DA
1581 bool *enabled;
1582 int width, height;
11e17a08 1583 int i;
38651674
DA
1584
1585 DRM_DEBUG_KMS("\n");
1586
1587 width = dev->mode_config.max_width;
1588 height = dev->mode_config.max_height;
1589
38651674 1590 crtcs = kcalloc(dev->mode_config.num_connector,
8be48d92 1591 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1592 modes = kcalloc(dev->mode_config.num_connector,
1593 sizeof(struct drm_display_mode *), GFP_KERNEL);
b0ee9e7f
DA
1594 offsets = kcalloc(dev->mode_config.num_connector,
1595 sizeof(struct drm_fb_offset), GFP_KERNEL);
38651674
DA
1596 enabled = kcalloc(dev->mode_config.num_connector,
1597 sizeof(bool), GFP_KERNEL);
b0ee9e7f 1598 if (!crtcs || !modes || !enabled || !offsets) {
8c5eaca0
SK
1599 DRM_ERROR("Memory allocation failed\n");
1600 goto out;
1601 }
1602
38651674 1603
0b4c0f3f 1604 drm_enable_connectors(fb_helper, enabled);
38651674 1605
11e17a08
JB
1606 if (!(fb_helper->funcs->initial_config &&
1607 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
b0ee9e7f 1608 offsets,
11e17a08
JB
1609 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]));
b0ee9e7f 1612 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
11e17a08 1613
b0ee9e7f
DA
1614 if (!drm_target_cloned(fb_helper, modes, offsets,
1615 enabled, width, height) &&
1616 !drm_target_preferred(fb_helper, modes, offsets,
1617 enabled, width, height))
1d42bbc8 1618 DRM_ERROR("Unable to find initial modes\n");
38651674 1619
11e17a08
JB
1620 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1621 width, height);
38651674 1622
11e17a08
JB
1623 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1624 }
8be48d92
DA
1625
1626 /* 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;
7e53f3a4 1631 modeset->fb = NULL;
8be48d92 1632 }
38651674 1633
0b4c0f3f 1634 for (i = 0; i < fb_helper->connector_count; i++) {
38651674 1635 struct drm_display_mode *mode = modes[i];
8be48d92 1636 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
b0ee9e7f 1637 struct drm_fb_offset *offset = &offsets[i];
8be48d92 1638 modeset = &fb_crtc->mode_set;
38651674 1639
8be48d92 1640 if (mode && fb_crtc) {
b0ee9e7f
DA
1641 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);
8be48d92 1643 fb_crtc->desired_mode = mode;
b0ee9e7f
DA
1644 fb_crtc->x = offset->x;
1645 fb_crtc->y = offset->y;
8be48d92
DA
1646 if (modeset->mode)
1647 drm_mode_destroy(dev, modeset->mode);
1648 modeset->mode = drm_mode_duplicate(dev,
1649 fb_crtc->desired_mode);
0b4c0f3f 1650 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
7e53f3a4 1651 modeset->fb = fb_helper->fb;
b0ee9e7f
DA
1652 modeset->x = offset->x;
1653 modeset->y = offset->y;
38651674 1654 }
38651674
DA
1655 }
1656
7e53f3a4
DV
1657 /* 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);
7e53f3a4
DV
1662 if (modeset->mode)
1663 drm_mode_destroy(dev, modeset->mode);
1664 modeset->mode = NULL;
1665 }
1666 }
8c5eaca0 1667out:
38651674
DA
1668 kfree(crtcs);
1669 kfree(modes);
b0ee9e7f 1670 kfree(offsets);
38651674
DA
1671 kfree(enabled);
1672}
1673
1674/**
207fd329 1675 * drm_fb_helper_initial_config - setup a sane initial connector configuration
d0ddc033
DV
1676 * @fb_helper: fb_helper device struct
1677 * @bpp_sel: bpp value to use for the framebuffer configuration
38651674 1678 *
d0ddc033 1679 * Scans the CRTCs and connectors and tries to put together an initial setup.
38651674
DA
1680 * At the moment, this is a cloned configuration across all heads with
1681 * a new framebuffer object as the backing store.
1682 *
207fd329
DV
1683 * 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 *
38651674
DA
1692 * RETURNS:
1693 * Zero if everything went ok, nonzero otherwise.
1694 */
4abe3520 1695bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
38651674 1696{
8be48d92 1697 struct drm_device *dev = fb_helper->dev;
38651674
DA
1698 int count = 0;
1699
53f1904b 1700 mutex_lock(&dev->mode_config.mutex);
0b4c0f3f
DA
1701 count = drm_fb_helper_probe_connector_modes(fb_helper,
1702 dev->mode_config.max_width,
1703 dev->mode_config.max_height);
53f1904b 1704 mutex_unlock(&dev->mode_config.mutex);
38651674
DA
1705 /*
1706 * we shouldn't end up with no modes here.
1707 */
96081cdf 1708 if (count == 0)
d56b1b9d 1709 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
96081cdf 1710
8be48d92 1711 drm_setup_crtcs(fb_helper);
38651674 1712
4abe3520 1713 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
38651674 1714}
8be48d92 1715EXPORT_SYMBOL(drm_fb_helper_initial_config);
38651674 1716
7394371d
CW
1717/**
1718 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
d0ddc033 1719 * probing all the outputs attached to the fb
7394371d
CW
1720 * @fb_helper: the drm_fb_helper
1721 *
7394371d
CW
1722 * 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 *
207fd329
DV
1725 * 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 *
50c3dc97
DV
1730 * 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.
207fd329 1734 *
7394371d
CW
1735 * 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)
38651674 1739{
7394371d 1740 struct drm_device *dev = fb_helper->dev;
51bbd276 1741 u32 max_width, max_height;
5c4426a7 1742
89ced125 1743 mutex_lock(&fb_helper->dev->mode_config.mutex);
50c3dc97 1744 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
4abe3520 1745 fb_helper->delayed_hotplug = true;
89ced125 1746 mutex_unlock(&fb_helper->dev->mode_config.mutex);
7394371d 1747 return 0;
4abe3520 1748 }
eb1f8e4f 1749 DRM_DEBUG_KMS("\n");
4abe3520 1750
eb1f8e4f
DA
1751 max_width = fb_helper->fb->width;
1752 max_height = fb_helper->fb->height;
5c4426a7 1753
51bbd276 1754 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
89ced125
DV
1755 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1756
1757 drm_modeset_lock_all(dev);
eb1f8e4f 1758 drm_setup_crtcs(fb_helper);
84849903 1759 drm_modeset_unlock_all(dev);
2180c3c7
DV
1760 drm_fb_helper_set_par(fb_helper->fbdev);
1761
1762 return 0;
5c4426a7 1763}
eb1f8e4f 1764EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
5c4426a7 1765
6a108a14 1766/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
3ce05168
DF
1767 * 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 */
6a108a14 1770#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
3ce05168
DF
1771static 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