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