drm/kms: start adding command line interface using fb.
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc_helper.c
CommitLineData
f453ba04
DA
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 *
5 * DRM core CRTC related functions
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 *
25 * Authors:
26 * Keith Packard
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
30 */
31
32#include "drmP.h"
33#include "drm_crtc.h"
34#include "drm_crtc_helper.h"
d50ba256 35#include "drm_fb_helper.h"
f453ba04 36
6714977b 37static void drm_mode_validate_flag(struct drm_connector *connector,
38 int flags)
39{
40 struct drm_display_mode *mode, *t;
41
42 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
43 return;
44
45 list_for_each_entry_safe(mode, t, &connector->modes, head) {
46 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
47 !(flags & DRM_MODE_FLAG_INTERLACE))
48 mode->status = MODE_NO_INTERLACE;
49 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
50 !(flags & DRM_MODE_FLAG_DBLSCAN))
51 mode->status = MODE_NO_DBLESCAN;
52 }
53
54 return;
55}
56
f453ba04
DA
57/**
58 * drm_helper_probe_connector_modes - get complete set of display modes
59 * @dev: DRM device
60 * @maxX: max width for modes
61 * @maxY: max height for modes
62 *
63 * LOCKING:
64 * Caller must hold mode config lock.
65 *
66 * Based on @dev's mode_config layout, scan all the connectors and try to detect
67 * modes on them. Modes will first be added to the connector's probed_modes
68 * list, then culled (based on validity and the @maxX, @maxY parameters) and
69 * put into the normal modes list.
70 *
71 * Intended to be used either at bootup time or when major configuration
72 * changes have occurred.
73 *
74 * FIXME: take into account monitor limits
40a518d9
JB
75 *
76 * RETURNS:
77 * Number of modes found on @connector.
f453ba04 78 */
40a518d9
JB
79int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
80 uint32_t maxX, uint32_t maxY)
f453ba04
DA
81{
82 struct drm_device *dev = connector->dev;
83 struct drm_display_mode *mode, *t;
84 struct drm_connector_helper_funcs *connector_funcs =
85 connector->helper_private;
40a518d9 86 int count = 0;
6714977b 87 int mode_flags = 0;
f453ba04 88
58367ed6 89 DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector));
f453ba04
DA
90 /* set all modes to the unverified state */
91 list_for_each_entry_safe(mode, t, &connector->modes, head)
92 mode->status = MODE_UNVERIFIED;
93
d50ba256
DA
94 if (connector->force) {
95 if (connector->force == DRM_FORCE_ON)
96 connector->status = connector_status_connected;
97 else
98 connector->status = connector_status_disconnected;
99 if (connector->funcs->force)
100 connector->funcs->force(connector);
101 } else
102 connector->status = connector->funcs->detect(connector);
f453ba04
DA
103
104 if (connector->status == connector_status_disconnected) {
58367ed6 105 DRM_DEBUG_KMS("%s is disconnected\n",
f453ba04 106 drm_get_connector_name(connector));
620f3781 107 goto prune;
f453ba04
DA
108 }
109
40a518d9 110 count = (*connector_funcs->get_modes)(connector);
50fe4cfd 111 if (!count) {
112 count = drm_add_modes_noedid(connector, 800, 600);
113 if (!count)
114 return 0;
115 }
f453ba04 116
40a518d9 117 drm_mode_connector_list_update(connector);
f453ba04
DA
118
119 if (maxX && maxY)
120 drm_mode_validate_size(dev, &connector->modes, maxX,
121 maxY, 0);
6714977b 122
123 if (connector->interlace_allowed)
124 mode_flags |= DRM_MODE_FLAG_INTERLACE;
125 if (connector->doublescan_allowed)
126 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
127 drm_mode_validate_flag(connector, mode_flags);
128
f453ba04
DA
129 list_for_each_entry_safe(mode, t, &connector->modes, head) {
130 if (mode->status == MODE_OK)
131 mode->status = connector_funcs->mode_valid(connector,
132 mode);
133 }
134
620f3781 135prune:
f453ba04
DA
136 drm_mode_prune_invalid(dev, &connector->modes, true);
137
40a518d9
JB
138 if (list_empty(&connector->modes))
139 return 0;
f453ba04
DA
140
141 drm_mode_sort(&connector->modes);
142
58367ed6
ZY
143 DRM_DEBUG_KMS("Probed modes for %s\n",
144 drm_get_connector_name(connector));
f453ba04
DA
145 list_for_each_entry_safe(mode, t, &connector->modes, head) {
146 mode->vrefresh = drm_mode_vrefresh(mode);
147
148 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
149 drm_mode_debug_printmodeline(mode);
150 }
40a518d9
JB
151
152 return count;
f453ba04
DA
153}
154EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
155
40a518d9 156int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
f453ba04
DA
157 uint32_t maxY)
158{
159 struct drm_connector *connector;
40a518d9 160 int count = 0;
f453ba04
DA
161
162 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
40a518d9
JB
163 count += drm_helper_probe_single_connector_modes(connector,
164 maxX, maxY);
f453ba04 165 }
40a518d9
JB
166
167 return count;
f453ba04
DA
168}
169EXPORT_SYMBOL(drm_helper_probe_connector_modes);
170
c9fb15f6
KP
171/**
172 * drm_helper_encoder_in_use - check if a given encoder is in use
173 * @encoder: encoder to check
174 *
175 * LOCKING:
176 * Caller must hold mode config lock.
177 *
178 * Walk @encoders's DRM device's mode_config and see if it's in use.
179 *
180 * RETURNS:
181 * True if @encoder is part of the mode_config, false otherwise.
182 */
183bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
184{
185 struct drm_connector *connector;
186 struct drm_device *dev = encoder->dev;
187 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
188 if (connector->encoder == encoder)
189 return true;
190 return false;
191}
192EXPORT_SYMBOL(drm_helper_encoder_in_use);
193
f453ba04
DA
194/**
195 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
196 * @crtc: CRTC to check
197 *
198 * LOCKING:
199 * Caller must hold mode config lock.
200 *
201 * Walk @crtc's DRM device's mode_config and see if it's in use.
202 *
203 * RETURNS:
204 * True if @crtc is part of the mode_config, false otherwise.
205 */
206bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
207{
208 struct drm_encoder *encoder;
209 struct drm_device *dev = crtc->dev;
210 /* FIXME: Locking around list access? */
211 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
c9fb15f6 212 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
f453ba04
DA
213 return true;
214 return false;
215}
216EXPORT_SYMBOL(drm_helper_crtc_in_use);
217
218/**
219 * drm_disable_unused_functions - disable unused objects
220 * @dev: DRM device
221 *
222 * LOCKING:
223 * Caller must hold mode config lock.
224 *
225 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
226 * by calling its dpms function, which should power it off.
227 */
228void drm_helper_disable_unused_functions(struct drm_device *dev)
229{
230 struct drm_encoder *encoder;
a3a0544b 231 struct drm_connector *connector;
f453ba04
DA
232 struct drm_encoder_helper_funcs *encoder_funcs;
233 struct drm_crtc *crtc;
234
a3a0544b
DA
235 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
236 if (!connector->encoder)
237 continue;
238 if (connector->status == connector_status_disconnected)
239 connector->encoder = NULL;
240 }
241
f453ba04
DA
242 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
243 encoder_funcs = encoder->helper_private;
a3a0544b
DA
244 if (!drm_helper_encoder_in_use(encoder)) {
245 if (encoder_funcs->disable)
246 (*encoder_funcs->disable)(encoder);
247 else
248 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
9c552dd7
DA
249 /* disconnector encoder from any connector */
250 encoder->crtc = NULL;
a3a0544b 251 }
f453ba04
DA
252 }
253
254 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
255 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
256 crtc->enabled = drm_helper_crtc_in_use(crtc);
257 if (!crtc->enabled) {
258 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
259 crtc->fb = NULL;
260 }
261 }
262}
263EXPORT_SYMBOL(drm_helper_disable_unused_functions);
264
265static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
266{
267 struct drm_display_mode *mode;
268
269 list_for_each_entry(mode, &connector->modes, head) {
270 if (drm_mode_width(mode) > width ||
271 drm_mode_height(mode) > height)
272 continue;
273 if (mode->type & DRM_MODE_TYPE_PREFERRED)
274 return mode;
275 }
276 return NULL;
277}
278
d50ba256
DA
279static bool drm_has_cmdline_mode(struct drm_connector *connector)
280{
281 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
282 struct drm_fb_helper_cmdline_mode *cmdline_mode = &fb_help_conn->cmdline_mode;
283 return cmdline_mode->specified;
284}
285
286static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
287{
288 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
289 struct drm_fb_helper_cmdline_mode *cmdline_mode = &fb_help_conn->cmdline_mode;
290 struct drm_display_mode *mode = NULL;
291
292 if (cmdline_mode->specified == false)
293 return mode;
294
295 /* attempt to find a matching mode in the list of modes
296 * we have gotten so far, if not add a CVT mode that conforms
297 */
298 if (cmdline_mode->rb || cmdline_mode->margins)
299 goto create_mode;
300
301 list_for_each_entry(mode, &connector->modes, head) {
302 /* check width/height */
303 if (mode->hdisplay != cmdline_mode->xres ||
304 mode->vdisplay != cmdline_mode->yres)
305 continue;
306
307 if (cmdline_mode->refresh_specified) {
308 if (mode->vrefresh != cmdline_mode->refresh)
309 continue;
310 }
311
312 if (cmdline_mode->interlace) {
313 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
314 continue;
315 }
316 return mode;
317 }
318
319create_mode:
320 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
321 cmdline_mode->yres,
322 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
323 cmdline_mode->rb, cmdline_mode->interlace,
324 cmdline_mode->margins);
325 list_add(&mode->head, &connector->modes);
326 return mode;
327}
328
f453ba04
DA
329static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
330{
331 bool enable;
332
333 if (strict) {
334 enable = connector->status == connector_status_connected;
335 } else {
336 enable = connector->status != connector_status_disconnected;
337 }
338 return enable;
339}
340
341static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
342{
343 bool any_enabled = false;
344 struct drm_connector *connector;
345 int i = 0;
346
347 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
348 enabled[i] = drm_connector_enabled(connector, true);
58367ed6 349 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
40a518d9 350 enabled[i] ? "yes" : "no");
f453ba04
DA
351 any_enabled |= enabled[i];
352 i++;
353 }
354
355 if (any_enabled)
356 return;
357
358 i = 0;
359 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
360 enabled[i] = drm_connector_enabled(connector, false);
361 i++;
362 }
363}
364
365static bool drm_target_preferred(struct drm_device *dev,
366 struct drm_display_mode **modes,
367 bool *enabled, int width, int height)
368{
369 struct drm_connector *connector;
370 int i = 0;
371
372 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
373
374 if (enabled[i] == false) {
375 i++;
376 continue;
377 }
378
d50ba256
DA
379 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
380 connector->base.id);
40a518d9 381
d50ba256
DA
382 /* got for command line mode first */
383 modes[i] = drm_pick_cmdline_mode(connector, width, height);
384 if (!modes[i]) {
385 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
386 connector->base.id);
387 modes[i] = drm_has_preferred_mode(connector, width, height);
388 }
40a518d9
JB
389 /* No preferred modes, pick one off the list */
390 if (!modes[i] && !list_empty(&connector->modes)) {
f453ba04
DA
391 list_for_each_entry(modes[i], &connector->modes, head)
392 break;
393 }
58367ed6 394 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
40a518d9 395 "none");
f453ba04
DA
396 i++;
397 }
398 return true;
399}
400
401static int drm_pick_crtcs(struct drm_device *dev,
402 struct drm_crtc **best_crtcs,
403 struct drm_display_mode **modes,
404 int n, int width, int height)
405{
406 int c, o;
407 struct drm_connector *connector;
408 struct drm_connector_helper_funcs *connector_funcs;
409 struct drm_encoder *encoder;
410 struct drm_crtc *best_crtc;
411 int my_score, best_score, score;
412 struct drm_crtc **crtcs, *crtc;
413
414 if (n == dev->mode_config.num_connector)
415 return 0;
416 c = 0;
417 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
418 if (c == n)
419 break;
420 c++;
421 }
422
423 best_crtcs[n] = NULL;
424 best_crtc = NULL;
425 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
426 if (modes[n] == NULL)
427 return best_score;
428
429 crtcs = kmalloc(dev->mode_config.num_connector *
430 sizeof(struct drm_crtc *), GFP_KERNEL);
431 if (!crtcs)
432 return best_score;
433
434 my_score = 1;
435 if (connector->status == connector_status_connected)
436 my_score++;
d50ba256
DA
437 if (drm_has_cmdline_mode(connector))
438 my_score++;
f453ba04
DA
439 if (drm_has_preferred_mode(connector, width, height))
440 my_score++;
441
442 connector_funcs = connector->helper_private;
443 encoder = connector_funcs->best_encoder(connector);
444 if (!encoder)
445 goto out;
446
447 connector->encoder = encoder;
448
449 /* select a crtc for this connector and then attempt to configure
450 remaining connectors */
451 c = 0;
452 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
453
a3a0544b 454 if ((encoder->possible_crtcs & (1 << c)) == 0) {
f453ba04
DA
455 c++;
456 continue;
457 }
458
459 for (o = 0; o < n; o++)
460 if (best_crtcs[o] == crtc)
461 break;
462
463 if (o < n) {
464 /* ignore cloning for now */
465 c++;
466 continue;
467 }
468
469 crtcs[n] = crtc;
470 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
471 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
472 width, height);
473 if (score > best_score) {
474 best_crtc = crtc;
475 best_score = score;
476 memcpy(best_crtcs, crtcs,
477 dev->mode_config.num_connector *
478 sizeof(struct drm_crtc *));
479 }
480 c++;
481 }
482out:
483 kfree(crtcs);
484 return best_score;
485}
486
487static void drm_setup_crtcs(struct drm_device *dev)
488{
489 struct drm_crtc **crtcs;
490 struct drm_display_mode **modes;
491 struct drm_encoder *encoder;
492 struct drm_connector *connector;
493 bool *enabled;
494 int width, height;
495 int i, ret;
496
58367ed6 497 DRM_DEBUG_KMS("\n");
40a518d9 498
f453ba04
DA
499 width = dev->mode_config.max_width;
500 height = dev->mode_config.max_height;
501
502 /* clean out all the encoder/crtc combos */
503 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
504 encoder->crtc = NULL;
505 }
506
507 crtcs = kcalloc(dev->mode_config.num_connector,
508 sizeof(struct drm_crtc *), GFP_KERNEL);
509 modes = kcalloc(dev->mode_config.num_connector,
510 sizeof(struct drm_display_mode *), GFP_KERNEL);
511 enabled = kcalloc(dev->mode_config.num_connector,
512 sizeof(bool), GFP_KERNEL);
513
514 drm_enable_connectors(dev, enabled);
515
516 ret = drm_target_preferred(dev, modes, enabled, width, height);
517 if (!ret)
518 DRM_ERROR("Unable to find initial modes\n");
519
58367ed6 520 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
40a518d9 521
f453ba04
DA
522 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
523
524 i = 0;
525 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
526 struct drm_display_mode *mode = modes[i];
527 struct drm_crtc *crtc = crtcs[i];
528
529 if (connector->encoder == NULL) {
530 i++;
531 continue;
532 }
533
534 if (mode && crtc) {
58367ed6 535 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
40a518d9 536 mode->name, crtc->base.id);
f453ba04
DA
537 crtc->desired_mode = mode;
538 connector->encoder->crtc = crtc;
a3a0544b 539 } else {
f453ba04 540 connector->encoder->crtc = NULL;
a3a0544b
DA
541 connector->encoder = NULL;
542 }
f453ba04
DA
543 i++;
544 }
545
546 kfree(crtcs);
547 kfree(modes);
548 kfree(enabled);
549}
7bec756c
JB
550
551/**
552 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
553 * @encoder: encoder to test
554 * @crtc: crtc to test
555 *
556 * Return false if @encoder can't be driven by @crtc, true otherwise.
557 */
558static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
559 struct drm_crtc *crtc)
560{
561 struct drm_device *dev;
562 struct drm_crtc *tmp;
563 int crtc_mask = 1;
564
565 WARN(!crtc, "checking null crtc?");
566
567 dev = crtc->dev;
568
569 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
570 if (tmp == crtc)
571 break;
572 crtc_mask <<= 1;
573 }
574
575 if (encoder->possible_crtcs & crtc_mask)
576 return true;
577 return false;
578}
579
580/*
581 * Check the CRTC we're going to map each output to vs. its current
582 * CRTC. If they don't match, we have to disable the output and the CRTC
583 * since the driver will have to re-route things.
584 */
585static void
586drm_crtc_prepare_encoders(struct drm_device *dev)
587{
588 struct drm_encoder_helper_funcs *encoder_funcs;
589 struct drm_encoder *encoder;
590
591 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
592 encoder_funcs = encoder->helper_private;
593 /* Disable unused encoders */
594 if (encoder->crtc == NULL)
595 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
596 /* Disable encoders whose CRTC is about to change */
597 if (encoder_funcs->get_crtc &&
598 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
599 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
600 }
601}
602
f453ba04
DA
603/**
604 * drm_crtc_set_mode - set a mode
605 * @crtc: CRTC to program
606 * @mode: mode to use
607 * @x: width of mode
608 * @y: height of mode
609 *
610 * LOCKING:
611 * Caller must hold mode config lock.
612 *
613 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
614 * to fixup or reject the mode prior to trying to set it.
615 *
616 * RETURNS:
617 * True if the mode was set successfully, or false otherwise.
618 */
619bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
620 struct drm_display_mode *mode,
3c4fdcfb
KH
621 int x, int y,
622 struct drm_framebuffer *old_fb)
f453ba04
DA
623{
624 struct drm_device *dev = crtc->dev;
625 struct drm_display_mode *adjusted_mode, saved_mode;
626 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
627 struct drm_encoder_helper_funcs *encoder_funcs;
628 int saved_x, saved_y;
629 struct drm_encoder *encoder;
630 bool ret = true;
631
632 adjusted_mode = drm_mode_duplicate(dev, mode);
633
634 crtc->enabled = drm_helper_crtc_in_use(crtc);
635
636 if (!crtc->enabled)
637 return true;
638
639 saved_mode = crtc->mode;
640 saved_x = crtc->x;
641 saved_y = crtc->y;
642
643 /* Update crtc values up front so the driver can rely on them for mode
644 * setting.
645 */
646 crtc->mode = *mode;
647 crtc->x = x;
648 crtc->y = y;
649
f453ba04
DA
650 /* Pass our mode to the connectors and the CRTC to give them a chance to
651 * adjust it according to limitations or connector properties, and also
652 * a chance to reject the mode entirely.
653 */
654 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
655
656 if (encoder->crtc != crtc)
657 continue;
658 encoder_funcs = encoder->helper_private;
659 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
660 adjusted_mode))) {
661 goto done;
662 }
663 }
664
665 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
666 goto done;
667 }
668
669 /* Prepare the encoders and CRTCs before setting the mode. */
670 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
671
672 if (encoder->crtc != crtc)
673 continue;
674 encoder_funcs = encoder->helper_private;
675 /* Disable the encoders as the first thing we do. */
676 encoder_funcs->prepare(encoder);
677 }
678
7bec756c
JB
679 drm_crtc_prepare_encoders(dev);
680
f453ba04
DA
681 crtc_funcs->prepare(crtc);
682
683 /* Set up the DPLL and any encoders state that needs to adjust or depend
684 * on the DPLL.
685 */
5c3b82e2
CW
686 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
687 if (!ret)
688 goto done;
f453ba04
DA
689
690 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
691
692 if (encoder->crtc != crtc)
693 continue;
694
695 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
696 mode->name, mode->base.id);
697 encoder_funcs = encoder->helper_private;
698 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
699 }
700
701 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
702 crtc_funcs->commit(crtc);
703
704 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
705
706 if (encoder->crtc != crtc)
707 continue;
708
709 encoder_funcs = encoder->helper_private;
710 encoder_funcs->commit(encoder);
711
712 }
713
714 /* XXX free adjustedmode */
715 drm_mode_destroy(dev, adjusted_mode);
716 /* FIXME: add subpixel order */
717done:
718 if (!ret) {
719 crtc->mode = saved_mode;
720 crtc->x = saved_x;
721 crtc->y = saved_y;
722 }
723
724 return ret;
725}
726EXPORT_SYMBOL(drm_crtc_helper_set_mode);
727
728
729/**
730 * drm_crtc_helper_set_config - set a new config from userspace
731 * @crtc: CRTC to setup
732 * @crtc_info: user provided configuration
733 * @new_mode: new mode to set
734 * @connector_set: set of connectors for the new config
735 * @fb: new framebuffer
736 *
737 * LOCKING:
738 * Caller must hold mode config lock.
739 *
740 * Setup a new configuration, provided by the user in @crtc_info, and enable
741 * it.
742 *
743 * RETURNS:
744 * Zero. (FIXME)
745 */
746int drm_crtc_helper_set_config(struct drm_mode_set *set)
747{
748 struct drm_device *dev;
e67aae79
MM
749 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
750 struct drm_encoder *save_encoders, *new_encoder, *encoder;
7bec756c 751 struct drm_framebuffer *old_fb = NULL;
4cb72b17
JB
752 bool mode_changed = false; /* if true do a full mode set */
753 bool fb_changed = false; /* if true and !mode_changed just do a flip */
e67aae79 754 struct drm_connector *save_connectors, *connector;
f453ba04
DA
755 int count = 0, ro, fail = 0;
756 struct drm_crtc_helper_funcs *crtc_funcs;
757 int ret = 0;
758
58367ed6 759 DRM_DEBUG_KMS("\n");
f453ba04
DA
760
761 if (!set)
762 return -EINVAL;
763
764 if (!set->crtc)
765 return -EINVAL;
766
767 if (!set->crtc->helper_private)
768 return -EINVAL;
769
770 crtc_funcs = set->crtc->helper_private;
771
58367ed6
ZY
772 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
773 " %d (x, y) (%i, %i)\n",
f453ba04
DA
774 set->crtc, set->crtc->base.id, set->fb, set->connectors,
775 (int)set->num_connectors, set->x, set->y);
776
777 dev = set->crtc->dev;
778
e67aae79
MM
779 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
780 * connector data. */
781 save_crtcs = kzalloc(dev->mode_config.num_crtc *
782 sizeof(struct drm_crtc), GFP_KERNEL);
f453ba04
DA
783 if (!save_crtcs)
784 return -ENOMEM;
785
e67aae79
MM
786 save_encoders = kzalloc(dev->mode_config.num_encoder *
787 sizeof(struct drm_encoder), GFP_KERNEL);
f453ba04
DA
788 if (!save_encoders) {
789 kfree(save_crtcs);
790 return -ENOMEM;
791 }
792
e67aae79
MM
793 save_connectors = kzalloc(dev->mode_config.num_connector *
794 sizeof(struct drm_connector), GFP_KERNEL);
795 if (!save_connectors) {
796 kfree(save_crtcs);
797 kfree(save_encoders);
798 return -ENOMEM;
799 }
800
801 /* Copy data. Note that driver private data is not affected.
802 * Should anything bad happen only the expected state is
803 * restored, not the drivers personal bookkeeping.
804 */
805 count = 0;
806 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
807 save_crtcs[count++] = *crtc;
808 }
809
810 count = 0;
811 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
812 save_encoders[count++] = *encoder;
813 }
814
815 count = 0;
816 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
817 save_connectors[count++] = *connector;
818 }
819
f453ba04
DA
820 /* We should be able to check here if the fb has the same properties
821 * and then just flip_or_move it */
822 if (set->crtc->fb != set->fb) {
712531bf 823 /* If we have no fb then treat it as a full mode set */
7bec756c 824 if (set->crtc->fb == NULL) {
58367ed6 825 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
712531bf 826 mode_changed = true;
4cb72b17
JB
827 } else if (set->fb == NULL) {
828 mode_changed = true;
7bec756c 829 } else if ((set->fb->bits_per_pixel !=
712531bf
JB
830 set->crtc->fb->bits_per_pixel) ||
831 set->fb->depth != set->crtc->fb->depth)
832 fb_changed = true;
f453ba04 833 else
712531bf 834 fb_changed = true;
f453ba04
DA
835 }
836
837 if (set->x != set->crtc->x || set->y != set->crtc->y)
712531bf 838 fb_changed = true;
f453ba04
DA
839
840 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
58367ed6 841 DRM_DEBUG_KMS("modes are different, full mode set\n");
f453ba04
DA
842 drm_mode_debug_printmodeline(&set->crtc->mode);
843 drm_mode_debug_printmodeline(set->mode);
712531bf 844 mode_changed = true;
f453ba04
DA
845 }
846
847 /* a) traverse passed in connector list and get encoders for them */
848 count = 0;
849 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
850 struct drm_connector_helper_funcs *connector_funcs =
851 connector->helper_private;
f453ba04
DA
852 new_encoder = connector->encoder;
853 for (ro = 0; ro < set->num_connectors; ro++) {
854 if (set->connectors[ro] == connector) {
855 new_encoder = connector_funcs->best_encoder(connector);
856 /* if we can't get an encoder for a connector
857 we are setting now - then fail */
858 if (new_encoder == NULL)
859 /* don't break so fail path works correct */
860 fail = 1;
861 break;
862 }
863 }
864
865 if (new_encoder != connector->encoder) {
58367ed6 866 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
712531bf 867 mode_changed = true;
ff846ab7
MM
868 /* If the encoder is reused for another connector, then
869 * the appropriate crtc will be set later.
870 */
ff6fdbed
MM
871 if (connector->encoder)
872 connector->encoder->crtc = NULL;
f453ba04
DA
873 connector->encoder = new_encoder;
874 }
875 }
876
877 if (fail) {
878 ret = -EINVAL;
e67aae79 879 goto fail;
f453ba04
DA
880 }
881
882 count = 0;
883 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
884 if (!connector->encoder)
885 continue;
886
f453ba04
DA
887 if (connector->encoder->crtc == set->crtc)
888 new_crtc = NULL;
889 else
890 new_crtc = connector->encoder->crtc;
891
892 for (ro = 0; ro < set->num_connectors; ro++) {
893 if (set->connectors[ro] == connector)
894 new_crtc = set->crtc;
895 }
7bec756c
JB
896
897 /* Make sure the new CRTC will work with the encoder */
898 if (new_crtc &&
899 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
900 ret = -EINVAL;
e67aae79 901 goto fail;
7bec756c 902 }
f453ba04 903 if (new_crtc != connector->encoder->crtc) {
58367ed6 904 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
712531bf 905 mode_changed = true;
f453ba04
DA
906 connector->encoder->crtc = new_crtc;
907 }
58367ed6 908 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
7bec756c 909 connector->base.id, new_crtc);
f453ba04
DA
910 }
911
912 /* mode_set_base is not a required function */
712531bf
JB
913 if (fb_changed && !crtc_funcs->mode_set_base)
914 mode_changed = true;
f453ba04 915
712531bf 916 if (mode_changed) {
3c4fdcfb 917 old_fb = set->crtc->fb;
f453ba04
DA
918 set->crtc->fb = set->fb;
919 set->crtc->enabled = (set->mode != NULL);
920 if (set->mode != NULL) {
58367ed6
ZY
921 DRM_DEBUG_KMS("attempting to set mode from"
922 " userspace\n");
f453ba04
DA
923 drm_mode_debug_printmodeline(set->mode);
924 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
3c4fdcfb
KH
925 set->x, set->y,
926 old_fb)) {
5c3b82e2
CW
927 DRM_ERROR("failed to set mode on crtc %p\n",
928 set->crtc);
f453ba04 929 ret = -EINVAL;
e67aae79 930 goto fail;
f453ba04
DA
931 }
932 /* TODO are these needed? */
933 set->crtc->desired_x = set->x;
934 set->crtc->desired_y = set->y;
935 set->crtc->desired_mode = set->mode;
936 }
937 drm_helper_disable_unused_functions(dev);
712531bf 938 } else if (fb_changed) {
9b1596af
BS
939 set->crtc->x = set->x;
940 set->crtc->y = set->y;
941
3c4fdcfb 942 old_fb = set->crtc->fb;
f453ba04
DA
943 if (set->crtc->fb != set->fb)
944 set->crtc->fb = set->fb;
5c3b82e2
CW
945 ret = crtc_funcs->mode_set_base(set->crtc,
946 set->x, set->y, old_fb);
947 if (ret != 0)
e67aae79 948 goto fail;
f453ba04
DA
949 }
950
e67aae79 951 kfree(save_connectors);
f453ba04
DA
952 kfree(save_encoders);
953 kfree(save_crtcs);
954 return 0;
955
e67aae79
MM
956fail:
957 /* Restore all previous data. */
f453ba04 958 count = 0;
e67aae79
MM
959 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
960 *crtc = save_crtcs[count++];
961 }
e62fb64e 962
e67aae79
MM
963 count = 0;
964 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
965 *encoder = save_encoders[count++];
e62fb64e 966 }
e67aae79 967
f453ba04
DA
968 count = 0;
969 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
e67aae79 970 *connector = save_connectors[count++];
f453ba04 971 }
e67aae79
MM
972
973 kfree(save_connectors);
f453ba04 974 kfree(save_encoders);
e67aae79 975 kfree(save_crtcs);
f453ba04
DA
976 return ret;
977}
978EXPORT_SYMBOL(drm_crtc_helper_set_config);
979
980bool drm_helper_plugged_event(struct drm_device *dev)
981{
58367ed6 982 DRM_DEBUG_KMS("\n");
f453ba04
DA
983
984 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
985 dev->mode_config.max_height);
986
987 drm_setup_crtcs(dev);
988
989 /* alert the driver fb layer */
990 dev->mode_config.funcs->fb_changed(dev);
991
992 /* FIXME: send hotplug event */
993 return true;
994}
995/**
996 * drm_initial_config - setup a sane initial connector configuration
997 * @dev: DRM device
f453ba04
DA
998 *
999 * LOCKING:
1000 * Called at init time, must take mode config lock.
1001 *
1002 * Scan the CRTCs and connectors and try to put together an initial setup.
1003 * At the moment, this is a cloned configuration across all heads with
1004 * a new framebuffer object as the backing store.
1005 *
1006 * RETURNS:
1007 * Zero if everything went ok, nonzero otherwise.
1008 */
7a1fb5d0 1009bool drm_helper_initial_config(struct drm_device *dev)
f453ba04 1010{
40a518d9 1011 int count = 0;
f453ba04 1012
d50ba256
DA
1013 drm_fb_helper_parse_command_line(dev);
1014
40a518d9
JB
1015 count = drm_helper_probe_connector_modes(dev,
1016 dev->mode_config.max_width,
1017 dev->mode_config.max_height);
1018
1019 /*
575dc34e 1020 * we shouldn't end up with no modes here.
40a518d9 1021 */
d50ba256 1022 WARN(!count, "No connectors reported connected with modes\n");
40a518d9
JB
1023
1024 drm_setup_crtcs(dev);
1025
1026 /* alert the driver fb layer */
1027 dev->mode_config.funcs->fb_changed(dev);
1028
1029 return 0;
f453ba04
DA
1030}
1031EXPORT_SYMBOL(drm_helper_initial_config);
1032
c9fb15f6
KP
1033static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1034{
1035 int dpms = DRM_MODE_DPMS_OFF;
1036 struct drm_connector *connector;
1037 struct drm_device *dev = encoder->dev;
1038
1039 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1040 if (connector->encoder == encoder)
1041 if (connector->dpms < dpms)
1042 dpms = connector->dpms;
1043 return dpms;
1044}
1045
1046static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1047{
1048 int dpms = DRM_MODE_DPMS_OFF;
1049 struct drm_connector *connector;
1050 struct drm_device *dev = crtc->dev;
1051
1052 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1053 if (connector->encoder && connector->encoder->crtc == crtc)
1054 if (connector->dpms < dpms)
1055 dpms = connector->dpms;
1056 return dpms;
1057}
1058
1059/**
1060 * drm_helper_connector_dpms
1061 * @connector affected connector
1062 * @mode DPMS mode
1063 *
1064 * Calls the low-level connector DPMS function, then
1065 * calls appropriate encoder and crtc DPMS functions as well
1066 */
1067void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1068{
1069 struct drm_encoder *encoder = connector->encoder;
1070 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1071 int old_dpms;
1072
1073 if (mode == connector->dpms)
1074 return;
1075
1076 old_dpms = connector->dpms;
1077 connector->dpms = mode;
1078
1079 /* from off to on, do crtc then encoder */
1080 if (mode < old_dpms) {
1081 if (crtc) {
1082 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1083 if (crtc_funcs->dpms)
1084 (*crtc_funcs->dpms) (crtc,
1085 drm_helper_choose_crtc_dpms(crtc));
1086 }
1087 if (encoder) {
1088 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1089 if (encoder_funcs->dpms)
1090 (*encoder_funcs->dpms) (encoder,
1091 drm_helper_choose_encoder_dpms(encoder));
1092 }
1093 }
1094
1095 /* from on to off, do encoder then crtc */
1096 if (mode > old_dpms) {
1097 if (encoder) {
1098 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1099 if (encoder_funcs->dpms)
1100 (*encoder_funcs->dpms) (encoder,
1101 drm_helper_choose_encoder_dpms(encoder));
1102 }
1103 if (crtc) {
1104 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1105 if (crtc_funcs->dpms)
1106 (*crtc_funcs->dpms) (crtc,
1107 drm_helper_choose_crtc_dpms(crtc));
1108 }
1109 }
1110
1111 return;
1112}
1113EXPORT_SYMBOL(drm_helper_connector_dpms);
1114
f453ba04
DA
1115/**
1116 * drm_hotplug_stage_two
1117 * @dev DRM device
1118 * @connector hotpluged connector
1119 *
1120 * LOCKING.
1121 * Caller must hold mode config lock, function might grab struct lock.
1122 *
1123 * Stage two of a hotplug.
1124 *
1125 * RETURNS:
1126 * Zero on success, errno on failure.
1127 */
1128int drm_helper_hotplug_stage_two(struct drm_device *dev)
1129{
f453ba04
DA
1130 drm_helper_plugged_event(dev);
1131
1132 return 0;
1133}
1134EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1135
1136int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1137 struct drm_mode_fb_cmd *mode_cmd)
1138{
1139 fb->width = mode_cmd->width;
1140 fb->height = mode_cmd->height;
1141 fb->pitch = mode_cmd->pitch;
1142 fb->bits_per_pixel = mode_cmd->bpp;
1143 fb->depth = mode_cmd->depth;
1144
1145 return 0;
1146}
1147EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1148
1149int drm_helper_resume_force_mode(struct drm_device *dev)
1150{
1151 struct drm_crtc *crtc;
1152 int ret;
1153
1154 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1155
1156 if (!crtc->enabled)
1157 continue;
1158
3c4fdcfb
KH
1159 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1160 crtc->x, crtc->y, crtc->fb);
f453ba04
DA
1161
1162 if (ret == false)
1163 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1164 }
af4fcb57
ZY
1165 /* disable the unused connectors while restoring the modesetting */
1166 drm_helper_disable_unused_functions(dev);
f453ba04
DA
1167 return 0;
1168}
1169EXPORT_SYMBOL(drm_helper_resume_force_mode);
This page took 0.153197 seconds and 5 git commands to generate.