drm: Handle drm_object_get() failures
[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
2d1a8a48 32#include <linux/export.h>
0603ba14 33#include <linux/moduleparam.h>
2d1a8a48 34
f453ba04
DA
35#include "drmP.h"
36#include "drm_crtc.h"
308e5bcb 37#include "drm_fourcc.h"
f453ba04 38#include "drm_crtc_helper.h"
d50ba256 39#include "drm_fb_helper.h"
f453ba04 40
e58f637b
CW
41static bool drm_kms_helper_poll = true;
42module_param_named(poll, drm_kms_helper_poll, bool, 0600);
43
6714977b 44static void drm_mode_validate_flag(struct drm_connector *connector,
45 int flags)
46{
a1178ca0 47 struct drm_display_mode *mode;
6714977b 48
49 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
50 return;
51
a1178ca0 52 list_for_each_entry(mode, &connector->modes, head) {
6714977b 53 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
54 !(flags & DRM_MODE_FLAG_INTERLACE))
55 mode->status = MODE_NO_INTERLACE;
56 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
57 !(flags & DRM_MODE_FLAG_DBLSCAN))
58 mode->status = MODE_NO_DBLESCAN;
59 }
60
61 return;
62}
63
f453ba04 64/**
38651674 65 * drm_helper_probe_single_connector_modes - get complete set of display modes
f453ba04
DA
66 * @dev: DRM device
67 * @maxX: max width for modes
68 * @maxY: max height for modes
69 *
70 * LOCKING:
71 * Caller must hold mode config lock.
72 *
73 * Based on @dev's mode_config layout, scan all the connectors and try to detect
74 * modes on them. Modes will first be added to the connector's probed_modes
75 * list, then culled (based on validity and the @maxX, @maxY parameters) and
76 * put into the normal modes list.
77 *
78 * Intended to be used either at bootup time or when major configuration
79 * changes have occurred.
80 *
81 * FIXME: take into account monitor limits
40a518d9
JB
82 *
83 * RETURNS:
84 * Number of modes found on @connector.
f453ba04 85 */
40a518d9
JB
86int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
87 uint32_t maxX, uint32_t maxY)
f453ba04
DA
88{
89 struct drm_device *dev = connector->dev;
a1178ca0 90 struct drm_display_mode *mode;
f453ba04
DA
91 struct drm_connector_helper_funcs *connector_funcs =
92 connector->helper_private;
40a518d9 93 int count = 0;
6714977b 94 int mode_flags = 0;
f453ba04 95
9440106b
JG
96 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
97 drm_get_connector_name(connector));
f453ba04 98 /* set all modes to the unverified state */
a1178ca0 99 list_for_each_entry(mode, &connector->modes, head)
f453ba04
DA
100 mode->status = MODE_UNVERIFIED;
101
d50ba256
DA
102 if (connector->force) {
103 if (connector->force == DRM_FORCE_ON)
104 connector->status = connector_status_connected;
105 else
106 connector->status = connector_status_disconnected;
107 if (connector->funcs->force)
108 connector->funcs->force(connector);
e58f637b 109 } else {
930a9e28 110 connector->status = connector->funcs->detect(connector, true);
551402a3 111 drm_kms_helper_poll_enable(dev);
e58f637b 112 }
f453ba04
DA
113
114 if (connector->status == connector_status_disconnected) {
9440106b
JG
115 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
116 connector->base.id, drm_get_connector_name(connector));
72539832 117 drm_mode_connector_update_edid_property(connector, NULL);
620f3781 118 goto prune;
f453ba04
DA
119 }
120
40a518d9 121 count = (*connector_funcs->get_modes)(connector);
c7ef35a9 122 if (count == 0 && connector->status == connector_status_connected)
9632b41f 123 count = drm_add_modes_noedid(connector, 1024, 768);
c7ef35a9
CW
124 if (count == 0)
125 goto prune;
f453ba04 126
40a518d9 127 drm_mode_connector_list_update(connector);
f453ba04
DA
128
129 if (maxX && maxY)
130 drm_mode_validate_size(dev, &connector->modes, maxX,
131 maxY, 0);
6714977b 132
133 if (connector->interlace_allowed)
134 mode_flags |= DRM_MODE_FLAG_INTERLACE;
135 if (connector->doublescan_allowed)
136 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
137 drm_mode_validate_flag(connector, mode_flags);
138
a1178ca0 139 list_for_each_entry(mode, &connector->modes, head) {
f453ba04
DA
140 if (mode->status == MODE_OK)
141 mode->status = connector_funcs->mode_valid(connector,
142 mode);
143 }
144
620f3781 145prune:
f453ba04
DA
146 drm_mode_prune_invalid(dev, &connector->modes, true);
147
40a518d9
JB
148 if (list_empty(&connector->modes))
149 return 0;
f453ba04
DA
150
151 drm_mode_sort(&connector->modes);
152
9440106b
JG
153 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
154 drm_get_connector_name(connector));
a1178ca0 155 list_for_each_entry(mode, &connector->modes, head) {
f453ba04
DA
156 mode->vrefresh = drm_mode_vrefresh(mode);
157
158 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
159 drm_mode_debug_printmodeline(mode);
160 }
40a518d9
JB
161
162 return count;
f453ba04
DA
163}
164EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
165
c9fb15f6
KP
166/**
167 * drm_helper_encoder_in_use - check if a given encoder is in use
168 * @encoder: encoder to check
169 *
170 * LOCKING:
171 * Caller must hold mode config lock.
172 *
173 * Walk @encoders's DRM device's mode_config and see if it's in use.
174 *
175 * RETURNS:
176 * True if @encoder is part of the mode_config, false otherwise.
177 */
178bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
179{
180 struct drm_connector *connector;
181 struct drm_device *dev = encoder->dev;
182 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
183 if (connector->encoder == encoder)
184 return true;
185 return false;
186}
187EXPORT_SYMBOL(drm_helper_encoder_in_use);
188
f453ba04
DA
189/**
190 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
191 * @crtc: CRTC to check
192 *
193 * LOCKING:
194 * Caller must hold mode config lock.
195 *
196 * Walk @crtc's DRM device's mode_config and see if it's in use.
197 *
198 * RETURNS:
199 * True if @crtc is part of the mode_config, false otherwise.
200 */
201bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
202{
203 struct drm_encoder *encoder;
204 struct drm_device *dev = crtc->dev;
205 /* FIXME: Locking around list access? */
206 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
c9fb15f6 207 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
f453ba04
DA
208 return true;
209 return false;
210}
211EXPORT_SYMBOL(drm_helper_crtc_in_use);
212
86a1b9d1
BS
213static void
214drm_encoder_disable(struct drm_encoder *encoder)
215{
216 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
217
218 if (encoder_funcs->disable)
219 (*encoder_funcs->disable)(encoder);
220 else
221 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
222}
223
f453ba04 224/**
89347bb8 225 * drm_helper_disable_unused_functions - disable unused objects
f453ba04
DA
226 * @dev: DRM device
227 *
228 * LOCKING:
229 * Caller must hold mode config lock.
230 *
231 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
232 * by calling its dpms function, which should power it off.
233 */
234void drm_helper_disable_unused_functions(struct drm_device *dev)
235{
236 struct drm_encoder *encoder;
a3a0544b 237 struct drm_connector *connector;
f453ba04
DA
238 struct drm_crtc *crtc;
239
a3a0544b
DA
240 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
241 if (!connector->encoder)
242 continue;
243 if (connector->status == connector_status_disconnected)
244 connector->encoder = NULL;
245 }
246
f453ba04 247 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
92971021 248 if (!drm_helper_encoder_in_use(encoder)) {
86a1b9d1 249 drm_encoder_disable(encoder);
9c552dd7
DA
250 /* disconnector encoder from any connector */
251 encoder->crtc = NULL;
a3a0544b 252 }
f453ba04
DA
253 }
254
255 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
256 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
257 crtc->enabled = drm_helper_crtc_in_use(crtc);
258 if (!crtc->enabled) {
5c8d7171
AD
259 if (crtc_funcs->disable)
260 (*crtc_funcs->disable)(crtc);
261 else
262 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
f453ba04
DA
263 crtc->fb = NULL;
264 }
265 }
266}
267EXPORT_SYMBOL(drm_helper_disable_unused_functions);
268
7bec756c
JB
269/**
270 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
271 * @encoder: encoder to test
272 * @crtc: crtc to test
273 *
274 * Return false if @encoder can't be driven by @crtc, true otherwise.
275 */
276static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
277 struct drm_crtc *crtc)
278{
279 struct drm_device *dev;
280 struct drm_crtc *tmp;
281 int crtc_mask = 1;
282
fce7d61b 283 WARN(!crtc, "checking null crtc?\n");
7bec756c
JB
284
285 dev = crtc->dev;
286
287 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
288 if (tmp == crtc)
289 break;
290 crtc_mask <<= 1;
291 }
292
293 if (encoder->possible_crtcs & crtc_mask)
294 return true;
295 return false;
296}
297
298/*
299 * Check the CRTC we're going to map each output to vs. its current
300 * CRTC. If they don't match, we have to disable the output and the CRTC
301 * since the driver will have to re-route things.
302 */
303static void
304drm_crtc_prepare_encoders(struct drm_device *dev)
305{
306 struct drm_encoder_helper_funcs *encoder_funcs;
307 struct drm_encoder *encoder;
308
309 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
310 encoder_funcs = encoder->helper_private;
311 /* Disable unused encoders */
312 if (encoder->crtc == NULL)
86a1b9d1 313 drm_encoder_disable(encoder);
7bec756c
JB
314 /* Disable encoders whose CRTC is about to change */
315 if (encoder_funcs->get_crtc &&
316 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
86a1b9d1 317 drm_encoder_disable(encoder);
7bec756c
JB
318 }
319}
320
f453ba04
DA
321/**
322 * drm_crtc_set_mode - set a mode
323 * @crtc: CRTC to program
324 * @mode: mode to use
325 * @x: width of mode
326 * @y: height of mode
327 *
328 * LOCKING:
329 * Caller must hold mode config lock.
330 *
331 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
332 * to fixup or reject the mode prior to trying to set it.
333 *
334 * RETURNS:
335 * True if the mode was set successfully, or false otherwise.
336 */
337bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
338 struct drm_display_mode *mode,
3c4fdcfb
KH
339 int x, int y,
340 struct drm_framebuffer *old_fb)
f453ba04
DA
341{
342 struct drm_device *dev = crtc->dev;
27641c3f 343 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
f453ba04
DA
344 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
345 struct drm_encoder_helper_funcs *encoder_funcs;
346 int saved_x, saved_y;
347 struct drm_encoder *encoder;
348 bool ret = true;
349
f453ba04 350 crtc->enabled = drm_helper_crtc_in_use(crtc);
f453ba04
DA
351 if (!crtc->enabled)
352 return true;
353
021a8455 354 adjusted_mode = drm_mode_duplicate(dev, mode);
6bfc56aa
VS
355 if (!adjusted_mode)
356 return false;
021a8455 357
27641c3f 358 saved_hwmode = crtc->hwmode;
f453ba04
DA
359 saved_mode = crtc->mode;
360 saved_x = crtc->x;
361 saved_y = crtc->y;
362
363 /* Update crtc values up front so the driver can rely on them for mode
364 * setting.
365 */
366 crtc->mode = *mode;
367 crtc->x = x;
368 crtc->y = y;
369
f453ba04
DA
370 /* Pass our mode to the connectors and the CRTC to give them a chance to
371 * adjust it according to limitations or connector properties, and also
372 * a chance to reject the mode entirely.
373 */
374 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
375
376 if (encoder->crtc != crtc)
377 continue;
378 encoder_funcs = encoder->helper_private;
379 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
380 adjusted_mode))) {
836e53d7 381 DRM_DEBUG_KMS("Encoder fixup failed\n");
f453ba04
DA
382 goto done;
383 }
384 }
385
386 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
836e53d7 387 DRM_DEBUG_KMS("CRTC fixup failed\n");
f453ba04
DA
388 goto done;
389 }
9440106b 390 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
391
392 /* Prepare the encoders and CRTCs before setting the mode. */
393 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
394
395 if (encoder->crtc != crtc)
396 continue;
397 encoder_funcs = encoder->helper_private;
398 /* Disable the encoders as the first thing we do. */
399 encoder_funcs->prepare(encoder);
400 }
401
7bec756c
JB
402 drm_crtc_prepare_encoders(dev);
403
f453ba04
DA
404 crtc_funcs->prepare(crtc);
405
406 /* Set up the DPLL and any encoders state that needs to adjust or depend
407 * on the DPLL.
408 */
5c3b82e2
CW
409 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
410 if (!ret)
411 goto done;
f453ba04
DA
412
413 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
414
415 if (encoder->crtc != crtc)
416 continue;
417
9440106b
JG
418 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
419 encoder->base.id, drm_get_encoder_name(encoder),
420 mode->base.id, mode->name);
f453ba04
DA
421 encoder_funcs = encoder->helper_private;
422 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
423 }
424
425 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
426 crtc_funcs->commit(crtc);
427
428 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
429
430 if (encoder->crtc != crtc)
431 continue;
432
433 encoder_funcs = encoder->helper_private;
434 encoder_funcs->commit(encoder);
435
436 }
437
27641c3f
MK
438 /* Store real post-adjustment hardware mode. */
439 crtc->hwmode = *adjusted_mode;
440
441 /* Calculate and store various constants which
442 * are later needed by vblank and swap-completion
443 * timestamping. They are derived from true hwmode.
444 */
445 drm_calc_timestamping_constants(crtc);
446
f453ba04
DA
447 /* FIXME: add subpixel order */
448done:
021a8455 449 drm_mode_destroy(dev, adjusted_mode);
f453ba04 450 if (!ret) {
27641c3f 451 crtc->hwmode = saved_hwmode;
f453ba04
DA
452 crtc->mode = saved_mode;
453 crtc->x = saved_x;
454 crtc->y = saved_y;
455 }
456
457 return ret;
458}
459EXPORT_SYMBOL(drm_crtc_helper_set_mode);
460
461
6eebd6bb
CW
462static int
463drm_crtc_helper_disable(struct drm_crtc *crtc)
464{
465 struct drm_device *dev = crtc->dev;
466 struct drm_connector *connector;
467 struct drm_encoder *encoder;
468
469 /* Decouple all encoders and their attached connectors from this crtc */
470 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
471 if (encoder->crtc != crtc)
472 continue;
473
474 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
475 if (connector->encoder != encoder)
476 continue;
477
478 connector->encoder = NULL;
479 }
480 }
481
482 drm_helper_disable_unused_functions(dev);
483 return 0;
484}
485
f453ba04
DA
486/**
487 * drm_crtc_helper_set_config - set a new config from userspace
488 * @crtc: CRTC to setup
489 * @crtc_info: user provided configuration
490 * @new_mode: new mode to set
491 * @connector_set: set of connectors for the new config
492 * @fb: new framebuffer
493 *
494 * LOCKING:
495 * Caller must hold mode config lock.
496 *
497 * Setup a new configuration, provided by the user in @crtc_info, and enable
498 * it.
499 *
500 * RETURNS:
501 * Zero. (FIXME)
502 */
503int drm_crtc_helper_set_config(struct drm_mode_set *set)
504{
505 struct drm_device *dev;
e67aae79
MM
506 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
507 struct drm_encoder *save_encoders, *new_encoder, *encoder;
7bec756c 508 struct drm_framebuffer *old_fb = NULL;
4cb72b17
JB
509 bool mode_changed = false; /* if true do a full mode set */
510 bool fb_changed = false; /* if true and !mode_changed just do a flip */
e67aae79 511 struct drm_connector *save_connectors, *connector;
f453ba04
DA
512 int count = 0, ro, fail = 0;
513 struct drm_crtc_helper_funcs *crtc_funcs;
c5006cfe 514 struct drm_mode_set save_set;
f453ba04 515 int ret = 0;
bf9dc102 516 int i;
f453ba04 517
58367ed6 518 DRM_DEBUG_KMS("\n");
f453ba04
DA
519
520 if (!set)
521 return -EINVAL;
522
523 if (!set->crtc)
524 return -EINVAL;
525
526 if (!set->crtc->helper_private)
527 return -EINVAL;
528
529 crtc_funcs = set->crtc->helper_private;
530
ede3ff52
CW
531 if (!set->mode)
532 set->fb = NULL;
533
9440106b
JG
534 if (set->fb) {
535 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
536 set->crtc->base.id, set->fb->base.id,
537 (int)set->num_connectors, set->x, set->y);
538 } else {
ede3ff52 539 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
6eebd6bb 540 return drm_crtc_helper_disable(set->crtc);
9440106b 541 }
f453ba04
DA
542
543 dev = set->crtc->dev;
544
e67aae79
MM
545 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
546 * connector data. */
547 save_crtcs = kzalloc(dev->mode_config.num_crtc *
548 sizeof(struct drm_crtc), GFP_KERNEL);
f453ba04
DA
549 if (!save_crtcs)
550 return -ENOMEM;
551
e67aae79
MM
552 save_encoders = kzalloc(dev->mode_config.num_encoder *
553 sizeof(struct drm_encoder), GFP_KERNEL);
f453ba04
DA
554 if (!save_encoders) {
555 kfree(save_crtcs);
556 return -ENOMEM;
557 }
558
e67aae79
MM
559 save_connectors = kzalloc(dev->mode_config.num_connector *
560 sizeof(struct drm_connector), GFP_KERNEL);
561 if (!save_connectors) {
562 kfree(save_crtcs);
563 kfree(save_encoders);
564 return -ENOMEM;
565 }
566
567 /* Copy data. Note that driver private data is not affected.
568 * Should anything bad happen only the expected state is
569 * restored, not the drivers personal bookkeeping.
570 */
571 count = 0;
572 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
573 save_crtcs[count++] = *crtc;
574 }
575
576 count = 0;
577 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
578 save_encoders[count++] = *encoder;
579 }
580
581 count = 0;
582 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
583 save_connectors[count++] = *connector;
584 }
585
c5006cfe
JB
586 save_set.crtc = set->crtc;
587 save_set.mode = &set->crtc->mode;
588 save_set.x = set->crtc->x;
589 save_set.y = set->crtc->y;
590 save_set.fb = set->crtc->fb;
591
f453ba04
DA
592 /* We should be able to check here if the fb has the same properties
593 * and then just flip_or_move it */
594 if (set->crtc->fb != set->fb) {
712531bf 595 /* If we have no fb then treat it as a full mode set */
7bec756c 596 if (set->crtc->fb == NULL) {
58367ed6 597 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
712531bf 598 mode_changed = true;
4cb72b17
JB
599 } else if (set->fb == NULL) {
600 mode_changed = true;
46e48456
JB
601 } else if (set->fb->depth != set->crtc->fb->depth) {
602 mode_changed = true;
603 } else if (set->fb->bits_per_pixel !=
604 set->crtc->fb->bits_per_pixel) {
605 mode_changed = true;
8dff4742 606 } else
712531bf 607 fb_changed = true;
f453ba04
DA
608 }
609
610 if (set->x != set->crtc->x || set->y != set->crtc->y)
712531bf 611 fb_changed = true;
f453ba04
DA
612
613 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
58367ed6 614 DRM_DEBUG_KMS("modes are different, full mode set\n");
f453ba04
DA
615 drm_mode_debug_printmodeline(&set->crtc->mode);
616 drm_mode_debug_printmodeline(set->mode);
712531bf 617 mode_changed = true;
f453ba04
DA
618 }
619
620 /* a) traverse passed in connector list and get encoders for them */
621 count = 0;
622 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
623 struct drm_connector_helper_funcs *connector_funcs =
624 connector->helper_private;
f453ba04
DA
625 new_encoder = connector->encoder;
626 for (ro = 0; ro < set->num_connectors; ro++) {
627 if (set->connectors[ro] == connector) {
628 new_encoder = connector_funcs->best_encoder(connector);
629 /* if we can't get an encoder for a connector
630 we are setting now - then fail */
631 if (new_encoder == NULL)
632 /* don't break so fail path works correct */
633 fail = 1;
634 break;
635 }
636 }
637
638 if (new_encoder != connector->encoder) {
58367ed6 639 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
712531bf 640 mode_changed = true;
ff846ab7
MM
641 /* If the encoder is reused for another connector, then
642 * the appropriate crtc will be set later.
643 */
ff6fdbed
MM
644 if (connector->encoder)
645 connector->encoder->crtc = NULL;
f453ba04
DA
646 connector->encoder = new_encoder;
647 }
648 }
649
650 if (fail) {
651 ret = -EINVAL;
e67aae79 652 goto fail;
f453ba04
DA
653 }
654
655 count = 0;
656 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
657 if (!connector->encoder)
658 continue;
659
f453ba04
DA
660 if (connector->encoder->crtc == set->crtc)
661 new_crtc = NULL;
662 else
663 new_crtc = connector->encoder->crtc;
664
665 for (ro = 0; ro < set->num_connectors; ro++) {
666 if (set->connectors[ro] == connector)
667 new_crtc = set->crtc;
668 }
7bec756c
JB
669
670 /* Make sure the new CRTC will work with the encoder */
671 if (new_crtc &&
672 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
673 ret = -EINVAL;
e67aae79 674 goto fail;
7bec756c 675 }
f453ba04 676 if (new_crtc != connector->encoder->crtc) {
58367ed6 677 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
712531bf 678 mode_changed = true;
f453ba04
DA
679 connector->encoder->crtc = new_crtc;
680 }
9440106b
JG
681 if (new_crtc) {
682 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
683 connector->base.id, drm_get_connector_name(connector),
684 new_crtc->base.id);
685 } else {
686 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
687 connector->base.id, drm_get_connector_name(connector));
688 }
f453ba04
DA
689 }
690
691 /* mode_set_base is not a required function */
712531bf
JB
692 if (fb_changed && !crtc_funcs->mode_set_base)
693 mode_changed = true;
f453ba04 694
712531bf 695 if (mode_changed) {
9334ef75
CW
696 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
697 if (set->crtc->enabled) {
58367ed6
ZY
698 DRM_DEBUG_KMS("attempting to set mode from"
699 " userspace\n");
f453ba04 700 drm_mode_debug_printmodeline(set->mode);
356ad3cd
CW
701 old_fb = set->crtc->fb;
702 set->crtc->fb = set->fb;
f453ba04 703 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
3c4fdcfb
KH
704 set->x, set->y,
705 old_fb)) {
9440106b
JG
706 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
707 set->crtc->base.id);
0ba41e44 708 set->crtc->fb = old_fb;
f453ba04 709 ret = -EINVAL;
e67aae79 710 goto fail;
f453ba04 711 }
811aaa55
KP
712 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
713 for (i = 0; i < set->num_connectors; i++) {
714 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
715 drm_get_connector_name(set->connectors[i]));
c7548837 716 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
811aaa55 717 }
f453ba04
DA
718 }
719 drm_helper_disable_unused_functions(dev);
712531bf 720 } else if (fb_changed) {
9b1596af
BS
721 set->crtc->x = set->x;
722 set->crtc->y = set->y;
723
3c4fdcfb 724 old_fb = set->crtc->fb;
f453ba04
DA
725 if (set->crtc->fb != set->fb)
726 set->crtc->fb = set->fb;
5c3b82e2
CW
727 ret = crtc_funcs->mode_set_base(set->crtc,
728 set->x, set->y, old_fb);
0ba41e44
CW
729 if (ret != 0) {
730 set->crtc->fb = old_fb;
e67aae79 731 goto fail;
0ba41e44 732 }
f453ba04
DA
733 }
734
e67aae79 735 kfree(save_connectors);
f453ba04
DA
736 kfree(save_encoders);
737 kfree(save_crtcs);
738 return 0;
739
e67aae79
MM
740fail:
741 /* Restore all previous data. */
f453ba04 742 count = 0;
e67aae79
MM
743 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
744 *crtc = save_crtcs[count++];
745 }
e62fb64e 746
e67aae79
MM
747 count = 0;
748 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
749 *encoder = save_encoders[count++];
e62fb64e 750 }
e67aae79 751
f453ba04
DA
752 count = 0;
753 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
e67aae79 754 *connector = save_connectors[count++];
f453ba04 755 }
e67aae79 756
c5006cfe
JB
757 /* Try to restore the config */
758 if (mode_changed &&
759 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
760 save_set.y, save_set.fb))
761 DRM_ERROR("failed to restore config after modeset failure\n");
762
e67aae79 763 kfree(save_connectors);
f453ba04 764 kfree(save_encoders);
e67aae79 765 kfree(save_crtcs);
f453ba04
DA
766 return ret;
767}
768EXPORT_SYMBOL(drm_crtc_helper_set_config);
769
c9fb15f6
KP
770static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
771{
772 int dpms = DRM_MODE_DPMS_OFF;
773 struct drm_connector *connector;
774 struct drm_device *dev = encoder->dev;
775
776 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
777 if (connector->encoder == encoder)
778 if (connector->dpms < dpms)
779 dpms = connector->dpms;
780 return dpms;
781}
782
783static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
784{
785 int dpms = DRM_MODE_DPMS_OFF;
786 struct drm_connector *connector;
787 struct drm_device *dev = crtc->dev;
788
789 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
790 if (connector->encoder && connector->encoder->crtc == crtc)
791 if (connector->dpms < dpms)
792 dpms = connector->dpms;
793 return dpms;
794}
795
796/**
797 * drm_helper_connector_dpms
798 * @connector affected connector
799 * @mode DPMS mode
800 *
801 * Calls the low-level connector DPMS function, then
802 * calls appropriate encoder and crtc DPMS functions as well
803 */
804void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
805{
806 struct drm_encoder *encoder = connector->encoder;
807 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
808 int old_dpms;
809
810 if (mode == connector->dpms)
811 return;
812
813 old_dpms = connector->dpms;
814 connector->dpms = mode;
815
816 /* from off to on, do crtc then encoder */
817 if (mode < old_dpms) {
818 if (crtc) {
819 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
820 if (crtc_funcs->dpms)
821 (*crtc_funcs->dpms) (crtc,
822 drm_helper_choose_crtc_dpms(crtc));
823 }
824 if (encoder) {
825 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
826 if (encoder_funcs->dpms)
827 (*encoder_funcs->dpms) (encoder,
828 drm_helper_choose_encoder_dpms(encoder));
829 }
830 }
831
832 /* from on to off, do encoder then crtc */
833 if (mode > old_dpms) {
834 if (encoder) {
835 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
836 if (encoder_funcs->dpms)
837 (*encoder_funcs->dpms) (encoder,
838 drm_helper_choose_encoder_dpms(encoder));
839 }
840 if (crtc) {
841 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
842 if (crtc_funcs->dpms)
843 (*crtc_funcs->dpms) (crtc,
844 drm_helper_choose_crtc_dpms(crtc));
845 }
846 }
847
848 return;
849}
850EXPORT_SYMBOL(drm_helper_connector_dpms);
851
f453ba04 852int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
308e5bcb 853 struct drm_mode_fb_cmd2 *mode_cmd)
f453ba04 854{
01f2c773
VS
855 int i;
856
f453ba04
DA
857 fb->width = mode_cmd->width;
858 fb->height = mode_cmd->height;
01f2c773
VS
859 for (i = 0; i < 4; i++) {
860 fb->pitches[i] = mode_cmd->pitches[i];
861 fb->offsets[i] = mode_cmd->offsets[i];
862 }
248dbc23 863 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
308e5bcb
JB
864 &fb->bits_per_pixel);
865 fb->pixel_format = mode_cmd->pixel_format;
f453ba04
DA
866
867 return 0;
868}
869EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
870
871int drm_helper_resume_force_mode(struct drm_device *dev)
872{
873 struct drm_crtc *crtc;
89347bb8
DJ
874 struct drm_encoder *encoder;
875 struct drm_encoder_helper_funcs *encoder_funcs;
876 struct drm_crtc_helper_funcs *crtc_funcs;
f453ba04
DA
877 int ret;
878
879 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
880
881 if (!crtc->enabled)
882 continue;
883
3c4fdcfb
KH
884 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
885 crtc->x, crtc->y, crtc->fb);
f453ba04
DA
886
887 if (ret == false)
888 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
89347bb8
DJ
889
890 /* Turn off outputs that were already powered off */
891 if (drm_helper_choose_crtc_dpms(crtc)) {
892 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
893
894 if(encoder->crtc != crtc)
895 continue;
896
897 encoder_funcs = encoder->helper_private;
898 if (encoder_funcs->dpms)
899 (*encoder_funcs->dpms) (encoder,
900 drm_helper_choose_encoder_dpms(encoder));
89347bb8 901 }
817e631e
CW
902
903 crtc_funcs = crtc->helper_private;
904 if (crtc_funcs->dpms)
905 (*crtc_funcs->dpms) (crtc,
906 drm_helper_choose_crtc_dpms(crtc));
89347bb8 907 }
f453ba04 908 }
af4fcb57
ZY
909 /* disable the unused connectors while restoring the modesetting */
910 drm_helper_disable_unused_functions(dev);
f453ba04
DA
911 return 0;
912}
913EXPORT_SYMBOL(drm_helper_resume_force_mode);
eb1f8e4f 914
eb1f8e4f 915#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
991ea75c 916static void output_poll_execute(struct work_struct *work)
eb1f8e4f 917{
991ea75c
TH
918 struct delayed_work *delayed_work = to_delayed_work(work);
919 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
eb1f8e4f 920 struct drm_connector *connector;
c5027dec 921 enum drm_connector_status old_status;
eb1f8e4f 922 bool repoll = false, changed = false;
eb1f8e4f 923
e58f637b
CW
924 if (!drm_kms_helper_poll)
925 return;
926
eb1f8e4f
DA
927 mutex_lock(&dev->mode_config.mutex);
928 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
929
930 /* if this is HPD or polled don't check it -
931 TV out for instance */
932 if (!connector->polled)
933 continue;
934
935 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
936 repoll = true;
937
938 old_status = connector->status;
939 /* if we are connected and don't want to poll for disconnect
940 skip it */
941 if (old_status == connector_status_connected &&
942 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
943 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
944 continue;
945
c5027dec 946 connector->status = connector->funcs->detect(connector, false);
0f16830e
CW
947 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
948 connector->base.id,
949 drm_get_connector_name(connector),
950 old_status, connector->status);
c5027dec 951 if (old_status != connector->status)
eb1f8e4f
DA
952 changed = true;
953 }
954
955 mutex_unlock(&dev->mode_config.mutex);
956
957 if (changed) {
958 /* send a uevent + call fbdev */
959 drm_sysfs_hotplug_event(dev);
960 if (dev->mode_config.funcs->output_poll_changed)
961 dev->mode_config.funcs->output_poll_changed(dev);
962 }
963
9a919c46
TH
964 if (repoll)
965 queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
eb1f8e4f
DA
966}
967
fbf81762
DA
968void drm_kms_helper_poll_disable(struct drm_device *dev)
969{
970 if (!dev->mode_config.poll_enabled)
971 return;
991ea75c 972 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
fbf81762
DA
973}
974EXPORT_SYMBOL(drm_kms_helper_poll_disable);
975
976void drm_kms_helper_poll_enable(struct drm_device *dev)
eb1f8e4f 977{
eb1f8e4f 978 bool poll = false;
fbf81762 979 struct drm_connector *connector;
eb1f8e4f 980
e58f637b
CW
981 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
982 return;
983
eb1f8e4f
DA
984 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
985 if (connector->polled)
986 poll = true;
987 }
eb1f8e4f 988
9a919c46
TH
989 if (poll)
990 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
eb1f8e4f 991}
fbf81762
DA
992EXPORT_SYMBOL(drm_kms_helper_poll_enable);
993
994void drm_kms_helper_poll_init(struct drm_device *dev)
995{
991ea75c 996 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
fbf81762
DA
997 dev->mode_config.poll_enabled = true;
998
999 drm_kms_helper_poll_enable(dev);
1000}
eb1f8e4f
DA
1001EXPORT_SYMBOL(drm_kms_helper_poll_init);
1002
1003void drm_kms_helper_poll_fini(struct drm_device *dev)
1004{
fbf81762 1005 drm_kms_helper_poll_disable(dev);
eb1f8e4f
DA
1006}
1007EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1008
1009void drm_helper_hpd_irq_event(struct drm_device *dev)
1010{
1011 if (!dev->mode_config.poll_enabled)
1012 return;
e58f637b 1013
991ea75c
TH
1014 /* kill timer and schedule immediate execution, this doesn't block */
1015 cancel_delayed_work(&dev->mode_config.output_poll_work);
e58f637b
CW
1016 if (drm_kms_helper_poll)
1017 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, 0);
eb1f8e4f
DA
1018}
1019EXPORT_SYMBOL(drm_helper_hpd_irq_event);
d0d110e0
VS
1020
1021
1022/**
1023 * drm_format_num_planes - get the number of planes for format
1024 * @format: pixel format (DRM_FORMAT_*)
1025 *
1026 * RETURNS:
1027 * The number of planes used by the specified pixel format.
1028 */
1029int drm_format_num_planes(uint32_t format)
1030{
1031 switch (format) {
1032 case DRM_FORMAT_YUV410:
1033 case DRM_FORMAT_YVU410:
1034 case DRM_FORMAT_YUV411:
1035 case DRM_FORMAT_YVU411:
1036 case DRM_FORMAT_YUV420:
1037 case DRM_FORMAT_YVU420:
1038 case DRM_FORMAT_YUV422:
1039 case DRM_FORMAT_YVU422:
1040 case DRM_FORMAT_YUV444:
1041 case DRM_FORMAT_YVU444:
1042 return 3;
1043 case DRM_FORMAT_NV12:
1044 case DRM_FORMAT_NV21:
1045 case DRM_FORMAT_NV16:
1046 case DRM_FORMAT_NV61:
1047 return 2;
1048 default:
1049 return 1;
1050 }
1051}
1052EXPORT_SYMBOL(drm_format_num_planes);
This page took 0.274702 seconds and 5 git commands to generate.