]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/gpu/drm/drm_fb_helper.c
drm/doc: integrate crtc helper api into docbook
[mirror_ubuntu-zesty-kernel.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 41
6fcefd56
DA
42MODULE_AUTHOR("David Airlie, Jesse Barnes");
43MODULE_DESCRIPTION("DRM KMS helper");
44MODULE_LICENSE("GPL and additional rights");
45
785b93ef
DA
46static LIST_HEAD(kernel_fb_helper_list);
47
0b4c0f3f
DA
48/* simple single crtc case helper function */
49int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
d50ba256 50{
0b4c0f3f
DA
51 struct drm_device *dev = fb_helper->dev;
52 struct drm_connector *connector;
53 int i;
d50ba256 54
0b4c0f3f
DA
55 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
56 struct drm_fb_helper_connector *fb_helper_connector;
57
58 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
59 if (!fb_helper_connector)
60 goto fail;
d50ba256 61
0b4c0f3f
DA
62 fb_helper_connector->connector = connector;
63 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
64 }
d50ba256 65 return 0;
0b4c0f3f
DA
66fail:
67 for (i = 0; i < fb_helper->connector_count; i++) {
68 kfree(fb_helper->connector_info[i]);
69 fb_helper->connector_info[i] = NULL;
70 }
71 fb_helper->connector_count = 0;
72 return -ENOMEM;
d50ba256 73}
0b4c0f3f 74EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
d50ba256 75
0b4c0f3f 76static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
d50ba256 77{
0b4c0f3f
DA
78 struct drm_fb_helper_connector *fb_helper_conn;
79 int i;
d50ba256 80
0b4c0f3f 81 for (i = 0; i < fb_helper->connector_count; i++) {
1794d257
CW
82 struct drm_cmdline_mode *mode;
83 struct drm_connector *connector;
d50ba256
DA
84 char *option = NULL;
85
0b4c0f3f 86 fb_helper_conn = fb_helper->connector_info[i];
1794d257
CW
87 connector = fb_helper_conn->connector;
88 mode = &fb_helper_conn->cmdline_mode;
0b4c0f3f 89
d50ba256 90 /* do something on return - turn off connector maybe */
1794d257 91 if (fb_get_options(drm_get_connector_name(connector), &option))
d50ba256
DA
92 continue;
93
1794d257
CW
94 if (drm_mode_parse_command_line_for_connector(option,
95 connector,
96 mode)) {
97 if (mode->force) {
98 const char *s;
99 switch (mode->force) {
6c91083f
SK
100 case DRM_FORCE_OFF:
101 s = "OFF";
102 break;
103 case DRM_FORCE_ON_DIGITAL:
104 s = "ON - dig";
105 break;
1794d257 106 default:
6c91083f
SK
107 case DRM_FORCE_ON:
108 s = "ON";
109 break;
1794d257
CW
110 }
111
112 DRM_INFO("forcing %s connector %s\n",
113 drm_get_connector_name(connector), s);
114 connector->force = mode->force;
115 }
116
117 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
118 drm_get_connector_name(connector),
119 mode->xres, mode->yres,
120 mode->refresh_specified ? mode->refresh : 60,
121 mode->rb ? " reduced blanking" : "",
122 mode->margins ? " with margins" : "",
123 mode->interlace ? " interlaced" : "");
124 }
125
d50ba256
DA
126 }
127 return 0;
128}
129
99231028
JW
130static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
131{
132 uint16_t *r_base, *g_base, *b_base;
133 int i;
134
135 r_base = crtc->gamma_store;
136 g_base = r_base + crtc->gamma_size;
137 b_base = g_base + crtc->gamma_size;
138
139 for (i = 0; i < crtc->gamma_size; i++)
140 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
141}
142
143static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
144{
145 uint16_t *r_base, *g_base, *b_base;
146
ebe0f244
LP
147 if (crtc->funcs->gamma_set == NULL)
148 return;
149
99231028
JW
150 r_base = crtc->gamma_store;
151 g_base = r_base + crtc->gamma_size;
152 b_base = g_base + crtc->gamma_size;
153
154 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
155}
156
1a7aba7f
JB
157int drm_fb_helper_debug_enter(struct fb_info *info)
158{
159 struct drm_fb_helper *helper = info->par;
160 struct drm_crtc_helper_funcs *funcs;
161 int i;
162
163 if (list_empty(&kernel_fb_helper_list))
164 return false;
165
166 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
167 for (i = 0; i < helper->crtc_count; i++) {
168 struct drm_mode_set *mode_set =
169 &helper->crtc_info[i].mode_set;
170
171 if (!mode_set->crtc->enabled)
172 continue;
173
174 funcs = mode_set->crtc->helper_private;
99231028 175 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
1a7aba7f
JB
176 funcs->mode_set_base_atomic(mode_set->crtc,
177 mode_set->fb,
178 mode_set->x,
413d45d3 179 mode_set->y,
21c74a8e 180 ENTER_ATOMIC_MODE_SET);
1a7aba7f
JB
181 }
182 }
183
184 return 0;
185}
186EXPORT_SYMBOL(drm_fb_helper_debug_enter);
187
188/* Find the real fb for a given fb helper CRTC */
189static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
190{
191 struct drm_device *dev = crtc->dev;
192 struct drm_crtc *c;
193
194 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
195 if (crtc->base.id == c->base.id)
196 return c->fb;
197 }
198
199 return NULL;
200}
201
202int drm_fb_helper_debug_leave(struct fb_info *info)
203{
204 struct drm_fb_helper *helper = info->par;
205 struct drm_crtc *crtc;
206 struct drm_crtc_helper_funcs *funcs;
207 struct drm_framebuffer *fb;
208 int i;
209
210 for (i = 0; i < helper->crtc_count; i++) {
211 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
212 crtc = mode_set->crtc;
213 funcs = crtc->helper_private;
214 fb = drm_mode_config_fb(crtc);
215
216 if (!crtc->enabled)
217 continue;
218
219 if (!fb) {
220 DRM_ERROR("no fb to restore??\n");
221 continue;
222 }
223
99231028 224 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
1a7aba7f 225 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
21c74a8e 226 crtc->y, LEAVE_ATOMIC_MODE_SET);
1a7aba7f
JB
227 }
228
229 return 0;
230}
231EXPORT_SYMBOL(drm_fb_helper_debug_leave);
232
e8e7a2b8
DA
233bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
234{
235 bool error = false;
236 int i, ret;
237 for (i = 0; i < fb_helper->crtc_count; i++) {
238 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
d3904754 239 ret = mode_set->crtc->funcs->set_config(mode_set);
e8e7a2b8
DA
240 if (ret)
241 error = true;
242 }
243 return error;
244}
245EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
246
78b9c353 247static bool drm_fb_helper_force_kernel_mode(void)
785b93ef 248{
785b93ef
DA
249 bool ret, error = false;
250 struct drm_fb_helper *helper;
251
252 if (list_empty(&kernel_fb_helper_list))
253 return false;
254
255 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
e8e7a2b8
DA
256 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
257 continue;
258
259 ret = drm_fb_helper_restore_fbdev_mode(helper);
260 if (ret)
261 error = true;
785b93ef
DA
262 }
263 return error;
264}
265
266int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
267 void *panic_str)
268{
d68752cf
HD
269 /*
270 * It's a waste of time and effort to switch back to text console
271 * if the kernel should reboot before panic messages can be seen.
272 */
273 if (panic_timeout < 0)
274 return 0;
275
d56b1b9d 276 pr_err("panic occurred, switching back to text console\n");
785b93ef 277 return drm_fb_helper_force_kernel_mode();
785b93ef
DA
278}
279EXPORT_SYMBOL(drm_fb_helper_panic);
280
281static struct notifier_block paniced = {
282 .notifier_call = drm_fb_helper_panic,
283};
284
285/**
286 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
287 *
288 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
289 */
290void drm_fb_helper_restore(void)
291{
292 bool ret;
293 ret = drm_fb_helper_force_kernel_mode();
294 if (ret == true)
295 DRM_ERROR("Failed to restore crtc configuration\n");
296}
297EXPORT_SYMBOL(drm_fb_helper_restore);
298
bea1d35b 299#ifdef CONFIG_MAGIC_SYSRQ
785b93ef
DA
300static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
301{
302 drm_fb_helper_restore();
303}
304static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
305
1495cc9d 306static void drm_fb_helper_sysrq(int dummy1)
785b93ef
DA
307{
308 schedule_work(&drm_fb_helper_restore_work);
309}
310
311static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
312 .handler = drm_fb_helper_sysrq,
313 .help_msg = "force-fb(V)",
314 .action_msg = "Restore framebuffer console",
315};
b8c40d62
RD
316#else
317static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
bea1d35b 318#endif
785b93ef 319
3a8148c5 320static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
785b93ef
DA
321{
322 struct drm_fb_helper *fb_helper = info->par;
323 struct drm_device *dev = fb_helper->dev;
324 struct drm_crtc *crtc;
023eb571 325 struct drm_connector *connector;
023eb571 326 int i, j;
785b93ef
DA
327
328 /*
3a8148c5 329 * For each CRTC in this fb, turn the connectors on/off.
785b93ef 330 */
8be48d92 331 mutex_lock(&dev->mode_config.mutex);
e87b2c42 332 for (i = 0; i < fb_helper->crtc_count; i++) {
8be48d92 333 crtc = fb_helper->crtc_info[i].mode_set.crtc;
785b93ef 334
8be48d92
DA
335 if (!crtc->enabled)
336 continue;
337
3a8148c5 338 /* Walk the connectors & encoders on this fb turning them on/off */
023eb571
JB
339 for (j = 0; j < fb_helper->connector_count; j++) {
340 connector = fb_helper->connector_info[j]->connector;
e04190e0 341 connector->funcs->dpms(connector, dpms_mode);
023eb571 342 drm_connector_property_set_value(connector,
3a8148c5 343 dev->mode_config.dpms_property, dpms_mode);
785b93ef 344 }
785b93ef 345 }
8be48d92 346 mutex_unlock(&dev->mode_config.mutex);
785b93ef
DA
347}
348
349int drm_fb_helper_blank(int blank, struct fb_info *info)
350{
351 switch (blank) {
731b5a15 352 /* Display: On; HSync: On, VSync: On */
785b93ef 353 case FB_BLANK_UNBLANK:
3a8148c5 354 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
785b93ef 355 break;
731b5a15 356 /* Display: Off; HSync: On, VSync: On */
785b93ef 357 case FB_BLANK_NORMAL:
3a8148c5 358 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
785b93ef 359 break;
731b5a15 360 /* Display: Off; HSync: Off, VSync: On */
785b93ef 361 case FB_BLANK_HSYNC_SUSPEND:
3a8148c5 362 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
785b93ef 363 break;
731b5a15 364 /* Display: Off; HSync: On, VSync: Off */
785b93ef 365 case FB_BLANK_VSYNC_SUSPEND:
3a8148c5 366 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
785b93ef 367 break;
731b5a15 368 /* Display: Off; HSync: Off, VSync: Off */
785b93ef 369 case FB_BLANK_POWERDOWN:
3a8148c5 370 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
785b93ef
DA
371 break;
372 }
373 return 0;
374}
375EXPORT_SYMBOL(drm_fb_helper_blank);
376
377static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
378{
379 int i;
380
0b4c0f3f
DA
381 for (i = 0; i < helper->connector_count; i++)
382 kfree(helper->connector_info[i]);
383 kfree(helper->connector_info);
a1b7736d 384 for (i = 0; i < helper->crtc_count; i++) {
785b93ef 385 kfree(helper->crtc_info[i].mode_set.connectors);
a1b7736d
SH
386 if (helper->crtc_info[i].mode_set.mode)
387 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
388 }
785b93ef
DA
389 kfree(helper->crtc_info);
390}
391
4abe3520
DA
392int drm_fb_helper_init(struct drm_device *dev,
393 struct drm_fb_helper *fb_helper,
eb1f8e4f 394 int crtc_count, int max_conn_count)
785b93ef 395{
785b93ef 396 struct drm_crtc *crtc;
785b93ef
DA
397 int i;
398
4abe3520 399 fb_helper->dev = dev;
4abe3520
DA
400
401 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
402
403 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
404 if (!fb_helper->crtc_info)
785b93ef
DA
405 return -ENOMEM;
406
4abe3520
DA
407 fb_helper->crtc_count = crtc_count;
408 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
409 if (!fb_helper->connector_info) {
410 kfree(fb_helper->crtc_info);
0b4c0f3f
DA
411 return -ENOMEM;
412 }
4abe3520 413 fb_helper->connector_count = 0;
785b93ef
DA
414
415 for (i = 0; i < crtc_count; i++) {
4abe3520 416 fb_helper->crtc_info[i].mode_set.connectors =
785b93ef
DA
417 kcalloc(max_conn_count,
418 sizeof(struct drm_connector *),
419 GFP_KERNEL);
420
4a1b0714 421 if (!fb_helper->crtc_info[i].mode_set.connectors)
785b93ef 422 goto out_free;
4abe3520 423 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
785b93ef
DA
424 }
425
426 i = 0;
427 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4abe3520 428 fb_helper->crtc_info[i].mode_set.crtc = crtc;
785b93ef
DA
429 i++;
430 }
e9ad3181 431
785b93ef
DA
432 return 0;
433out_free:
4abe3520 434 drm_fb_helper_crtc_free(fb_helper);
785b93ef
DA
435 return -ENOMEM;
436}
4abe3520
DA
437EXPORT_SYMBOL(drm_fb_helper_init);
438
439void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
440{
441 if (!list_empty(&fb_helper->kernel_fb_list)) {
442 list_del(&fb_helper->kernel_fb_list);
443 if (list_empty(&kernel_fb_helper_list)) {
d56b1b9d 444 pr_info("drm: unregistered panic notifier\n");
4abe3520
DA
445 atomic_notifier_chain_unregister(&panic_notifier_list,
446 &paniced);
447 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
448 }
449 }
450
451 drm_fb_helper_crtc_free(fb_helper);
452
4abe3520
DA
453}
454EXPORT_SYMBOL(drm_fb_helper_fini);
785b93ef 455
c850cb78 456static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
b8c00ac5
DA
457 u16 blue, u16 regno, struct fb_info *info)
458{
459 struct drm_fb_helper *fb_helper = info->par;
460 struct drm_framebuffer *fb = fb_helper->fb;
461 int pindex;
462
c850cb78
DA
463 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
464 u32 *palette;
465 u32 value;
466 /* place color in psuedopalette */
467 if (regno > 16)
468 return -EINVAL;
469 palette = (u32 *)info->pseudo_palette;
470 red >>= (16 - info->var.red.length);
471 green >>= (16 - info->var.green.length);
472 blue >>= (16 - info->var.blue.length);
473 value = (red << info->var.red.offset) |
474 (green << info->var.green.offset) |
475 (blue << info->var.blue.offset);
9da12b6a
RC
476 if (info->var.transp.length > 0) {
477 u32 mask = (1 << info->var.transp.length) - 1;
478 mask <<= info->var.transp.offset;
479 value |= mask;
480 }
c850cb78
DA
481 palette[regno] = value;
482 return 0;
483 }
484
b8c00ac5
DA
485 pindex = regno;
486
487 if (fb->bits_per_pixel == 16) {
488 pindex = regno << 3;
489
490 if (fb->depth == 16 && regno > 63)
c850cb78 491 return -EINVAL;
b8c00ac5 492 if (fb->depth == 15 && regno > 31)
c850cb78 493 return -EINVAL;
b8c00ac5
DA
494
495 if (fb->depth == 16) {
496 u16 r, g, b;
497 int i;
498 if (regno < 32) {
499 for (i = 0; i < 8; i++)
500 fb_helper->funcs->gamma_set(crtc, red,
501 green, blue, pindex + i);
502 }
503
504 fb_helper->funcs->gamma_get(crtc, &r,
505 &g, &b,
506 pindex >> 1);
507
508 for (i = 0; i < 4; i++)
509 fb_helper->funcs->gamma_set(crtc, r,
510 green, b,
511 (pindex >> 1) + i);
512 }
513 }
514
515 if (fb->depth != 16)
516 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
c850cb78 517 return 0;
b8c00ac5
DA
518}
519
068143d3
DA
520int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
521{
522 struct drm_fb_helper *fb_helper = info->par;
8be48d92 523 struct drm_crtc_helper_funcs *crtc_funcs;
068143d3
DA
524 u16 *red, *green, *blue, *transp;
525 struct drm_crtc *crtc;
062ac622 526 int i, j, rc = 0;
068143d3
DA
527 int start;
528
8be48d92
DA
529 for (i = 0; i < fb_helper->crtc_count; i++) {
530 crtc = fb_helper->crtc_info[i].mode_set.crtc;
531 crtc_funcs = crtc->helper_private;
068143d3
DA
532
533 red = cmap->red;
534 green = cmap->green;
535 blue = cmap->blue;
536 transp = cmap->transp;
537 start = cmap->start;
538
062ac622 539 for (j = 0; j < cmap->len; j++) {
068143d3
DA
540 u16 hred, hgreen, hblue, htransp = 0xffff;
541
542 hred = *red++;
543 hgreen = *green++;
544 hblue = *blue++;
545
546 if (transp)
547 htransp = *transp++;
548
c850cb78
DA
549 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
550 if (rc)
551 return rc;
068143d3
DA
552 }
553 crtc_funcs->load_lut(crtc);
554 }
555 return rc;
556}
557EXPORT_SYMBOL(drm_fb_helper_setcmap);
558
785b93ef
DA
559int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
560 struct fb_info *info)
561{
562 struct drm_fb_helper *fb_helper = info->par;
563 struct drm_framebuffer *fb = fb_helper->fb;
564 int depth;
565
f90ebd9e 566 if (var->pixclock != 0 || in_dbg_master())
785b93ef
DA
567 return -EINVAL;
568
569 /* Need to resize the fb object !!! */
62fb376e
CW
570 if (var->bits_per_pixel > fb->bits_per_pixel ||
571 var->xres > fb->width || var->yres > fb->height ||
572 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
509c7d83 573 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
62fb376e
CW
574 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
575 var->xres, var->yres, var->bits_per_pixel,
576 var->xres_virtual, var->yres_virtual,
509c7d83 577 fb->width, fb->height, fb->bits_per_pixel);
785b93ef
DA
578 return -EINVAL;
579 }
580
581 switch (var->bits_per_pixel) {
582 case 16:
583 depth = (var->green.length == 6) ? 16 : 15;
584 break;
585 case 32:
586 depth = (var->transp.length > 0) ? 32 : 24;
587 break;
588 default:
589 depth = var->bits_per_pixel;
590 break;
591 }
592
593 switch (depth) {
594 case 8:
595 var->red.offset = 0;
596 var->green.offset = 0;
597 var->blue.offset = 0;
598 var->red.length = 8;
599 var->green.length = 8;
600 var->blue.length = 8;
601 var->transp.length = 0;
602 var->transp.offset = 0;
603 break;
604 case 15:
605 var->red.offset = 10;
606 var->green.offset = 5;
607 var->blue.offset = 0;
608 var->red.length = 5;
609 var->green.length = 5;
610 var->blue.length = 5;
611 var->transp.length = 1;
612 var->transp.offset = 15;
613 break;
614 case 16:
615 var->red.offset = 11;
616 var->green.offset = 5;
617 var->blue.offset = 0;
618 var->red.length = 5;
619 var->green.length = 6;
620 var->blue.length = 5;
621 var->transp.length = 0;
622 var->transp.offset = 0;
623 break;
624 case 24:
625 var->red.offset = 16;
626 var->green.offset = 8;
627 var->blue.offset = 0;
628 var->red.length = 8;
629 var->green.length = 8;
630 var->blue.length = 8;
631 var->transp.length = 0;
632 var->transp.offset = 0;
633 break;
634 case 32:
635 var->red.offset = 16;
636 var->green.offset = 8;
637 var->blue.offset = 0;
638 var->red.length = 8;
639 var->green.length = 8;
640 var->blue.length = 8;
641 var->transp.length = 8;
642 var->transp.offset = 24;
643 break;
644 default:
645 return -EINVAL;
646 }
647 return 0;
648}
649EXPORT_SYMBOL(drm_fb_helper_check_var);
650
651/* this will let fbcon do the mode init */
652int drm_fb_helper_set_par(struct fb_info *info)
653{
654 struct drm_fb_helper *fb_helper = info->par;
655 struct drm_device *dev = fb_helper->dev;
656 struct fb_var_screeninfo *var = &info->var;
657 struct drm_crtc *crtc;
658 int ret;
659 int i;
660
5349ef31 661 if (var->pixclock != 0) {
172e91f5 662 DRM_ERROR("PIXEL CLOCK SET\n");
785b93ef
DA
663 return -EINVAL;
664 }
665
8be48d92
DA
666 mutex_lock(&dev->mode_config.mutex);
667 for (i = 0; i < fb_helper->crtc_count; i++) {
668 crtc = fb_helper->crtc_info[i].mode_set.crtc;
0b4c0f3f
DA
669 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
670 if (ret) {
785b93ef 671 mutex_unlock(&dev->mode_config.mutex);
0b4c0f3f 672 return ret;
785b93ef
DA
673 }
674 }
8be48d92 675 mutex_unlock(&dev->mode_config.mutex);
4abe3520
DA
676
677 if (fb_helper->delayed_hotplug) {
678 fb_helper->delayed_hotplug = false;
eb1f8e4f 679 drm_fb_helper_hotplug_event(fb_helper);
4abe3520 680 }
785b93ef
DA
681 return 0;
682}
683EXPORT_SYMBOL(drm_fb_helper_set_par);
684
685int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
686 struct fb_info *info)
687{
688 struct drm_fb_helper *fb_helper = info->par;
689 struct drm_device *dev = fb_helper->dev;
690 struct drm_mode_set *modeset;
691 struct drm_crtc *crtc;
692 int ret = 0;
693 int i;
694
8be48d92
DA
695 mutex_lock(&dev->mode_config.mutex);
696 for (i = 0; i < fb_helper->crtc_count; i++) {
697 crtc = fb_helper->crtc_info[i].mode_set.crtc;
785b93ef
DA
698
699 modeset = &fb_helper->crtc_info[i].mode_set;
700
701 modeset->x = var->xoffset;
702 modeset->y = var->yoffset;
703
704 if (modeset->num_connectors) {
785b93ef 705 ret = crtc->funcs->set_config(modeset);
785b93ef
DA
706 if (!ret) {
707 info->var.xoffset = var->xoffset;
708 info->var.yoffset = var->yoffset;
709 }
710 }
711 }
8be48d92 712 mutex_unlock(&dev->mode_config.mutex);
785b93ef
DA
713 return ret;
714}
715EXPORT_SYMBOL(drm_fb_helper_pan_display);
716
8be48d92
DA
717int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
718 int preferred_bpp)
785b93ef 719{
785b93ef
DA
720 int new_fb = 0;
721 int crtc_count = 0;
4abe3520 722 int i;
785b93ef 723 struct fb_info *info;
38651674 724 struct drm_fb_helper_surface_size sizes;
8be48d92 725 int gamma_size = 0;
38651674
DA
726
727 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
728 sizes.surface_depth = 24;
729 sizes.surface_bpp = 32;
730 sizes.fb_width = (unsigned)-1;
731 sizes.fb_height = (unsigned)-1;
785b93ef 732
b8c00ac5
DA
733 /* if driver picks 8 or 16 by default use that
734 for both depth/bpp */
96081cdf 735 if (preferred_bpp != sizes.surface_bpp)
38651674 736 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
96081cdf 737
785b93ef 738 /* first up get a count of crtcs now in use and new min/maxes width/heights */
0b4c0f3f
DA
739 for (i = 0; i < fb_helper->connector_count; i++) {
740 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1794d257 741 struct drm_cmdline_mode *cmdline_mode;
8ef8678c 742
0b4c0f3f 743 cmdline_mode = &fb_helper_conn->cmdline_mode;
d50ba256
DA
744
745 if (cmdline_mode->bpp_specified) {
746 switch (cmdline_mode->bpp) {
747 case 8:
38651674 748 sizes.surface_depth = sizes.surface_bpp = 8;
d50ba256
DA
749 break;
750 case 15:
38651674
DA
751 sizes.surface_depth = 15;
752 sizes.surface_bpp = 16;
d50ba256
DA
753 break;
754 case 16:
38651674 755 sizes.surface_depth = sizes.surface_bpp = 16;
d50ba256
DA
756 break;
757 case 24:
38651674 758 sizes.surface_depth = sizes.surface_bpp = 24;
d50ba256
DA
759 break;
760 case 32:
38651674
DA
761 sizes.surface_depth = 24;
762 sizes.surface_bpp = 32;
d50ba256
DA
763 break;
764 }
765 break;
766 }
767 }
768
8be48d92
DA
769 crtc_count = 0;
770 for (i = 0; i < fb_helper->crtc_count; i++) {
771 struct drm_display_mode *desired_mode;
772 desired_mode = fb_helper->crtc_info[i].desired_mode;
773
774 if (desired_mode) {
775 if (gamma_size == 0)
776 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
777 if (desired_mode->hdisplay < sizes.fb_width)
778 sizes.fb_width = desired_mode->hdisplay;
779 if (desired_mode->vdisplay < sizes.fb_height)
780 sizes.fb_height = desired_mode->vdisplay;
781 if (desired_mode->hdisplay > sizes.surface_width)
782 sizes.surface_width = desired_mode->hdisplay;
783 if (desired_mode->vdisplay > sizes.surface_height)
784 sizes.surface_height = desired_mode->vdisplay;
785b93ef
DA
785 crtc_count++;
786 }
787 }
788
38651674 789 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
785b93ef
DA
790 /* hmm everyone went away - assume VGA cable just fell out
791 and will come back later. */
eb1f8e4f 792 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
19b4b445
DA
793 sizes.fb_width = sizes.surface_width = 1024;
794 sizes.fb_height = sizes.surface_height = 768;
785b93ef
DA
795 }
796
38651674 797 /* push down into drivers */
4abe3520 798 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
38651674
DA
799 if (new_fb < 0)
800 return new_fb;
785b93ef 801
38651674 802 info = fb_helper->fbdev;
785b93ef 803
8be48d92 804 /* set the fb pointer */
96081cdf 805 for (i = 0; i < fb_helper->crtc_count; i++)
8be48d92 806 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
785b93ef
DA
807
808 if (new_fb) {
5349ef31 809 info->var.pixclock = 0;
96081cdf 810 if (register_framebuffer(info) < 0)
785b93ef 811 return -EINVAL;
38651674 812
d56b1b9d
SK
813 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
814 info->node, info->fix.id);
38651674 815
785b93ef
DA
816 } else {
817 drm_fb_helper_set_par(info);
818 }
785b93ef
DA
819
820 /* Switch back to kernel console on panic */
821 /* multi card linked list maybe */
822 if (list_empty(&kernel_fb_helper_list)) {
d56b1b9d 823 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
785b93ef
DA
824 atomic_notifier_chain_register(&panic_notifier_list,
825 &paniced);
826 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
827 }
38651674
DA
828 if (new_fb)
829 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
830
785b93ef
DA
831 return 0;
832}
833EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
834
3632ef89
DA
835void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
836 uint32_t depth)
837{
838 info->fix.type = FB_TYPE_PACKED_PIXELS;
839 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
840 FB_VISUAL_TRUECOLOR;
841 info->fix.mmio_start = 0;
842 info->fix.mmio_len = 0;
843 info->fix.type_aux = 0;
844 info->fix.xpanstep = 1; /* doing it in hw */
845 info->fix.ypanstep = 1; /* doing it in hw */
846 info->fix.ywrapstep = 0;
847 info->fix.accel = FB_ACCEL_NONE;
848 info->fix.type_aux = 0;
849
850 info->fix.line_length = pitch;
851 return;
852}
853EXPORT_SYMBOL(drm_fb_helper_fill_fix);
854
38651674 855void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
785b93ef
DA
856 uint32_t fb_width, uint32_t fb_height)
857{
38651674
DA
858 struct drm_framebuffer *fb = fb_helper->fb;
859 info->pseudo_palette = fb_helper->pseudo_palette;
785b93ef
DA
860 info->var.xres_virtual = fb->width;
861 info->var.yres_virtual = fb->height;
862 info->var.bits_per_pixel = fb->bits_per_pixel;
57084d05 863 info->var.accel_flags = FB_ACCELF_TEXT;
785b93ef
DA
864 info->var.xoffset = 0;
865 info->var.yoffset = 0;
866 info->var.activate = FB_ACTIVATE_NOW;
867 info->var.height = -1;
868 info->var.width = -1;
869
870 switch (fb->depth) {
871 case 8:
872 info->var.red.offset = 0;
873 info->var.green.offset = 0;
874 info->var.blue.offset = 0;
875 info->var.red.length = 8; /* 8bit DAC */
876 info->var.green.length = 8;
877 info->var.blue.length = 8;
878 info->var.transp.offset = 0;
879 info->var.transp.length = 0;
880 break;
881 case 15:
882 info->var.red.offset = 10;
883 info->var.green.offset = 5;
884 info->var.blue.offset = 0;
885 info->var.red.length = 5;
886 info->var.green.length = 5;
887 info->var.blue.length = 5;
888 info->var.transp.offset = 15;
889 info->var.transp.length = 1;
890 break;
891 case 16:
892 info->var.red.offset = 11;
893 info->var.green.offset = 5;
894 info->var.blue.offset = 0;
895 info->var.red.length = 5;
896 info->var.green.length = 6;
897 info->var.blue.length = 5;
898 info->var.transp.offset = 0;
899 break;
900 case 24:
901 info->var.red.offset = 16;
902 info->var.green.offset = 8;
903 info->var.blue.offset = 0;
904 info->var.red.length = 8;
905 info->var.green.length = 8;
906 info->var.blue.length = 8;
907 info->var.transp.offset = 0;
908 info->var.transp.length = 0;
909 break;
910 case 32:
911 info->var.red.offset = 16;
912 info->var.green.offset = 8;
913 info->var.blue.offset = 0;
914 info->var.red.length = 8;
915 info->var.green.length = 8;
916 info->var.blue.length = 8;
917 info->var.transp.offset = 24;
918 info->var.transp.length = 8;
919 break;
920 default:
921 break;
922 }
923
924 info->var.xres = fb_width;
925 info->var.yres = fb_height;
926}
927EXPORT_SYMBOL(drm_fb_helper_fill_var);
38651674 928
0b4c0f3f
DA
929static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
930 uint32_t maxX,
931 uint32_t maxY)
38651674
DA
932{
933 struct drm_connector *connector;
934 int count = 0;
0b4c0f3f 935 int i;
38651674 936
0b4c0f3f
DA
937 for (i = 0; i < fb_helper->connector_count; i++) {
938 connector = fb_helper->connector_info[i]->connector;
38651674
DA
939 count += connector->funcs->fill_modes(connector, maxX, maxY);
940 }
941
942 return count;
943}
944
0b4c0f3f 945static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
38651674
DA
946{
947 struct drm_display_mode *mode;
948
0b4c0f3f 949 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
38651674
DA
950 if (drm_mode_width(mode) > width ||
951 drm_mode_height(mode) > height)
952 continue;
953 if (mode->type & DRM_MODE_TYPE_PREFERRED)
954 return mode;
955 }
956 return NULL;
957}
958
0b4c0f3f 959static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
38651674 960{
1794d257 961 struct drm_cmdline_mode *cmdline_mode;
0b4c0f3f 962 cmdline_mode = &fb_connector->cmdline_mode;
38651674
DA
963 return cmdline_mode->specified;
964}
965
0b4c0f3f
DA
966static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
967 int width, int height)
38651674 968{
1794d257 969 struct drm_cmdline_mode *cmdline_mode;
38651674
DA
970 struct drm_display_mode *mode = NULL;
971
0b4c0f3f 972 cmdline_mode = &fb_helper_conn->cmdline_mode;
38651674
DA
973 if (cmdline_mode->specified == false)
974 return mode;
975
976 /* attempt to find a matching mode in the list of modes
977 * we have gotten so far, if not add a CVT mode that conforms
978 */
979 if (cmdline_mode->rb || cmdline_mode->margins)
980 goto create_mode;
981
0b4c0f3f 982 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
38651674
DA
983 /* check width/height */
984 if (mode->hdisplay != cmdline_mode->xres ||
985 mode->vdisplay != cmdline_mode->yres)
986 continue;
987
988 if (cmdline_mode->refresh_specified) {
989 if (mode->vrefresh != cmdline_mode->refresh)
990 continue;
991 }
992
993 if (cmdline_mode->interlace) {
994 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
995 continue;
996 }
997 return mode;
998 }
999
1000create_mode:
1794d257
CW
1001 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1002 cmdline_mode);
0b4c0f3f 1003 list_add(&mode->head, &fb_helper_conn->connector->modes);
38651674
DA
1004 return mode;
1005}
1006
1007static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1008{
1009 bool enable;
1010
96081cdf 1011 if (strict)
38651674 1012 enable = connector->status == connector_status_connected;
96081cdf 1013 else
38651674 1014 enable = connector->status != connector_status_disconnected;
96081cdf 1015
38651674
DA
1016 return enable;
1017}
1018
0b4c0f3f
DA
1019static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1020 bool *enabled)
38651674
DA
1021{
1022 bool any_enabled = false;
1023 struct drm_connector *connector;
1024 int i = 0;
1025
0b4c0f3f
DA
1026 for (i = 0; i < fb_helper->connector_count; i++) {
1027 connector = fb_helper->connector_info[i]->connector;
38651674
DA
1028 enabled[i] = drm_connector_enabled(connector, true);
1029 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1030 enabled[i] ? "yes" : "no");
1031 any_enabled |= enabled[i];
38651674
DA
1032 }
1033
1034 if (any_enabled)
1035 return;
1036
0b4c0f3f
DA
1037 for (i = 0; i < fb_helper->connector_count; i++) {
1038 connector = fb_helper->connector_info[i]->connector;
38651674 1039 enabled[i] = drm_connector_enabled(connector, false);
38651674
DA
1040 }
1041}
1042
1d42bbc8
DA
1043static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1044 struct drm_display_mode **modes,
1045 bool *enabled, int width, int height)
1046{
1047 int count, i, j;
1048 bool can_clone = false;
1049 struct drm_fb_helper_connector *fb_helper_conn;
1050 struct drm_display_mode *dmt_mode, *mode;
1051
1052 /* only contemplate cloning in the single crtc case */
1053 if (fb_helper->crtc_count > 1)
1054 return false;
1055
1056 count = 0;
1057 for (i = 0; i < fb_helper->connector_count; i++) {
1058 if (enabled[i])
1059 count++;
1060 }
1061
1062 /* only contemplate cloning if more than one connector is enabled */
1063 if (count <= 1)
1064 return false;
1065
1066 /* check the command line or if nothing common pick 1024x768 */
1067 can_clone = true;
1068 for (i = 0; i < fb_helper->connector_count; i++) {
1069 if (!enabled[i])
1070 continue;
1071 fb_helper_conn = fb_helper->connector_info[i];
1072 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1073 if (!modes[i]) {
1074 can_clone = false;
1075 break;
1076 }
1077 for (j = 0; j < i; j++) {
1078 if (!enabled[j])
1079 continue;
1080 if (!drm_mode_equal(modes[j], modes[i]))
1081 can_clone = false;
1082 }
1083 }
1084
1085 if (can_clone) {
1086 DRM_DEBUG_KMS("can clone using command line\n");
1087 return true;
1088 }
1089
1090 /* try and find a 1024x768 mode on each connector */
1091 can_clone = true;
f6e252ba 1092 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1d42bbc8
DA
1093
1094 for (i = 0; i < fb_helper->connector_count; i++) {
1095
1096 if (!enabled[i])
1097 continue;
1098
1099 fb_helper_conn = fb_helper->connector_info[i];
1100 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1101 if (drm_mode_equal(mode, dmt_mode))
1102 modes[i] = mode;
1103 }
1104 if (!modes[i])
1105 can_clone = false;
1106 }
1107
1108 if (can_clone) {
1109 DRM_DEBUG_KMS("can clone using 1024x768\n");
1110 return true;
1111 }
1112 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1113 return false;
1114}
1115
0b4c0f3f 1116static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
38651674
DA
1117 struct drm_display_mode **modes,
1118 bool *enabled, int width, int height)
1119{
0b4c0f3f
DA
1120 struct drm_fb_helper_connector *fb_helper_conn;
1121 int i;
38651674 1122
0b4c0f3f
DA
1123 for (i = 0; i < fb_helper->connector_count; i++) {
1124 fb_helper_conn = fb_helper->connector_info[i];
38651674 1125
0b4c0f3f 1126 if (enabled[i] == false)
38651674 1127 continue;
38651674
DA
1128
1129 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
0b4c0f3f 1130 fb_helper_conn->connector->base.id);
38651674
DA
1131
1132 /* got for command line mode first */
0b4c0f3f 1133 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
38651674
DA
1134 if (!modes[i]) {
1135 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
0b4c0f3f
DA
1136 fb_helper_conn->connector->base.id);
1137 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
38651674
DA
1138 }
1139 /* No preferred modes, pick one off the list */
0b4c0f3f
DA
1140 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1141 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
38651674
DA
1142 break;
1143 }
1144 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1145 "none");
38651674
DA
1146 }
1147 return true;
1148}
1149
8be48d92
DA
1150static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1151 struct drm_fb_helper_crtc **best_crtcs,
38651674
DA
1152 struct drm_display_mode **modes,
1153 int n, int width, int height)
1154{
1155 int c, o;
8be48d92 1156 struct drm_device *dev = fb_helper->dev;
38651674
DA
1157 struct drm_connector *connector;
1158 struct drm_connector_helper_funcs *connector_funcs;
1159 struct drm_encoder *encoder;
8be48d92 1160 struct drm_fb_helper_crtc *best_crtc;
38651674 1161 int my_score, best_score, score;
8be48d92 1162 struct drm_fb_helper_crtc **crtcs, *crtc;
0b4c0f3f 1163 struct drm_fb_helper_connector *fb_helper_conn;
38651674 1164
0b4c0f3f 1165 if (n == fb_helper->connector_count)
38651674 1166 return 0;
0b4c0f3f
DA
1167
1168 fb_helper_conn = fb_helper->connector_info[n];
1169 connector = fb_helper_conn->connector;
38651674
DA
1170
1171 best_crtcs[n] = NULL;
1172 best_crtc = NULL;
8be48d92 1173 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
38651674
DA
1174 if (modes[n] == NULL)
1175 return best_score;
1176
8be48d92
DA
1177 crtcs = kzalloc(dev->mode_config.num_connector *
1178 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1179 if (!crtcs)
1180 return best_score;
1181
1182 my_score = 1;
1183 if (connector->status == connector_status_connected)
1184 my_score++;
0b4c0f3f 1185 if (drm_has_cmdline_mode(fb_helper_conn))
38651674 1186 my_score++;
0b4c0f3f 1187 if (drm_has_preferred_mode(fb_helper_conn, width, height))
38651674
DA
1188 my_score++;
1189
1190 connector_funcs = connector->helper_private;
1191 encoder = connector_funcs->best_encoder(connector);
1192 if (!encoder)
1193 goto out;
1194
38651674
DA
1195 /* select a crtc for this connector and then attempt to configure
1196 remaining connectors */
8be48d92
DA
1197 for (c = 0; c < fb_helper->crtc_count; c++) {
1198 crtc = &fb_helper->crtc_info[c];
38651674 1199
96081cdf 1200 if ((encoder->possible_crtcs & (1 << c)) == 0)
38651674 1201 continue;
38651674
DA
1202
1203 for (o = 0; o < n; o++)
1204 if (best_crtcs[o] == crtc)
1205 break;
1206
1207 if (o < n) {
1d42bbc8
DA
1208 /* ignore cloning unless only a single crtc */
1209 if (fb_helper->crtc_count > 1)
1210 continue;
1211
1212 if (!drm_mode_equal(modes[o], modes[n]))
1213 continue;
38651674
DA
1214 }
1215
1216 crtcs[n] = crtc;
8be48d92
DA
1217 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1218 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
38651674
DA
1219 width, height);
1220 if (score > best_score) {
1221 best_crtc = crtc;
1222 best_score = score;
1223 memcpy(best_crtcs, crtcs,
1224 dev->mode_config.num_connector *
8be48d92 1225 sizeof(struct drm_fb_helper_crtc *));
38651674 1226 }
38651674
DA
1227 }
1228out:
1229 kfree(crtcs);
1230 return best_score;
1231}
1232
8be48d92 1233static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
38651674 1234{
8be48d92
DA
1235 struct drm_device *dev = fb_helper->dev;
1236 struct drm_fb_helper_crtc **crtcs;
38651674 1237 struct drm_display_mode **modes;
8be48d92 1238 struct drm_mode_set *modeset;
38651674
DA
1239 bool *enabled;
1240 int width, height;
1241 int i, ret;
1242
1243 DRM_DEBUG_KMS("\n");
1244
1245 width = dev->mode_config.max_width;
1246 height = dev->mode_config.max_height;
1247
38651674 1248 crtcs = kcalloc(dev->mode_config.num_connector,
8be48d92 1249 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1250 modes = kcalloc(dev->mode_config.num_connector,
1251 sizeof(struct drm_display_mode *), GFP_KERNEL);
1252 enabled = kcalloc(dev->mode_config.num_connector,
1253 sizeof(bool), GFP_KERNEL);
8c5eaca0
SK
1254 if (!crtcs || !modes || !enabled) {
1255 DRM_ERROR("Memory allocation failed\n");
1256 goto out;
1257 }
1258
38651674 1259
0b4c0f3f 1260 drm_enable_connectors(fb_helper, enabled);
38651674 1261
1d42bbc8
DA
1262 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1263 if (!ret) {
1264 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1265 if (!ret)
1266 DRM_ERROR("Unable to find initial modes\n");
1267 }
38651674
DA
1268
1269 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1270
8be48d92
DA
1271 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1272
1273 /* need to set the modesets up here for use later */
1274 /* fill out the connector<->crtc mappings into the modesets */
1275 for (i = 0; i < fb_helper->crtc_count; i++) {
1276 modeset = &fb_helper->crtc_info[i].mode_set;
1277 modeset->num_connectors = 0;
1278 }
38651674 1279
0b4c0f3f 1280 for (i = 0; i < fb_helper->connector_count; i++) {
38651674 1281 struct drm_display_mode *mode = modes[i];
8be48d92
DA
1282 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1283 modeset = &fb_crtc->mode_set;
38651674 1284
8be48d92 1285 if (mode && fb_crtc) {
38651674 1286 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
8be48d92
DA
1287 mode->name, fb_crtc->mode_set.crtc->base.id);
1288 fb_crtc->desired_mode = mode;
1289 if (modeset->mode)
1290 drm_mode_destroy(dev, modeset->mode);
1291 modeset->mode = drm_mode_duplicate(dev,
1292 fb_crtc->desired_mode);
0b4c0f3f 1293 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
38651674 1294 }
38651674
DA
1295 }
1296
8c5eaca0 1297out:
38651674
DA
1298 kfree(crtcs);
1299 kfree(modes);
1300 kfree(enabled);
1301}
1302
1303/**
1304 * drm_helper_initial_config - setup a sane initial connector configuration
1305 * @dev: DRM device
1306 *
1307 * LOCKING:
1308 * Called at init time, must take mode config lock.
1309 *
1310 * Scan the CRTCs and connectors and try to put together an initial setup.
1311 * At the moment, this is a cloned configuration across all heads with
1312 * a new framebuffer object as the backing store.
1313 *
1314 * RETURNS:
1315 * Zero if everything went ok, nonzero otherwise.
1316 */
4abe3520 1317bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
38651674 1318{
8be48d92 1319 struct drm_device *dev = fb_helper->dev;
38651674
DA
1320 int count = 0;
1321
1322 /* disable all the possible outputs/crtcs before entering KMS mode */
8be48d92 1323 drm_helper_disable_unused_functions(fb_helper->dev);
38651674 1324
0b4c0f3f 1325 drm_fb_helper_parse_command_line(fb_helper);
38651674 1326
0b4c0f3f
DA
1327 count = drm_fb_helper_probe_connector_modes(fb_helper,
1328 dev->mode_config.max_width,
1329 dev->mode_config.max_height);
38651674
DA
1330 /*
1331 * we shouldn't end up with no modes here.
1332 */
96081cdf 1333 if (count == 0)
d56b1b9d 1334 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
96081cdf 1335
8be48d92 1336 drm_setup_crtcs(fb_helper);
38651674 1337
4abe3520 1338 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
38651674 1339}
8be48d92 1340EXPORT_SYMBOL(drm_fb_helper_initial_config);
38651674 1341
7394371d
CW
1342/**
1343 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1344 * probing all the outputs attached to the fb.
1345 * @fb_helper: the drm_fb_helper
1346 *
1347 * LOCKING:
1348 * Called at runtime, must take mode config lock.
1349 *
1350 * Scan the connectors attached to the fb_helper and try to put together a
1351 * setup after *notification of a change in output configuration.
1352 *
1353 * RETURNS:
1354 * 0 on success and a non-zero error code otherwise.
1355 */
1356int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
38651674 1357{
7394371d 1358 struct drm_device *dev = fb_helper->dev;
5c4426a7 1359 int count = 0;
4abe3520 1360 u32 max_width, max_height, bpp_sel;
343065a6 1361 int bound = 0, crtcs_bound = 0;
4abe3520 1362 struct drm_crtc *crtc;
5c4426a7 1363
eb1f8e4f 1364 if (!fb_helper->fb)
7394371d 1365 return 0;
4abe3520 1366
7394371d
CW
1367 mutex_lock(&dev->mode_config.mutex);
1368 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4abe3520 1369 if (crtc->fb)
343065a6 1370 crtcs_bound++;
4abe3520 1371 if (crtc->fb == fb_helper->fb)
343065a6 1372 bound++;
4abe3520
DA
1373 }
1374
343065a6 1375 if (bound < crtcs_bound) {
4abe3520 1376 fb_helper->delayed_hotplug = true;
7394371d
CW
1377 mutex_unlock(&dev->mode_config.mutex);
1378 return 0;
4abe3520 1379 }
eb1f8e4f 1380 DRM_DEBUG_KMS("\n");
4abe3520 1381
eb1f8e4f
DA
1382 max_width = fb_helper->fb->width;
1383 max_height = fb_helper->fb->height;
1384 bpp_sel = fb_helper->fb->bits_per_pixel;
5c4426a7 1385
eb1f8e4f
DA
1386 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1387 max_height);
1388 drm_setup_crtcs(fb_helper);
7394371d 1389 mutex_unlock(&dev->mode_config.mutex);
4abe3520 1390
eb1f8e4f 1391 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
5c4426a7 1392}
eb1f8e4f 1393EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
5c4426a7 1394
6a108a14 1395/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
3ce05168
DF
1396 * but the module doesn't depend on any fb console symbols. At least
1397 * attempt to load fbcon to avoid leaving the system without a usable console.
1398 */
6a108a14 1399#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
3ce05168
DF
1400static int __init drm_fb_helper_modinit(void)
1401{
1402 const char *name = "fbcon";
1403 struct module *fbcon;
1404
1405 mutex_lock(&module_mutex);
1406 fbcon = find_module(name);
1407 mutex_unlock(&module_mutex);
1408
1409 if (!fbcon)
1410 request_module_nowait(name);
1411 return 0;
1412}
1413
1414module_init(drm_fb_helper_modinit);
1415#endif