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