]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpu/drm/drm_fb_helper.c
drm: move dereference below check
[net-next-2.6.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
785b93ef
DA
245bool drm_fb_helper_force_kernel_mode(void)
246{
247 int i = 0;
248 bool ret, error = false;
249 struct drm_fb_helper *helper;
250
251 if (list_empty(&kernel_fb_helper_list))
252 return false;
253
254 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
255 for (i = 0; i < helper->crtc_count; i++) {
256 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
257 ret = drm_crtc_helper_set_config(mode_set);
258 if (ret)
259 error = true;
260 }
261 }
262 return error;
263}
264
265int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
266 void *panic_str)
267{
fc2362af 268 printk(KERN_ERR "panic occurred, switching back to text console\n");
785b93ef
DA
269 return drm_fb_helper_force_kernel_mode();
270 return 0;
271}
272EXPORT_SYMBOL(drm_fb_helper_panic);
273
274static struct notifier_block paniced = {
275 .notifier_call = drm_fb_helper_panic,
276};
277
278/**
279 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
280 *
281 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
282 */
283void drm_fb_helper_restore(void)
284{
285 bool ret;
286 ret = drm_fb_helper_force_kernel_mode();
287 if (ret == true)
288 DRM_ERROR("Failed to restore crtc configuration\n");
289}
290EXPORT_SYMBOL(drm_fb_helper_restore);
291
bea1d35b 292#ifdef CONFIG_MAGIC_SYSRQ
785b93ef
DA
293static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
294{
295 drm_fb_helper_restore();
296}
297static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
298
299static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
300{
301 schedule_work(&drm_fb_helper_restore_work);
302}
303
304static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
305 .handler = drm_fb_helper_sysrq,
306 .help_msg = "force-fb(V)",
307 .action_msg = "Restore framebuffer console",
308};
b8c40d62
RD
309#else
310static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
bea1d35b 311#endif
785b93ef
DA
312
313static void drm_fb_helper_on(struct fb_info *info)
314{
315 struct drm_fb_helper *fb_helper = info->par;
316 struct drm_device *dev = fb_helper->dev;
317 struct drm_crtc *crtc;
8be48d92 318 struct drm_crtc_helper_funcs *crtc_funcs;
023eb571 319 struct drm_connector *connector;
785b93ef 320 struct drm_encoder *encoder;
023eb571 321 int i, j;
785b93ef
DA
322
323 /*
324 * For each CRTC in this fb, turn the crtc on then,
325 * find all associated encoders and turn them on.
326 */
8be48d92 327 mutex_lock(&dev->mode_config.mutex);
e87b2c42 328 for (i = 0; i < fb_helper->crtc_count; i++) {
8be48d92
DA
329 crtc = fb_helper->crtc_info[i].mode_set.crtc;
330 crtc_funcs = crtc->helper_private;
785b93ef 331
8be48d92
DA
332 if (!crtc->enabled)
333 continue;
334
335 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
785b93ef 336
023eb571
JB
337 /* Walk the connectors & encoders on this fb turning them on */
338 for (j = 0; j < fb_helper->connector_count; j++) {
339 connector = fb_helper->connector_info[j]->connector;
340 connector->dpms = DRM_MODE_DPMS_ON;
341 drm_connector_property_set_value(connector,
342 dev->mode_config.dpms_property,
343 DRM_MODE_DPMS_ON);
344 }
8be48d92
DA
345 /* Found a CRTC on this fb, now find encoders */
346 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
347 if (encoder->crtc == crtc) {
348 struct drm_encoder_helper_funcs *encoder_funcs;
785b93ef 349
8be48d92
DA
350 encoder_funcs = encoder->helper_private;
351 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
785b93ef
DA
352 }
353 }
354 }
8be48d92 355 mutex_unlock(&dev->mode_config.mutex);
785b93ef
DA
356}
357
358static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
359{
360 struct drm_fb_helper *fb_helper = info->par;
361 struct drm_device *dev = fb_helper->dev;
362 struct drm_crtc *crtc;
8be48d92 363 struct drm_crtc_helper_funcs *crtc_funcs;
023eb571 364 struct drm_connector *connector;
785b93ef 365 struct drm_encoder *encoder;
023eb571 366 int i, j;
785b93ef
DA
367
368 /*
369 * For each CRTC in this fb, find all associated encoders
370 * and turn them off, then turn off the CRTC.
371 */
8be48d92 372 mutex_lock(&dev->mode_config.mutex);
e87b2c42 373 for (i = 0; i < fb_helper->crtc_count; i++) {
8be48d92
DA
374 crtc = fb_helper->crtc_info[i].mode_set.crtc;
375 crtc_funcs = crtc->helper_private;
e87b2c42 376
8be48d92
DA
377 if (!crtc->enabled)
378 continue;
e87b2c42 379
023eb571
JB
380 /* Walk the connectors on this fb and mark them off */
381 for (j = 0; j < fb_helper->connector_count; j++) {
382 connector = fb_helper->connector_info[j]->connector;
383 connector->dpms = dpms_mode;
384 drm_connector_property_set_value(connector,
385 dev->mode_config.dpms_property,
386 dpms_mode);
387 }
8be48d92
DA
388 /* Found a CRTC on this fb, now find encoders */
389 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
390 if (encoder->crtc == crtc) {
391 struct drm_encoder_helper_funcs *encoder_funcs;
e87b2c42 392
8be48d92
DA
393 encoder_funcs = encoder->helper_private;
394 encoder_funcs->dpms(encoder, dpms_mode);
e87b2c42 395 }
785b93ef 396 }
8be48d92 397 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
785b93ef 398 }
8be48d92 399 mutex_unlock(&dev->mode_config.mutex);
785b93ef
DA
400}
401
402int drm_fb_helper_blank(int blank, struct fb_info *info)
403{
404 switch (blank) {
731b5a15 405 /* Display: On; HSync: On, VSync: On */
785b93ef
DA
406 case FB_BLANK_UNBLANK:
407 drm_fb_helper_on(info);
408 break;
731b5a15 409 /* Display: Off; HSync: On, VSync: On */
785b93ef 410 case FB_BLANK_NORMAL:
5fd4df4d 411 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
785b93ef 412 break;
731b5a15 413 /* Display: Off; HSync: Off, VSync: On */
785b93ef
DA
414 case FB_BLANK_HSYNC_SUSPEND:
415 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
416 break;
731b5a15 417 /* Display: Off; HSync: On, VSync: Off */
785b93ef
DA
418 case FB_BLANK_VSYNC_SUSPEND:
419 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
420 break;
731b5a15 421 /* Display: Off; HSync: Off, VSync: Off */
785b93ef
DA
422 case FB_BLANK_POWERDOWN:
423 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
424 break;
425 }
426 return 0;
427}
428EXPORT_SYMBOL(drm_fb_helper_blank);
429
430static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
431{
432 int i;
433
0b4c0f3f
DA
434 for (i = 0; i < helper->connector_count; i++)
435 kfree(helper->connector_info[i]);
436 kfree(helper->connector_info);
785b93ef
DA
437 for (i = 0; i < helper->crtc_count; i++)
438 kfree(helper->crtc_info[i].mode_set.connectors);
439 kfree(helper->crtc_info);
440}
441
4abe3520
DA
442int drm_fb_helper_init(struct drm_device *dev,
443 struct drm_fb_helper *fb_helper,
eb1f8e4f 444 int crtc_count, int max_conn_count)
785b93ef 445{
785b93ef
DA
446 struct drm_crtc *crtc;
447 int ret = 0;
448 int i;
449
4abe3520 450 fb_helper->dev = dev;
4abe3520
DA
451
452 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
453
454 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
455 if (!fb_helper->crtc_info)
785b93ef
DA
456 return -ENOMEM;
457
4abe3520
DA
458 fb_helper->crtc_count = crtc_count;
459 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
460 if (!fb_helper->connector_info) {
461 kfree(fb_helper->crtc_info);
0b4c0f3f
DA
462 return -ENOMEM;
463 }
4abe3520 464 fb_helper->connector_count = 0;
785b93ef
DA
465
466 for (i = 0; i < crtc_count; i++) {
4abe3520 467 fb_helper->crtc_info[i].mode_set.connectors =
785b93ef
DA
468 kcalloc(max_conn_count,
469 sizeof(struct drm_connector *),
470 GFP_KERNEL);
471
4abe3520 472 if (!fb_helper->crtc_info[i].mode_set.connectors) {
785b93ef
DA
473 ret = -ENOMEM;
474 goto out_free;
475 }
4abe3520 476 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
785b93ef
DA
477 }
478
479 i = 0;
480 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4abe3520
DA
481 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
482 fb_helper->crtc_info[i].mode_set.crtc = crtc;
785b93ef
DA
483 i++;
484 }
4abe3520 485 fb_helper->conn_limit = max_conn_count;
785b93ef
DA
486 return 0;
487out_free:
4abe3520 488 drm_fb_helper_crtc_free(fb_helper);
785b93ef
DA
489 return -ENOMEM;
490}
4abe3520
DA
491EXPORT_SYMBOL(drm_fb_helper_init);
492
493void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
494{
495 if (!list_empty(&fb_helper->kernel_fb_list)) {
496 list_del(&fb_helper->kernel_fb_list);
497 if (list_empty(&kernel_fb_helper_list)) {
3da1f33e 498 printk(KERN_INFO "drm: unregistered panic notifier\n");
4abe3520
DA
499 atomic_notifier_chain_unregister(&panic_notifier_list,
500 &paniced);
501 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
502 }
503 }
504
505 drm_fb_helper_crtc_free(fb_helper);
506
4abe3520
DA
507}
508EXPORT_SYMBOL(drm_fb_helper_fini);
785b93ef 509
c850cb78 510static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
b8c00ac5
DA
511 u16 blue, u16 regno, struct fb_info *info)
512{
513 struct drm_fb_helper *fb_helper = info->par;
514 struct drm_framebuffer *fb = fb_helper->fb;
515 int pindex;
516
c850cb78
DA
517 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
518 u32 *palette;
519 u32 value;
520 /* place color in psuedopalette */
521 if (regno > 16)
522 return -EINVAL;
523 palette = (u32 *)info->pseudo_palette;
524 red >>= (16 - info->var.red.length);
525 green >>= (16 - info->var.green.length);
526 blue >>= (16 - info->var.blue.length);
527 value = (red << info->var.red.offset) |
528 (green << info->var.green.offset) |
529 (blue << info->var.blue.offset);
530 palette[regno] = value;
531 return 0;
532 }
533
b8c00ac5
DA
534 pindex = regno;
535
536 if (fb->bits_per_pixel == 16) {
537 pindex = regno << 3;
538
539 if (fb->depth == 16 && regno > 63)
c850cb78 540 return -EINVAL;
b8c00ac5 541 if (fb->depth == 15 && regno > 31)
c850cb78 542 return -EINVAL;
b8c00ac5
DA
543
544 if (fb->depth == 16) {
545 u16 r, g, b;
546 int i;
547 if (regno < 32) {
548 for (i = 0; i < 8; i++)
549 fb_helper->funcs->gamma_set(crtc, red,
550 green, blue, pindex + i);
551 }
552
553 fb_helper->funcs->gamma_get(crtc, &r,
554 &g, &b,
555 pindex >> 1);
556
557 for (i = 0; i < 4; i++)
558 fb_helper->funcs->gamma_set(crtc, r,
559 green, b,
560 (pindex >> 1) + i);
561 }
562 }
563
564 if (fb->depth != 16)
565 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
c850cb78 566 return 0;
b8c00ac5
DA
567}
568
068143d3
DA
569int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
570{
571 struct drm_fb_helper *fb_helper = info->par;
8be48d92 572 struct drm_crtc_helper_funcs *crtc_funcs;
068143d3
DA
573 u16 *red, *green, *blue, *transp;
574 struct drm_crtc *crtc;
575 int i, rc = 0;
576 int start;
577
8be48d92
DA
578 for (i = 0; i < fb_helper->crtc_count; i++) {
579 crtc = fb_helper->crtc_info[i].mode_set.crtc;
580 crtc_funcs = crtc->helper_private;
068143d3
DA
581
582 red = cmap->red;
583 green = cmap->green;
584 blue = cmap->blue;
585 transp = cmap->transp;
586 start = cmap->start;
587
588 for (i = 0; i < cmap->len; i++) {
589 u16 hred, hgreen, hblue, htransp = 0xffff;
590
591 hred = *red++;
592 hgreen = *green++;
593 hblue = *blue++;
594
595 if (transp)
596 htransp = *transp++;
597
c850cb78
DA
598 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
599 if (rc)
600 return rc;
068143d3
DA
601 }
602 crtc_funcs->load_lut(crtc);
603 }
604 return rc;
605}
606EXPORT_SYMBOL(drm_fb_helper_setcmap);
607
785b93ef
DA
608int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
609 struct fb_info *info)
610{
611 struct drm_fb_helper *fb_helper = info->par;
612 struct drm_framebuffer *fb = fb_helper->fb;
613 int depth;
614
5349ef31 615 if (var->pixclock != 0)
785b93ef
DA
616 return -EINVAL;
617
618 /* Need to resize the fb object !!! */
509c7d83
DA
619 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
620 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
621 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
622 fb->width, fb->height, fb->bits_per_pixel);
785b93ef
DA
623 return -EINVAL;
624 }
625
626 switch (var->bits_per_pixel) {
627 case 16:
628 depth = (var->green.length == 6) ? 16 : 15;
629 break;
630 case 32:
631 depth = (var->transp.length > 0) ? 32 : 24;
632 break;
633 default:
634 depth = var->bits_per_pixel;
635 break;
636 }
637
638 switch (depth) {
639 case 8:
640 var->red.offset = 0;
641 var->green.offset = 0;
642 var->blue.offset = 0;
643 var->red.length = 8;
644 var->green.length = 8;
645 var->blue.length = 8;
646 var->transp.length = 0;
647 var->transp.offset = 0;
648 break;
649 case 15:
650 var->red.offset = 10;
651 var->green.offset = 5;
652 var->blue.offset = 0;
653 var->red.length = 5;
654 var->green.length = 5;
655 var->blue.length = 5;
656 var->transp.length = 1;
657 var->transp.offset = 15;
658 break;
659 case 16:
660 var->red.offset = 11;
661 var->green.offset = 5;
662 var->blue.offset = 0;
663 var->red.length = 5;
664 var->green.length = 6;
665 var->blue.length = 5;
666 var->transp.length = 0;
667 var->transp.offset = 0;
668 break;
669 case 24:
670 var->red.offset = 16;
671 var->green.offset = 8;
672 var->blue.offset = 0;
673 var->red.length = 8;
674 var->green.length = 8;
675 var->blue.length = 8;
676 var->transp.length = 0;
677 var->transp.offset = 0;
678 break;
679 case 32:
680 var->red.offset = 16;
681 var->green.offset = 8;
682 var->blue.offset = 0;
683 var->red.length = 8;
684 var->green.length = 8;
685 var->blue.length = 8;
686 var->transp.length = 8;
687 var->transp.offset = 24;
688 break;
689 default:
690 return -EINVAL;
691 }
692 return 0;
693}
694EXPORT_SYMBOL(drm_fb_helper_check_var);
695
696/* this will let fbcon do the mode init */
697int drm_fb_helper_set_par(struct fb_info *info)
698{
699 struct drm_fb_helper *fb_helper = info->par;
700 struct drm_device *dev = fb_helper->dev;
701 struct fb_var_screeninfo *var = &info->var;
702 struct drm_crtc *crtc;
703 int ret;
704 int i;
705
5349ef31 706 if (var->pixclock != 0) {
172e91f5 707 DRM_ERROR("PIXEL CLOCK SET\n");
785b93ef
DA
708 return -EINVAL;
709 }
710
8be48d92
DA
711 mutex_lock(&dev->mode_config.mutex);
712 for (i = 0; i < fb_helper->crtc_count; i++) {
713 crtc = fb_helper->crtc_info[i].mode_set.crtc;
0b4c0f3f
DA
714 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
715 if (ret) {
785b93ef 716 mutex_unlock(&dev->mode_config.mutex);
0b4c0f3f 717 return ret;
785b93ef
DA
718 }
719 }
8be48d92 720 mutex_unlock(&dev->mode_config.mutex);
4abe3520
DA
721
722 if (fb_helper->delayed_hotplug) {
723 fb_helper->delayed_hotplug = false;
eb1f8e4f 724 drm_fb_helper_hotplug_event(fb_helper);
4abe3520 725 }
785b93ef
DA
726 return 0;
727}
728EXPORT_SYMBOL(drm_fb_helper_set_par);
729
730int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
731 struct fb_info *info)
732{
733 struct drm_fb_helper *fb_helper = info->par;
734 struct drm_device *dev = fb_helper->dev;
735 struct drm_mode_set *modeset;
736 struct drm_crtc *crtc;
737 int ret = 0;
738 int i;
739
8be48d92
DA
740 mutex_lock(&dev->mode_config.mutex);
741 for (i = 0; i < fb_helper->crtc_count; i++) {
742 crtc = fb_helper->crtc_info[i].mode_set.crtc;
785b93ef
DA
743
744 modeset = &fb_helper->crtc_info[i].mode_set;
745
746 modeset->x = var->xoffset;
747 modeset->y = var->yoffset;
748
749 if (modeset->num_connectors) {
785b93ef 750 ret = crtc->funcs->set_config(modeset);
785b93ef
DA
751 if (!ret) {
752 info->var.xoffset = var->xoffset;
753 info->var.yoffset = var->yoffset;
754 }
755 }
756 }
8be48d92 757 mutex_unlock(&dev->mode_config.mutex);
785b93ef
DA
758 return ret;
759}
760EXPORT_SYMBOL(drm_fb_helper_pan_display);
761
8be48d92
DA
762int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
763 int preferred_bpp)
785b93ef 764{
785b93ef
DA
765 int new_fb = 0;
766 int crtc_count = 0;
4abe3520 767 int i;
785b93ef 768 struct fb_info *info;
38651674 769 struct drm_fb_helper_surface_size sizes;
8be48d92 770 int gamma_size = 0;
38651674
DA
771
772 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
773 sizes.surface_depth = 24;
774 sizes.surface_bpp = 32;
775 sizes.fb_width = (unsigned)-1;
776 sizes.fb_height = (unsigned)-1;
785b93ef 777
b8c00ac5
DA
778 /* if driver picks 8 or 16 by default use that
779 for both depth/bpp */
38651674
DA
780 if (preferred_bpp != sizes.surface_bpp) {
781 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
b8c00ac5 782 }
785b93ef 783 /* first up get a count of crtcs now in use and new min/maxes width/heights */
0b4c0f3f
DA
784 for (i = 0; i < fb_helper->connector_count; i++) {
785 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
8ef8678c
DA
786 struct drm_fb_helper_cmdline_mode *cmdline_mode;
787
0b4c0f3f 788 cmdline_mode = &fb_helper_conn->cmdline_mode;
d50ba256
DA
789
790 if (cmdline_mode->bpp_specified) {
791 switch (cmdline_mode->bpp) {
792 case 8:
38651674 793 sizes.surface_depth = sizes.surface_bpp = 8;
d50ba256
DA
794 break;
795 case 15:
38651674
DA
796 sizes.surface_depth = 15;
797 sizes.surface_bpp = 16;
d50ba256
DA
798 break;
799 case 16:
38651674 800 sizes.surface_depth = sizes.surface_bpp = 16;
d50ba256
DA
801 break;
802 case 24:
38651674 803 sizes.surface_depth = sizes.surface_bpp = 24;
d50ba256
DA
804 break;
805 case 32:
38651674
DA
806 sizes.surface_depth = 24;
807 sizes.surface_bpp = 32;
d50ba256
DA
808 break;
809 }
810 break;
811 }
812 }
813
8be48d92
DA
814 crtc_count = 0;
815 for (i = 0; i < fb_helper->crtc_count; i++) {
816 struct drm_display_mode *desired_mode;
817 desired_mode = fb_helper->crtc_info[i].desired_mode;
818
819 if (desired_mode) {
820 if (gamma_size == 0)
821 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
822 if (desired_mode->hdisplay < sizes.fb_width)
823 sizes.fb_width = desired_mode->hdisplay;
824 if (desired_mode->vdisplay < sizes.fb_height)
825 sizes.fb_height = desired_mode->vdisplay;
826 if (desired_mode->hdisplay > sizes.surface_width)
827 sizes.surface_width = desired_mode->hdisplay;
828 if (desired_mode->vdisplay > sizes.surface_height)
829 sizes.surface_height = desired_mode->vdisplay;
785b93ef
DA
830 crtc_count++;
831 }
832 }
833
38651674 834 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
785b93ef
DA
835 /* hmm everyone went away - assume VGA cable just fell out
836 and will come back later. */
eb1f8e4f 837 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
19b4b445
DA
838 sizes.fb_width = sizes.surface_width = 1024;
839 sizes.fb_height = sizes.surface_height = 768;
785b93ef
DA
840 }
841
38651674 842 /* push down into drivers */
4abe3520 843 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
38651674
DA
844 if (new_fb < 0)
845 return new_fb;
785b93ef 846
38651674 847 info = fb_helper->fbdev;
785b93ef 848
8be48d92
DA
849 /* set the fb pointer */
850 for (i = 0; i < fb_helper->crtc_count; i++) {
851 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
785b93ef 852 }
785b93ef
DA
853
854 if (new_fb) {
5349ef31 855 info->var.pixclock = 0;
3bea21b6 856 if (register_framebuffer(info) < 0) {
785b93ef 857 return -EINVAL;
3bea21b6 858 }
38651674
DA
859
860 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
861 info->fix.id);
862
785b93ef
DA
863 } else {
864 drm_fb_helper_set_par(info);
865 }
785b93ef
DA
866
867 /* Switch back to kernel console on panic */
868 /* multi card linked list maybe */
869 if (list_empty(&kernel_fb_helper_list)) {
3da1f33e 870 printk(KERN_INFO "drm: registered panic notifier\n");
785b93ef
DA
871 atomic_notifier_chain_register(&panic_notifier_list,
872 &paniced);
873 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
874 }
38651674
DA
875 if (new_fb)
876 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
877
785b93ef
DA
878 return 0;
879}
880EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
881
068143d3
DA
882void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
883 uint32_t depth)
785b93ef
DA
884{
885 info->fix.type = FB_TYPE_PACKED_PIXELS;
068143d3 886 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
c850cb78 887 FB_VISUAL_TRUECOLOR;
785b93ef
DA
888 info->fix.type_aux = 0;
889 info->fix.xpanstep = 1; /* doing it in hw */
890 info->fix.ypanstep = 1; /* doing it in hw */
891 info->fix.ywrapstep = 0;
3420e742 892 info->fix.accel = FB_ACCEL_NONE;
785b93ef
DA
893 info->fix.type_aux = 0;
894
895 info->fix.line_length = pitch;
896 return;
897}
898EXPORT_SYMBOL(drm_fb_helper_fill_fix);
899
38651674 900void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
785b93ef
DA
901 uint32_t fb_width, uint32_t fb_height)
902{
38651674
DA
903 struct drm_framebuffer *fb = fb_helper->fb;
904 info->pseudo_palette = fb_helper->pseudo_palette;
785b93ef
DA
905 info->var.xres_virtual = fb->width;
906 info->var.yres_virtual = fb->height;
907 info->var.bits_per_pixel = fb->bits_per_pixel;
908 info->var.xoffset = 0;
909 info->var.yoffset = 0;
910 info->var.activate = FB_ACTIVATE_NOW;
911 info->var.height = -1;
912 info->var.width = -1;
913
914 switch (fb->depth) {
915 case 8:
916 info->var.red.offset = 0;
917 info->var.green.offset = 0;
918 info->var.blue.offset = 0;
919 info->var.red.length = 8; /* 8bit DAC */
920 info->var.green.length = 8;
921 info->var.blue.length = 8;
922 info->var.transp.offset = 0;
923 info->var.transp.length = 0;
924 break;
925 case 15:
926 info->var.red.offset = 10;
927 info->var.green.offset = 5;
928 info->var.blue.offset = 0;
929 info->var.red.length = 5;
930 info->var.green.length = 5;
931 info->var.blue.length = 5;
932 info->var.transp.offset = 15;
933 info->var.transp.length = 1;
934 break;
935 case 16:
936 info->var.red.offset = 11;
937 info->var.green.offset = 5;
938 info->var.blue.offset = 0;
939 info->var.red.length = 5;
940 info->var.green.length = 6;
941 info->var.blue.length = 5;
942 info->var.transp.offset = 0;
943 break;
944 case 24:
945 info->var.red.offset = 16;
946 info->var.green.offset = 8;
947 info->var.blue.offset = 0;
948 info->var.red.length = 8;
949 info->var.green.length = 8;
950 info->var.blue.length = 8;
951 info->var.transp.offset = 0;
952 info->var.transp.length = 0;
953 break;
954 case 32:
955 info->var.red.offset = 16;
956 info->var.green.offset = 8;
957 info->var.blue.offset = 0;
958 info->var.red.length = 8;
959 info->var.green.length = 8;
960 info->var.blue.length = 8;
961 info->var.transp.offset = 24;
962 info->var.transp.length = 8;
963 break;
964 default:
965 break;
966 }
967
968 info->var.xres = fb_width;
969 info->var.yres = fb_height;
970}
971EXPORT_SYMBOL(drm_fb_helper_fill_var);
38651674 972
0b4c0f3f
DA
973static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
974 uint32_t maxX,
975 uint32_t maxY)
38651674
DA
976{
977 struct drm_connector *connector;
978 int count = 0;
0b4c0f3f 979 int i;
38651674 980
0b4c0f3f
DA
981 for (i = 0; i < fb_helper->connector_count; i++) {
982 connector = fb_helper->connector_info[i]->connector;
38651674
DA
983 count += connector->funcs->fill_modes(connector, maxX, maxY);
984 }
985
986 return count;
987}
988
0b4c0f3f 989static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
38651674
DA
990{
991 struct drm_display_mode *mode;
992
0b4c0f3f 993 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
38651674
DA
994 if (drm_mode_width(mode) > width ||
995 drm_mode_height(mode) > height)
996 continue;
997 if (mode->type & DRM_MODE_TYPE_PREFERRED)
998 return mode;
999 }
1000 return NULL;
1001}
1002
0b4c0f3f 1003static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
38651674 1004{
38651674 1005 struct drm_fb_helper_cmdline_mode *cmdline_mode;
0b4c0f3f 1006 cmdline_mode = &fb_connector->cmdline_mode;
38651674
DA
1007 return cmdline_mode->specified;
1008}
1009
0b4c0f3f
DA
1010static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1011 int width, int height)
38651674 1012{
38651674
DA
1013 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1014 struct drm_display_mode *mode = NULL;
1015
0b4c0f3f 1016 cmdline_mode = &fb_helper_conn->cmdline_mode;
38651674
DA
1017 if (cmdline_mode->specified == false)
1018 return mode;
1019
1020 /* attempt to find a matching mode in the list of modes
1021 * we have gotten so far, if not add a CVT mode that conforms
1022 */
1023 if (cmdline_mode->rb || cmdline_mode->margins)
1024 goto create_mode;
1025
0b4c0f3f 1026 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
38651674
DA
1027 /* check width/height */
1028 if (mode->hdisplay != cmdline_mode->xres ||
1029 mode->vdisplay != cmdline_mode->yres)
1030 continue;
1031
1032 if (cmdline_mode->refresh_specified) {
1033 if (mode->vrefresh != cmdline_mode->refresh)
1034 continue;
1035 }
1036
1037 if (cmdline_mode->interlace) {
1038 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1039 continue;
1040 }
1041 return mode;
1042 }
1043
1044create_mode:
b829e011
AJ
1045 if (cmdline_mode->cvt)
1046 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1047 cmdline_mode->xres, cmdline_mode->yres,
1048 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1049 cmdline_mode->rb, cmdline_mode->interlace,
1050 cmdline_mode->margins);
1051 else
1052 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1053 cmdline_mode->xres, cmdline_mode->yres,
1054 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1055 cmdline_mode->interlace,
1056 cmdline_mode->margins);
38651674 1057 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
0b4c0f3f 1058 list_add(&mode->head, &fb_helper_conn->connector->modes);
38651674
DA
1059 return mode;
1060}
1061
1062static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1063{
1064 bool enable;
1065
1066 if (strict) {
1067 enable = connector->status == connector_status_connected;
1068 } else {
1069 enable = connector->status != connector_status_disconnected;
1070 }
1071 return enable;
1072}
1073
0b4c0f3f
DA
1074static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1075 bool *enabled)
38651674
DA
1076{
1077 bool any_enabled = false;
1078 struct drm_connector *connector;
1079 int i = 0;
1080
0b4c0f3f
DA
1081 for (i = 0; i < fb_helper->connector_count; i++) {
1082 connector = fb_helper->connector_info[i]->connector;
38651674
DA
1083 enabled[i] = drm_connector_enabled(connector, true);
1084 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1085 enabled[i] ? "yes" : "no");
1086 any_enabled |= enabled[i];
38651674
DA
1087 }
1088
1089 if (any_enabled)
1090 return;
1091
0b4c0f3f
DA
1092 for (i = 0; i < fb_helper->connector_count; i++) {
1093 connector = fb_helper->connector_info[i]->connector;
38651674 1094 enabled[i] = drm_connector_enabled(connector, false);
38651674
DA
1095 }
1096}
1097
1d42bbc8
DA
1098static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1099 struct drm_display_mode **modes,
1100 bool *enabled, int width, int height)
1101{
1102 int count, i, j;
1103 bool can_clone = false;
1104 struct drm_fb_helper_connector *fb_helper_conn;
1105 struct drm_display_mode *dmt_mode, *mode;
1106
1107 /* only contemplate cloning in the single crtc case */
1108 if (fb_helper->crtc_count > 1)
1109 return false;
1110
1111 count = 0;
1112 for (i = 0; i < fb_helper->connector_count; i++) {
1113 if (enabled[i])
1114 count++;
1115 }
1116
1117 /* only contemplate cloning if more than one connector is enabled */
1118 if (count <= 1)
1119 return false;
1120
1121 /* check the command line or if nothing common pick 1024x768 */
1122 can_clone = true;
1123 for (i = 0; i < fb_helper->connector_count; i++) {
1124 if (!enabled[i])
1125 continue;
1126 fb_helper_conn = fb_helper->connector_info[i];
1127 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1128 if (!modes[i]) {
1129 can_clone = false;
1130 break;
1131 }
1132 for (j = 0; j < i; j++) {
1133 if (!enabled[j])
1134 continue;
1135 if (!drm_mode_equal(modes[j], modes[i]))
1136 can_clone = false;
1137 }
1138 }
1139
1140 if (can_clone) {
1141 DRM_DEBUG_KMS("can clone using command line\n");
1142 return true;
1143 }
1144
1145 /* try and find a 1024x768 mode on each connector */
1146 can_clone = true;
1147 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1148
1149 for (i = 0; i < fb_helper->connector_count; i++) {
1150
1151 if (!enabled[i])
1152 continue;
1153
1154 fb_helper_conn = fb_helper->connector_info[i];
1155 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1156 if (drm_mode_equal(mode, dmt_mode))
1157 modes[i] = mode;
1158 }
1159 if (!modes[i])
1160 can_clone = false;
1161 }
1162
1163 if (can_clone) {
1164 DRM_DEBUG_KMS("can clone using 1024x768\n");
1165 return true;
1166 }
1167 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1168 return false;
1169}
1170
0b4c0f3f 1171static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
38651674
DA
1172 struct drm_display_mode **modes,
1173 bool *enabled, int width, int height)
1174{
0b4c0f3f
DA
1175 struct drm_fb_helper_connector *fb_helper_conn;
1176 int i;
38651674 1177
0b4c0f3f
DA
1178 for (i = 0; i < fb_helper->connector_count; i++) {
1179 fb_helper_conn = fb_helper->connector_info[i];
38651674 1180
0b4c0f3f 1181 if (enabled[i] == false)
38651674 1182 continue;
38651674
DA
1183
1184 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
0b4c0f3f 1185 fb_helper_conn->connector->base.id);
38651674
DA
1186
1187 /* got for command line mode first */
0b4c0f3f 1188 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
38651674
DA
1189 if (!modes[i]) {
1190 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
0b4c0f3f
DA
1191 fb_helper_conn->connector->base.id);
1192 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
38651674
DA
1193 }
1194 /* No preferred modes, pick one off the list */
0b4c0f3f
DA
1195 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1196 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
38651674
DA
1197 break;
1198 }
1199 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1200 "none");
38651674
DA
1201 }
1202 return true;
1203}
1204
8be48d92
DA
1205static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1206 struct drm_fb_helper_crtc **best_crtcs,
38651674
DA
1207 struct drm_display_mode **modes,
1208 int n, int width, int height)
1209{
1210 int c, o;
8be48d92 1211 struct drm_device *dev = fb_helper->dev;
38651674
DA
1212 struct drm_connector *connector;
1213 struct drm_connector_helper_funcs *connector_funcs;
1214 struct drm_encoder *encoder;
8be48d92 1215 struct drm_fb_helper_crtc *best_crtc;
38651674 1216 int my_score, best_score, score;
8be48d92 1217 struct drm_fb_helper_crtc **crtcs, *crtc;
0b4c0f3f 1218 struct drm_fb_helper_connector *fb_helper_conn;
38651674 1219
0b4c0f3f 1220 if (n == fb_helper->connector_count)
38651674 1221 return 0;
0b4c0f3f
DA
1222
1223 fb_helper_conn = fb_helper->connector_info[n];
1224 connector = fb_helper_conn->connector;
38651674
DA
1225
1226 best_crtcs[n] = NULL;
1227 best_crtc = NULL;
8be48d92 1228 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
38651674
DA
1229 if (modes[n] == NULL)
1230 return best_score;
1231
8be48d92
DA
1232 crtcs = kzalloc(dev->mode_config.num_connector *
1233 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1234 if (!crtcs)
1235 return best_score;
1236
1237 my_score = 1;
1238 if (connector->status == connector_status_connected)
1239 my_score++;
0b4c0f3f 1240 if (drm_has_cmdline_mode(fb_helper_conn))
38651674 1241 my_score++;
0b4c0f3f 1242 if (drm_has_preferred_mode(fb_helper_conn, width, height))
38651674
DA
1243 my_score++;
1244
1245 connector_funcs = connector->helper_private;
1246 encoder = connector_funcs->best_encoder(connector);
1247 if (!encoder)
1248 goto out;
1249
38651674
DA
1250 /* select a crtc for this connector and then attempt to configure
1251 remaining connectors */
8be48d92
DA
1252 for (c = 0; c < fb_helper->crtc_count; c++) {
1253 crtc = &fb_helper->crtc_info[c];
38651674
DA
1254
1255 if ((encoder->possible_crtcs & (1 << c)) == 0) {
38651674
DA
1256 continue;
1257 }
1258
1259 for (o = 0; o < n; o++)
1260 if (best_crtcs[o] == crtc)
1261 break;
1262
1263 if (o < n) {
1d42bbc8
DA
1264 /* ignore cloning unless only a single crtc */
1265 if (fb_helper->crtc_count > 1)
1266 continue;
1267
1268 if (!drm_mode_equal(modes[o], modes[n]))
1269 continue;
38651674
DA
1270 }
1271
1272 crtcs[n] = crtc;
8be48d92
DA
1273 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1274 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
38651674
DA
1275 width, height);
1276 if (score > best_score) {
1277 best_crtc = crtc;
1278 best_score = score;
1279 memcpy(best_crtcs, crtcs,
1280 dev->mode_config.num_connector *
8be48d92 1281 sizeof(struct drm_fb_helper_crtc *));
38651674 1282 }
38651674
DA
1283 }
1284out:
1285 kfree(crtcs);
1286 return best_score;
1287}
1288
8be48d92 1289static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
38651674 1290{
8be48d92
DA
1291 struct drm_device *dev = fb_helper->dev;
1292 struct drm_fb_helper_crtc **crtcs;
38651674
DA
1293 struct drm_display_mode **modes;
1294 struct drm_encoder *encoder;
8be48d92 1295 struct drm_mode_set *modeset;
38651674
DA
1296 bool *enabled;
1297 int width, height;
1298 int i, ret;
1299
1300 DRM_DEBUG_KMS("\n");
1301
1302 width = dev->mode_config.max_width;
1303 height = dev->mode_config.max_height;
1304
1305 /* clean out all the encoder/crtc combos */
1306 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1307 encoder->crtc = NULL;
1308 }
1309
1310 crtcs = kcalloc(dev->mode_config.num_connector,
8be48d92 1311 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
38651674
DA
1312 modes = kcalloc(dev->mode_config.num_connector,
1313 sizeof(struct drm_display_mode *), GFP_KERNEL);
1314 enabled = kcalloc(dev->mode_config.num_connector,
1315 sizeof(bool), GFP_KERNEL);
1316
0b4c0f3f 1317 drm_enable_connectors(fb_helper, enabled);
38651674 1318
1d42bbc8
DA
1319 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1320 if (!ret) {
1321 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1322 if (!ret)
1323 DRM_ERROR("Unable to find initial modes\n");
1324 }
38651674
DA
1325
1326 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1327
8be48d92
DA
1328 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1329
1330 /* need to set the modesets up here for use later */
1331 /* fill out the connector<->crtc mappings into the modesets */
1332 for (i = 0; i < fb_helper->crtc_count; i++) {
1333 modeset = &fb_helper->crtc_info[i].mode_set;
1334 modeset->num_connectors = 0;
1335 }
38651674 1336
0b4c0f3f 1337 for (i = 0; i < fb_helper->connector_count; i++) {
38651674 1338 struct drm_display_mode *mode = modes[i];
8be48d92
DA
1339 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1340 modeset = &fb_crtc->mode_set;
38651674 1341
8be48d92 1342 if (mode && fb_crtc) {
38651674 1343 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
8be48d92
DA
1344 mode->name, fb_crtc->mode_set.crtc->base.id);
1345 fb_crtc->desired_mode = mode;
1346 if (modeset->mode)
1347 drm_mode_destroy(dev, modeset->mode);
1348 modeset->mode = drm_mode_duplicate(dev,
1349 fb_crtc->desired_mode);
0b4c0f3f 1350 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
38651674 1351 }
38651674
DA
1352 }
1353
1354 kfree(crtcs);
1355 kfree(modes);
1356 kfree(enabled);
1357}
1358
1359/**
1360 * drm_helper_initial_config - setup a sane initial connector configuration
1361 * @dev: DRM device
1362 *
1363 * LOCKING:
1364 * Called at init time, must take mode config lock.
1365 *
1366 * Scan the CRTCs and connectors and try to put together an initial setup.
1367 * At the moment, this is a cloned configuration across all heads with
1368 * a new framebuffer object as the backing store.
1369 *
1370 * RETURNS:
1371 * Zero if everything went ok, nonzero otherwise.
1372 */
4abe3520 1373bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
38651674 1374{
8be48d92 1375 struct drm_device *dev = fb_helper->dev;
38651674
DA
1376 int count = 0;
1377
1378 /* disable all the possible outputs/crtcs before entering KMS mode */
8be48d92 1379 drm_helper_disable_unused_functions(fb_helper->dev);
38651674 1380
0b4c0f3f 1381 drm_fb_helper_parse_command_line(fb_helper);
38651674 1382
0b4c0f3f
DA
1383 count = drm_fb_helper_probe_connector_modes(fb_helper,
1384 dev->mode_config.max_width,
1385 dev->mode_config.max_height);
38651674
DA
1386 /*
1387 * we shouldn't end up with no modes here.
1388 */
5c4426a7 1389 if (count == 0) {
eb1f8e4f 1390 printk(KERN_INFO "No connectors reported connected with modes\n");
5c4426a7 1391 }
8be48d92 1392 drm_setup_crtcs(fb_helper);
38651674 1393
4abe3520 1394 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
38651674 1395}
8be48d92 1396EXPORT_SYMBOL(drm_fb_helper_initial_config);
38651674 1397
eb1f8e4f 1398bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
38651674 1399{
5c4426a7 1400 int count = 0;
4abe3520 1401 u32 max_width, max_height, bpp_sel;
4abe3520
DA
1402 bool bound = false, crtcs_bound = false;
1403 struct drm_crtc *crtc;
5c4426a7 1404
eb1f8e4f
DA
1405 if (!fb_helper->fb)
1406 return false;
4abe3520 1407
4abe3520
DA
1408 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1409 if (crtc->fb)
1410 crtcs_bound = true;
1411 if (crtc->fb == fb_helper->fb)
1412 bound = true;
1413 }
1414
eb1f8e4f 1415 if (!bound && crtcs_bound) {
4abe3520 1416 fb_helper->delayed_hotplug = true;
eb1f8e4f 1417 return false;
4abe3520 1418 }
eb1f8e4f 1419 DRM_DEBUG_KMS("\n");
4abe3520 1420
eb1f8e4f
DA
1421 max_width = fb_helper->fb->width;
1422 max_height = fb_helper->fb->height;
1423 bpp_sel = fb_helper->fb->bits_per_pixel;
5c4426a7 1424
eb1f8e4f
DA
1425 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1426 max_height);
1427 drm_setup_crtcs(fb_helper);
4abe3520 1428
eb1f8e4f 1429 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
5c4426a7 1430}
eb1f8e4f 1431EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
5c4426a7 1432