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