]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/gpu/drm/drm_fb_helper.c
drm/fb-helper: fixup set_config semantics
[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
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;
785b93ef
DA
692 int ret;
693 int i;
694
5349ef31 695 if (var->pixclock != 0) {
172e91f5 696 DRM_ERROR("PIXEL CLOCK SET\n");
785b93ef
DA
697 return -EINVAL;
698 }
699
84849903 700 drm_modeset_lock_all(dev);
8be48d92 701 for (i = 0; i < fb_helper->crtc_count; i++) {
2d13b679 702 ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
0b4c0f3f 703 if (ret) {
84849903 704 drm_modeset_unlock_all(dev);
0b4c0f3f 705 return ret;
785b93ef
DA
706 }
707 }
84849903 708 drm_modeset_unlock_all(dev);
4abe3520
DA
709
710 if (fb_helper->delayed_hotplug) {
711 fb_helper->delayed_hotplug = false;
eb1f8e4f 712 drm_fb_helper_hotplug_event(fb_helper);
4abe3520 713 }
785b93ef
DA
714 return 0;
715}
716EXPORT_SYMBOL(drm_fb_helper_set_par);
717
718int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
719 struct fb_info *info)
720{
721 struct drm_fb_helper *fb_helper = info->par;
722 struct drm_device *dev = fb_helper->dev;
723 struct drm_mode_set *modeset;
724 struct drm_crtc *crtc;
725 int ret = 0;
726 int i;
727
84849903 728 drm_modeset_lock_all(dev);
20c60c35
DV
729 if (!drm_fb_helper_is_bound(fb_helper)) {
730 drm_modeset_unlock_all(dev);
731 return -EBUSY;
732 }
733
8be48d92
DA
734 for (i = 0; i < fb_helper->crtc_count; i++) {
735 crtc = fb_helper->crtc_info[i].mode_set.crtc;
785b93ef
DA
736
737 modeset = &fb_helper->crtc_info[i].mode_set;
738
739 modeset->x = var->xoffset;
740 modeset->y = var->yoffset;
741
742 if (modeset->num_connectors) {
2d13b679 743 ret = drm_mode_set_config_internal(modeset);
785b93ef
DA
744 if (!ret) {
745 info->var.xoffset = var->xoffset;
746 info->var.yoffset = var->yoffset;
747 }
748 }
749 }
84849903 750 drm_modeset_unlock_all(dev);
785b93ef
DA
751 return ret;
752}
753EXPORT_SYMBOL(drm_fb_helper_pan_display);
754
de1ace5b
DV
755static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
756 int preferred_bpp)
785b93ef 757{
785b93ef
DA
758 int new_fb = 0;
759 int crtc_count = 0;
4abe3520 760 int i;
785b93ef 761 struct fb_info *info;
38651674 762 struct drm_fb_helper_surface_size sizes;
8be48d92 763 int gamma_size = 0;
38651674
DA
764
765 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
766 sizes.surface_depth = 24;
767 sizes.surface_bpp = 32;
768 sizes.fb_width = (unsigned)-1;
769 sizes.fb_height = (unsigned)-1;
785b93ef 770
b8c00ac5
DA
771 /* if driver picks 8 or 16 by default use that
772 for both depth/bpp */
96081cdf 773 if (preferred_bpp != sizes.surface_bpp)
38651674 774 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
96081cdf 775
785b93ef 776 /* first up get a count of crtcs now in use and new min/maxes width/heights */
0b4c0f3f
DA
777 for (i = 0; i < fb_helper->connector_count; i++) {
778 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1794d257 779 struct drm_cmdline_mode *cmdline_mode;
8ef8678c 780
0b4c0f3f 781 cmdline_mode = &fb_helper_conn->cmdline_mode;
d50ba256
DA
782
783 if (cmdline_mode->bpp_specified) {
784 switch (cmdline_mode->bpp) {
785 case 8:
38651674 786 sizes.surface_depth = sizes.surface_bpp = 8;
d50ba256
DA
787 break;
788 case 15:
38651674
DA
789 sizes.surface_depth = 15;
790 sizes.surface_bpp = 16;
d50ba256
DA
791 break;
792 case 16:
38651674 793 sizes.surface_depth = sizes.surface_bpp = 16;
d50ba256
DA
794 break;
795 case 24:
38651674 796 sizes.surface_depth = sizes.surface_bpp = 24;
d50ba256
DA
797 break;
798 case 32:
38651674
DA
799 sizes.surface_depth = 24;
800 sizes.surface_bpp = 32;
d50ba256
DA
801 break;
802 }
803 break;
804 }
805 }
806
8be48d92
DA
807 crtc_count = 0;
808 for (i = 0; i < fb_helper->crtc_count; i++) {
809 struct drm_display_mode *desired_mode;
810 desired_mode = fb_helper->crtc_info[i].desired_mode;
811
812 if (desired_mode) {
813 if (gamma_size == 0)
814 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
815 if (desired_mode->hdisplay < sizes.fb_width)
816 sizes.fb_width = desired_mode->hdisplay;
817 if (desired_mode->vdisplay < sizes.fb_height)
818 sizes.fb_height = desired_mode->vdisplay;
819 if (desired_mode->hdisplay > sizes.surface_width)
820 sizes.surface_width = desired_mode->hdisplay;
821 if (desired_mode->vdisplay > sizes.surface_height)
822 sizes.surface_height = desired_mode->vdisplay;
785b93ef
DA
823 crtc_count++;
824 }
825 }
826
38651674 827 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
785b93ef
DA
828 /* hmm everyone went away - assume VGA cable just fell out
829 and will come back later. */
eb1f8e4f 830 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
19b4b445
DA
831 sizes.fb_width = sizes.surface_width = 1024;
832 sizes.fb_height = sizes.surface_height = 768;
785b93ef
DA
833 }
834
38651674 835 /* push down into drivers */
4abe3520 836 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
38651674
DA
837 if (new_fb < 0)
838 return new_fb;
785b93ef 839
38651674 840 info = fb_helper->fbdev;
785b93ef 841
7e53f3a4
DV
842 /*
843 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
844 * events, but at init time drm_setup_crtcs needs to be called before
845 * the fb is allocated (since we need to figure out the desired size of
846 * the fb before we can allocate it ...). Hence we need to fix things up
847 * here again.
848 */
96081cdf 849 for (i = 0; i < fb_helper->crtc_count; i++)
7e53f3a4
DV
850 if (fb_helper->crtc_info[i].mode_set.num_connectors)
851 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
852
785b93ef
DA
853
854 if (new_fb) {
5349ef31 855 info->var.pixclock = 0;
96081cdf 856 if (register_framebuffer(info) < 0)
785b93ef 857 return -EINVAL;
38651674 858
d56b1b9d
SK
859 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
860 info->node, info->fix.id);
38651674 861
785b93ef
DA
862 } else {
863 drm_fb_helper_set_par(info);
864 }
785b93ef
DA
865
866 /* Switch back to kernel console on panic */
867 /* multi card linked list maybe */
868 if (list_empty(&kernel_fb_helper_list)) {
d56b1b9d 869 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
785b93ef
DA
870 atomic_notifier_chain_register(&panic_notifier_list,
871 &paniced);
872 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
873 }
38651674
DA
874 if (new_fb)
875 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
876
785b93ef
DA
877 return 0;
878}
785b93ef 879
3632ef89
DA
880void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
881 uint32_t depth)
882{
883 info->fix.type = FB_TYPE_PACKED_PIXELS;
884 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
885 FB_VISUAL_TRUECOLOR;
886 info->fix.mmio_start = 0;
887 info->fix.mmio_len = 0;
888 info->fix.type_aux = 0;
889 info->fix.xpanstep = 1; /* doing it in hw */
890 info->fix.ypanstep = 1; /* doing it in hw */
891 info->fix.ywrapstep = 0;
892 info->fix.accel = FB_ACCEL_NONE;
893 info->fix.type_aux = 0;
894
895 info->fix.line_length = pitch;
896 return;
897}
898EXPORT_SYMBOL(drm_fb_helper_fill_fix);
899
38651674 900void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
785b93ef
DA
901 uint32_t fb_width, uint32_t fb_height)
902{
38651674
DA
903 struct drm_framebuffer *fb = fb_helper->fb;
904 info->pseudo_palette = fb_helper->pseudo_palette;
785b93ef
DA
905 info->var.xres_virtual = fb->width;
906 info->var.yres_virtual = fb->height;
907 info->var.bits_per_pixel = fb->bits_per_pixel;
57084d05 908 info->var.accel_flags = FB_ACCELF_TEXT;
785b93ef
DA
909 info->var.xoffset = 0;
910 info->var.yoffset = 0;
911 info->var.activate = FB_ACTIVATE_NOW;
912 info->var.height = -1;
913 info->var.width = -1;
914
915 switch (fb->depth) {
916 case 8:
917 info->var.red.offset = 0;
918 info->var.green.offset = 0;
919 info->var.blue.offset = 0;
920 info->var.red.length = 8; /* 8bit DAC */
921 info->var.green.length = 8;
922 info->var.blue.length = 8;
923 info->var.transp.offset = 0;
924 info->var.transp.length = 0;
925 break;
926 case 15:
927 info->var.red.offset = 10;
928 info->var.green.offset = 5;
929 info->var.blue.offset = 0;
930 info->var.red.length = 5;
931 info->var.green.length = 5;
932 info->var.blue.length = 5;
933 info->var.transp.offset = 15;
934 info->var.transp.length = 1;
935 break;
936 case 16:
937 info->var.red.offset = 11;
938 info->var.green.offset = 5;
939 info->var.blue.offset = 0;
940 info->var.red.length = 5;
941 info->var.green.length = 6;
942 info->var.blue.length = 5;
943 info->var.transp.offset = 0;
944 break;
945 case 24:
946 info->var.red.offset = 16;
947 info->var.green.offset = 8;
948 info->var.blue.offset = 0;
949 info->var.red.length = 8;
950 info->var.green.length = 8;
951 info->var.blue.length = 8;
952 info->var.transp.offset = 0;
953 info->var.transp.length = 0;
954 break;
955 case 32:
956 info->var.red.offset = 16;
957 info->var.green.offset = 8;
958 info->var.blue.offset = 0;
959 info->var.red.length = 8;
960 info->var.green.length = 8;
961 info->var.blue.length = 8;
962 info->var.transp.offset = 24;
963 info->var.transp.length = 8;
964 break;
965 default:
966 break;
967 }
968
969 info->var.xres = fb_width;
970 info->var.yres = fb_height;
971}
972EXPORT_SYMBOL(drm_fb_helper_fill_var);
38651674 973
0b4c0f3f
DA
974static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
975 uint32_t maxX,
976 uint32_t maxY)
38651674
DA
977{
978 struct drm_connector *connector;
979 int count = 0;
0b4c0f3f 980 int i;
38651674 981
0b4c0f3f
DA
982 for (i = 0; i < fb_helper->connector_count; i++) {
983 connector = fb_helper->connector_info[i]->connector;
38651674
DA
984 count += connector->funcs->fill_modes(connector, maxX, maxY);
985 }
986
987 return count;
988}
989
0b4c0f3f 990static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
38651674
DA
991{
992 struct drm_display_mode *mode;
993
0b4c0f3f 994 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
38651674
DA
995 if (drm_mode_width(mode) > width ||
996 drm_mode_height(mode) > height)
997 continue;
998 if (mode->type & DRM_MODE_TYPE_PREFERRED)
999 return mode;
1000 }
1001 return NULL;
1002}
1003
0b4c0f3f 1004static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
38651674 1005{
1794d257 1006 struct drm_cmdline_mode *cmdline_mode;
0b4c0f3f 1007 cmdline_mode = &fb_connector->cmdline_mode;
38651674
DA
1008 return cmdline_mode->specified;
1009}
1010
0b4c0f3f
DA
1011static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1012 int width, int height)
38651674 1013{
1794d257 1014 struct drm_cmdline_mode *cmdline_mode;
38651674
DA
1015 struct drm_display_mode *mode = NULL;
1016
0b4c0f3f 1017 cmdline_mode = &fb_helper_conn->cmdline_mode;
38651674
DA
1018 if (cmdline_mode->specified == false)
1019 return mode;
1020
1021 /* attempt to find a matching mode in the list of modes
1022 * we have gotten so far, if not add a CVT mode that conforms
1023 */
1024 if (cmdline_mode->rb || cmdline_mode->margins)
1025 goto create_mode;
1026
0b4c0f3f 1027 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
38651674
DA
1028 /* check width/height */
1029 if (mode->hdisplay != cmdline_mode->xres ||
1030 mode->vdisplay != cmdline_mode->yres)
1031 continue;
1032
1033 if (cmdline_mode->refresh_specified) {
1034 if (mode->vrefresh != cmdline_mode->refresh)
1035 continue;
1036 }
1037
1038 if (cmdline_mode->interlace) {
1039 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1040 continue;
1041 }
1042 return mode;
1043 }
1044
1045create_mode:
1794d257
CW
1046 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1047 cmdline_mode);
0b4c0f3f 1048 list_add(&mode->head, &fb_helper_conn->connector->modes);
38651674
DA
1049 return mode;
1050}
1051
1052static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1053{
1054 bool enable;
1055
96081cdf 1056 if (strict)
38651674 1057 enable = connector->status == connector_status_connected;
96081cdf 1058 else
38651674 1059 enable = connector->status != connector_status_disconnected;
96081cdf 1060
38651674
DA
1061 return enable;
1062}
1063
0b4c0f3f
DA
1064static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1065 bool *enabled)
38651674
DA
1066{
1067 bool any_enabled = false;
1068 struct drm_connector *connector;
1069 int i = 0;
1070
0b4c0f3f
DA
1071 for (i = 0; i < fb_helper->connector_count; i++) {
1072 connector = fb_helper->connector_info[i]->connector;
38651674
DA
1073 enabled[i] = drm_connector_enabled(connector, true);
1074 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1075 enabled[i] ? "yes" : "no");
1076 any_enabled |= enabled[i];
38651674
DA
1077 }
1078
1079 if (any_enabled)
1080 return;
1081
0b4c0f3f
DA
1082 for (i = 0; i < fb_helper->connector_count; i++) {
1083 connector = fb_helper->connector_info[i]->connector;
38651674 1084 enabled[i] = drm_connector_enabled(connector, false);
38651674
DA
1085 }
1086}
1087
1d42bbc8
DA
1088static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1089 struct drm_display_mode **modes,
1090 bool *enabled, int width, int height)
1091{
1092 int count, i, j;
1093 bool can_clone = false;
1094 struct drm_fb_helper_connector *fb_helper_conn;
1095 struct drm_display_mode *dmt_mode, *mode;
1096
1097 /* only contemplate cloning in the single crtc case */
1098 if (fb_helper->crtc_count > 1)
1099 return false;
1100
1101 count = 0;
1102 for (i = 0; i < fb_helper->connector_count; i++) {
1103 if (enabled[i])
1104 count++;
1105 }
1106
1107 /* only contemplate cloning if more than one connector is enabled */
1108 if (count <= 1)
1109 return false;
1110
1111 /* check the command line or if nothing common pick 1024x768 */
1112 can_clone = true;
1113 for (i = 0; i < fb_helper->connector_count; i++) {
1114 if (!enabled[i])
1115 continue;
1116 fb_helper_conn = fb_helper->connector_info[i];
1117 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1118 if (!modes[i]) {
1119 can_clone = false;
1120 break;
1121 }
1122 for (j = 0; j < i; j++) {
1123 if (!enabled[j])
1124 continue;
1125 if (!drm_mode_equal(modes[j], modes[i]))
1126 can_clone = false;
1127 }
1128 }
1129
1130 if (can_clone) {
1131 DRM_DEBUG_KMS("can clone using command line\n");
1132 return true;
1133 }
1134
1135 /* try and find a 1024x768 mode on each connector */
1136 can_clone = true;
f6e252ba 1137 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1d42bbc8
DA
1138
1139 for (i = 0; i < fb_helper->connector_count; i++) {
1140
1141 if (!enabled[i])
1142 continue;
1143
1144 fb_helper_conn = fb_helper->connector_info[i];
1145 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1146 if (drm_mode_equal(mode, dmt_mode))
1147 modes[i] = mode;
1148 }
1149 if (!modes[i])
1150 can_clone = false;
1151 }
1152
1153 if (can_clone) {
1154 DRM_DEBUG_KMS("can clone using 1024x768\n");
1155 return true;
1156 }
1157 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1158 return false;
1159}
1160
0b4c0f3f 1161static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
38651674
DA
1162 struct drm_display_mode **modes,
1163 bool *enabled, int width, int height)
1164{
0b4c0f3f
DA
1165 struct drm_fb_helper_connector *fb_helper_conn;
1166 int i;
38651674 1167
0b4c0f3f
DA
1168 for (i = 0; i < fb_helper->connector_count; i++) {
1169 fb_helper_conn = fb_helper->connector_info[i];
38651674 1170
0b4c0f3f 1171 if (enabled[i] == false)
38651674 1172 continue;
38651674
DA
1173
1174 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
0b4c0f3f 1175 fb_helper_conn->connector->base.id);
38651674
DA
1176
1177 /* got for command line mode first */
0b4c0f3f 1178 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
38651674
DA
1179 if (!modes[i]) {
1180 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
0b4c0f3f
DA
1181 fb_helper_conn->connector->base.id);
1182 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
38651674
DA
1183 }
1184 /* No preferred modes, pick one off the list */
0b4c0f3f
DA
1185 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1186 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
38651674
DA
1187 break;
1188 }
1189 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1190 "none");
38651674
DA
1191 }
1192 return true;
1193}
1194
8be48d92
DA
1195static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1196 struct drm_fb_helper_crtc **best_crtcs,
38651674
DA
1197 struct drm_display_mode **modes,
1198 int n, int width, int height)
1199{
1200 int c, o;
8be48d92 1201 struct drm_device *dev = fb_helper->dev;
38651674
DA
1202 struct drm_connector *connector;
1203 struct drm_connector_helper_funcs *connector_funcs;
1204 struct drm_encoder *encoder;
8be48d92 1205 struct drm_fb_helper_crtc *best_crtc;
38651674 1206 int my_score, best_score, score;
8be48d92 1207 struct drm_fb_helper_crtc **crtcs, *crtc;
0b4c0f3f 1208 struct drm_fb_helper_connector *fb_helper_conn;
38651674 1209
0b4c0f3f 1210 if (n == fb_helper->connector_count)
38651674 1211 return 0;
0b4c0f3f
DA
1212
1213 fb_helper_conn = fb_helper->connector_info[n];
1214 connector = fb_helper_conn->connector;
38651674
DA
1215
1216 best_crtcs[n] = NULL;
1217 best_crtc = NULL;
8be48d92 1218 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
38651674
DA
1219 if (modes[n] == NULL)
1220 return best_score;
1221
8be48d92
DA
1222 crtcs = kzalloc(dev->mode_config.num_connector *
1223 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1224 if (!crtcs)
1225 return best_score;
1226
1227 my_score = 1;
1228 if (connector->status == connector_status_connected)
1229 my_score++;
0b4c0f3f 1230 if (drm_has_cmdline_mode(fb_helper_conn))
38651674 1231 my_score++;
0b4c0f3f 1232 if (drm_has_preferred_mode(fb_helper_conn, width, height))
38651674
DA
1233 my_score++;
1234
1235 connector_funcs = connector->helper_private;
1236 encoder = connector_funcs->best_encoder(connector);
1237 if (!encoder)
1238 goto out;
1239
38651674
DA
1240 /* select a crtc for this connector and then attempt to configure
1241 remaining connectors */
8be48d92
DA
1242 for (c = 0; c < fb_helper->crtc_count; c++) {
1243 crtc = &fb_helper->crtc_info[c];
38651674 1244
96081cdf 1245 if ((encoder->possible_crtcs & (1 << c)) == 0)
38651674 1246 continue;
38651674
DA
1247
1248 for (o = 0; o < n; o++)
1249 if (best_crtcs[o] == crtc)
1250 break;
1251
1252 if (o < n) {
1d42bbc8
DA
1253 /* ignore cloning unless only a single crtc */
1254 if (fb_helper->crtc_count > 1)
1255 continue;
1256
1257 if (!drm_mode_equal(modes[o], modes[n]))
1258 continue;
38651674
DA
1259 }
1260
1261 crtcs[n] = crtc;
8be48d92
DA
1262 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1263 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
38651674
DA
1264 width, height);
1265 if (score > best_score) {
1266 best_crtc = crtc;
1267 best_score = score;
1268 memcpy(best_crtcs, crtcs,
1269 dev->mode_config.num_connector *
8be48d92 1270 sizeof(struct drm_fb_helper_crtc *));
38651674 1271 }
38651674
DA
1272 }
1273out:
1274 kfree(crtcs);
1275 return best_score;
1276}
1277
8be48d92 1278static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
38651674 1279{
8be48d92
DA
1280 struct drm_device *dev = fb_helper->dev;
1281 struct drm_fb_helper_crtc **crtcs;
38651674 1282 struct drm_display_mode **modes;
8be48d92 1283 struct drm_mode_set *modeset;
38651674
DA
1284 bool *enabled;
1285 int width, height;
1286 int i, ret;
1287
1288 DRM_DEBUG_KMS("\n");
1289
1290 width = dev->mode_config.max_width;
1291 height = dev->mode_config.max_height;
1292
38651674 1293 crtcs = kcalloc(dev->mode_config.num_connector,
8be48d92 1294 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1295 modes = kcalloc(dev->mode_config.num_connector,
1296 sizeof(struct drm_display_mode *), GFP_KERNEL);
1297 enabled = kcalloc(dev->mode_config.num_connector,
1298 sizeof(bool), GFP_KERNEL);
8c5eaca0
SK
1299 if (!crtcs || !modes || !enabled) {
1300 DRM_ERROR("Memory allocation failed\n");
1301 goto out;
1302 }
1303
38651674 1304
0b4c0f3f 1305 drm_enable_connectors(fb_helper, enabled);
38651674 1306
1d42bbc8
DA
1307 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1308 if (!ret) {
1309 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1310 if (!ret)
1311 DRM_ERROR("Unable to find initial modes\n");
1312 }
38651674
DA
1313
1314 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1315
8be48d92
DA
1316 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1317
1318 /* need to set the modesets up here for use later */
1319 /* fill out the connector<->crtc mappings into the modesets */
1320 for (i = 0; i < fb_helper->crtc_count; i++) {
1321 modeset = &fb_helper->crtc_info[i].mode_set;
1322 modeset->num_connectors = 0;
7e53f3a4 1323 modeset->fb = NULL;
8be48d92 1324 }
38651674 1325
0b4c0f3f 1326 for (i = 0; i < fb_helper->connector_count; i++) {
38651674 1327 struct drm_display_mode *mode = modes[i];
8be48d92
DA
1328 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1329 modeset = &fb_crtc->mode_set;
38651674 1330
8be48d92 1331 if (mode && fb_crtc) {
38651674 1332 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
8be48d92
DA
1333 mode->name, fb_crtc->mode_set.crtc->base.id);
1334 fb_crtc->desired_mode = mode;
1335 if (modeset->mode)
1336 drm_mode_destroy(dev, modeset->mode);
1337 modeset->mode = drm_mode_duplicate(dev,
1338 fb_crtc->desired_mode);
0b4c0f3f 1339 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
7e53f3a4 1340 modeset->fb = fb_helper->fb;
38651674 1341 }
38651674
DA
1342 }
1343
7e53f3a4
DV
1344 /* Clear out any old modes if there are no more connected outputs. */
1345 for (i = 0; i < fb_helper->crtc_count; i++) {
1346 modeset = &fb_helper->crtc_info[i].mode_set;
1347 if (modeset->num_connectors == 0) {
1348 BUG_ON(modeset->fb);
1349 BUG_ON(modeset->num_connectors);
1350 if (modeset->mode)
1351 drm_mode_destroy(dev, modeset->mode);
1352 modeset->mode = NULL;
1353 }
1354 }
8c5eaca0 1355out:
38651674
DA
1356 kfree(crtcs);
1357 kfree(modes);
1358 kfree(enabled);
1359}
1360
1361/**
1362 * drm_helper_initial_config - setup a sane initial connector configuration
d0ddc033
DV
1363 * @fb_helper: fb_helper device struct
1364 * @bpp_sel: bpp value to use for the framebuffer configuration
38651674
DA
1365 *
1366 * LOCKING:
d0ddc033
DV
1367 * Called at init time by the driver to set up the @fb_helper initial
1368 * configuration, must take the mode config lock.
38651674 1369 *
d0ddc033 1370 * Scans the CRTCs and connectors and tries to put together an initial setup.
38651674
DA
1371 * At the moment, this is a cloned configuration across all heads with
1372 * a new framebuffer object as the backing store.
1373 *
1374 * RETURNS:
1375 * Zero if everything went ok, nonzero otherwise.
1376 */
4abe3520 1377bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
38651674 1378{
8be48d92 1379 struct drm_device *dev = fb_helper->dev;
38651674
DA
1380 int count = 0;
1381
0b4c0f3f 1382 drm_fb_helper_parse_command_line(fb_helper);
38651674 1383
0b4c0f3f
DA
1384 count = drm_fb_helper_probe_connector_modes(fb_helper,
1385 dev->mode_config.max_width,
1386 dev->mode_config.max_height);
38651674
DA
1387 /*
1388 * we shouldn't end up with no modes here.
1389 */
96081cdf 1390 if (count == 0)
d56b1b9d 1391 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
96081cdf 1392
8be48d92 1393 drm_setup_crtcs(fb_helper);
38651674 1394
4abe3520 1395 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
38651674 1396}
8be48d92 1397EXPORT_SYMBOL(drm_fb_helper_initial_config);
38651674 1398
7394371d
CW
1399/**
1400 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
d0ddc033 1401 * probing all the outputs attached to the fb
7394371d
CW
1402 * @fb_helper: the drm_fb_helper
1403 *
1404 * LOCKING:
1405 * Called at runtime, must take mode config lock.
1406 *
1407 * Scan the connectors attached to the fb_helper and try to put together a
1408 * setup after *notification of a change in output configuration.
1409 *
1410 * RETURNS:
1411 * 0 on success and a non-zero error code otherwise.
1412 */
1413int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
38651674 1414{
7394371d 1415 struct drm_device *dev = fb_helper->dev;
5c4426a7 1416 int count = 0;
4abe3520 1417 u32 max_width, max_height, bpp_sel;
5c4426a7 1418
eb1f8e4f 1419 if (!fb_helper->fb)
7394371d 1420 return 0;
4abe3520 1421
84849903 1422 drm_modeset_lock_all(dev);
20c60c35 1423 if (!drm_fb_helper_is_bound(fb_helper)) {
4abe3520 1424 fb_helper->delayed_hotplug = true;
84849903 1425 drm_modeset_unlock_all(dev);
7394371d 1426 return 0;
4abe3520 1427 }
eb1f8e4f 1428 DRM_DEBUG_KMS("\n");
4abe3520 1429
eb1f8e4f
DA
1430 max_width = fb_helper->fb->width;
1431 max_height = fb_helper->fb->height;
1432 bpp_sel = fb_helper->fb->bits_per_pixel;
5c4426a7 1433
eb1f8e4f
DA
1434 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1435 max_height);
1436 drm_setup_crtcs(fb_helper);
84849903 1437 drm_modeset_unlock_all(dev);
4abe3520 1438
eb1f8e4f 1439 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
5c4426a7 1440}
eb1f8e4f 1441EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
5c4426a7 1442
6a108a14 1443/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
3ce05168
DF
1444 * but the module doesn't depend on any fb console symbols. At least
1445 * attempt to load fbcon to avoid leaving the system without a usable console.
1446 */
6a108a14 1447#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
3ce05168
DF
1448static int __init drm_fb_helper_modinit(void)
1449{
1450 const char *name = "fbcon";
1451 struct module *fbcon;
1452
1453 mutex_lock(&module_mutex);
1454 fbcon = find_module(name);
1455 mutex_unlock(&module_mutex);
1456
1457 if (!fbcon)
1458 request_module_nowait(name);
1459 return 0;
1460}
1461
1462module_init(drm_fb_helper_modinit);
1463#endif