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