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