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