Merge branch 'vendor/GCC50' - gcc 5.0 snapshot 1 FEB 2015
[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                 if (encoder_funcs->dpms)
285                         (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
286 }
287
288 /**
289  * drm_helper_disable_unused_functions - disable unused objects
290  * @dev: DRM device
291  *
292  * LOCKING:
293  * Caller must hold mode config lock.
294  *
295  * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
296  * by calling its dpms function, which should power it off.
297  */
298 void drm_helper_disable_unused_functions(struct drm_device *dev)
299 {
300         struct drm_encoder *encoder;
301         struct drm_connector *connector;
302         struct drm_crtc *crtc;
303
304         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
305                 if (!connector->encoder)
306                         continue;
307                 if (connector->status == connector_status_disconnected)
308                         connector->encoder = NULL;
309         }
310
311         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
312                 if (!drm_helper_encoder_in_use(encoder)) {
313                         drm_encoder_disable(encoder);
314                         /* disconnector encoder from any connector */
315                         encoder->crtc = NULL;
316                 }
317         }
318
319         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
320                 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
321                 crtc->enabled = drm_helper_crtc_in_use(crtc);
322                 if (!crtc->enabled) {
323                         if (crtc_funcs->disable)
324                                 (*crtc_funcs->disable)(crtc);
325                         else
326                                 if (crtc_funcs->dpms)
327                                         (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
328                         crtc->fb = NULL;
329                 }
330         }
331 }
332 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
333
334 /**
335  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
336  * @encoder: encoder to test
337  * @crtc: crtc to test
338  *
339  * Return false if @encoder can't be driven by @crtc, true otherwise.
340  */
341 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
342                                 struct drm_crtc *crtc)
343 {
344         struct drm_device *dev;
345         struct drm_crtc *tmp;
346         int crtc_mask = 1;
347
348         WARN(!crtc, "checking null crtc?\n");
349
350         dev = crtc->dev;
351
352         list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
353                 if (tmp == crtc)
354                         break;
355                 crtc_mask <<= 1;
356         }
357
358         if (encoder->possible_crtcs & crtc_mask)
359                 return true;
360         return false;
361 }
362
363 /*
364  * Check the CRTC we're going to map each output to vs. its current
365  * CRTC.  If they don't match, we have to disable the output and the CRTC
366  * since the driver will have to re-route things.
367  */
368 static void
369 drm_crtc_prepare_encoders(struct drm_device *dev)
370 {
371         struct drm_encoder_helper_funcs *encoder_funcs;
372         struct drm_encoder *encoder;
373
374         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
375                 encoder_funcs = encoder->helper_private;
376                 /* Disable unused encoders */
377                 if (encoder->crtc == NULL)
378                         drm_encoder_disable(encoder);
379                 /* Disable encoders whose CRTC is about to change */
380                 if (encoder_funcs->get_crtc &&
381                     encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
382                         drm_encoder_disable(encoder);
383         }
384 }
385
386 /**
387  * drm_crtc_helper_set_mode - internal helper to set a mode
388  * @crtc: CRTC to program
389  * @mode: mode to use
390  * @x: horizontal offset into the surface
391  * @y: vertical offset into the surface
392  * @old_fb: old framebuffer, for cleanup
393  *
394  * LOCKING:
395  * Caller must hold mode config lock.
396  *
397  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
398  * to fixup or reject the mode prior to trying to set it. This is an internal
399  * helper that drivers could e.g. use to update properties that require the
400  * entire output pipe to be disabled and re-enabled in a new configuration. For
401  * example for changing whether audio is enabled on a hdmi link or for changing
402  * panel fitter or dither attributes. It is also called by the
403  * drm_crtc_helper_set_config() helper function to drive the mode setting
404  * sequence.
405  *
406  * RETURNS:
407  * True if the mode was set successfully, or false otherwise.
408  */
409 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
410                               struct drm_display_mode *mode,
411                               int x, int y,
412                               struct drm_framebuffer *old_fb)
413 {
414         struct drm_device *dev = crtc->dev;
415         struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
416         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
417         struct drm_encoder_helper_funcs *encoder_funcs;
418         int saved_x, saved_y;
419         struct drm_encoder *encoder;
420         bool ret = true;
421
422         crtc->enabled = drm_helper_crtc_in_use(crtc);
423         if (!crtc->enabled)
424                 return true;
425
426         adjusted_mode = drm_mode_duplicate(dev, mode);
427         if (!adjusted_mode)
428                 return false;
429
430         saved_hwmode = crtc->hwmode;
431         saved_mode = crtc->mode;
432         saved_x = crtc->x;
433         saved_y = crtc->y;
434
435         /* Update crtc values up front so the driver can rely on them for mode
436          * setting.
437          */
438         crtc->mode = *mode;
439         crtc->x = x;
440         crtc->y = y;
441
442         /* Pass our mode to the connectors and the CRTC to give them a chance to
443          * adjust it according to limitations or connector properties, and also
444          * a chance to reject the mode entirely.
445          */
446         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
447
448                 if (encoder->crtc != crtc)
449                         continue;
450                 encoder_funcs = encoder->helper_private;
451                 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
452                                                       adjusted_mode))) {
453                         DRM_DEBUG_KMS("Encoder fixup failed\n");
454                         goto done;
455                 }
456         }
457
458         if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
459                 DRM_DEBUG_KMS("CRTC fixup failed\n");
460                 goto done;
461         }
462         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
463
464         /* Prepare the encoders and CRTCs before setting the mode. */
465         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
466
467                 if (encoder->crtc != crtc)
468                         continue;
469                 encoder_funcs = encoder->helper_private;
470                 /* Disable the encoders as the first thing we do. */
471                 encoder_funcs->prepare(encoder);
472         }
473
474         drm_crtc_prepare_encoders(dev);
475
476         crtc_funcs->prepare(crtc);
477
478         /* Set up the DPLL and any encoders state that needs to adjust or depend
479          * on the DPLL.
480          */
481         ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
482         if (!ret)
483             goto done;
484
485         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
486
487                 if (encoder->crtc != crtc)
488                         continue;
489
490                 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
491                         encoder->base.id, drm_get_encoder_name(encoder),
492                         mode->base.id, mode->name);
493                 encoder_funcs = encoder->helper_private;
494                 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
495         }
496
497         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
498         crtc_funcs->commit(crtc);
499
500         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
501
502                 if (encoder->crtc != crtc)
503                         continue;
504
505                 encoder_funcs = encoder->helper_private;
506                 encoder_funcs->commit(encoder);
507
508         }
509
510         /* Store real post-adjustment hardware mode. */
511         crtc->hwmode = *adjusted_mode;
512
513         /* Calculate and store various constants which
514          * are later needed by vblank and swap-completion
515          * timestamping. They are derived from true hwmode.
516          */
517         drm_calc_timestamping_constants(crtc);
518
519         /* FIXME: add subpixel order */
520 done:
521         drm_mode_destroy(dev, adjusted_mode);
522         if (!ret) {
523                 crtc->hwmode = saved_hwmode;
524                 crtc->mode = saved_mode;
525                 crtc->x = saved_x;
526                 crtc->y = saved_y;
527         }
528
529         return ret;
530 }
531 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
532
533
534 static int
535 drm_crtc_helper_disable(struct drm_crtc *crtc)
536 {
537         struct drm_device *dev = crtc->dev;
538         struct drm_connector *connector;
539         struct drm_encoder *encoder;
540
541         /* Decouple all encoders and their attached connectors from this crtc */
542         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
543                 if (encoder->crtc != crtc)
544                         continue;
545
546                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
547                         if (connector->encoder != encoder)
548                                 continue;
549
550                         connector->encoder = NULL;
551                 }
552         }
553
554         drm_helper_disable_unused_functions(dev);
555         return 0;
556 }
557
558 /**
559  * drm_crtc_helper_set_config - set a new config from userspace
560  * @set: mode set configuration
561  *
562  * LOCKING:
563  * Caller must hold mode config lock.
564  *
565  * Setup a new configuration, provided by the upper layers (either an ioctl call
566  * from userspace or internally e.g. from the fbdev suppport code) in @set, and
567  * enable it. This is the main helper functions for drivers that implement
568  * kernel mode setting with the crtc helper functions and the assorted
569  * ->prepare(), ->modeset() and ->commit() helper callbacks.
570  *
571  * RETURNS:
572  * Returns 0 on success, -ERRNO on failure.
573  */
574 int drm_crtc_helper_set_config(struct drm_mode_set *set)
575 {
576         struct drm_device *dev;
577         struct drm_crtc *save_crtcs, *new_crtc, *crtc;
578         struct drm_encoder *save_encoders, *new_encoder, *encoder;
579         struct drm_framebuffer *old_fb = NULL;
580         bool mode_changed = false; /* if true do a full mode set */
581         bool fb_changed = false; /* if true and !mode_changed just do a flip */
582         struct drm_connector *save_connectors, *connector;
583         int count = 0, ro, fail = 0;
584         struct drm_crtc_helper_funcs *crtc_funcs;
585         struct drm_mode_set save_set;
586         int ret;
587         int i;
588
589         DRM_DEBUG_KMS("\n");
590
591         if (!set)
592                 return -EINVAL;
593
594         if (!set->crtc)
595                 return -EINVAL;
596
597         if (!set->crtc->helper_private)
598                 return -EINVAL;
599
600         crtc_funcs = set->crtc->helper_private;
601
602         if (!set->mode)
603                 set->fb = NULL;
604
605         if (set->fb) {
606                 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
607                                 set->crtc->base.id, set->fb->base.id,
608                                 (int)set->num_connectors, set->x, set->y);
609         } else {
610                 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
611                 return drm_crtc_helper_disable(set->crtc);
612         }
613
614         dev = set->crtc->dev;
615
616         /* Allocate space for the backup of all (non-pointer) crtc, encoder and
617          * connector data. */
618         save_crtcs = kzalloc(dev->mode_config.num_crtc *
619                              sizeof(struct drm_crtc), GFP_KERNEL);
620         if (!save_crtcs)
621                 return -ENOMEM;
622
623         save_encoders = kzalloc(dev->mode_config.num_encoder *
624                                 sizeof(struct drm_encoder), GFP_KERNEL);
625         if (!save_encoders) {
626                 kfree(save_crtcs);
627                 return -ENOMEM;
628         }
629
630         save_connectors = kzalloc(dev->mode_config.num_connector *
631                                 sizeof(struct drm_connector), GFP_KERNEL);
632         if (!save_connectors) {
633                 kfree(save_crtcs);
634                 kfree(save_encoders);
635                 return -ENOMEM;
636         }
637
638         /* Copy data. Note that driver private data is not affected.
639          * Should anything bad happen only the expected state is
640          * restored, not the drivers personal bookkeeping.
641          */
642         count = 0;
643         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
644                 save_crtcs[count++] = *crtc;
645         }
646
647         count = 0;
648         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
649                 save_encoders[count++] = *encoder;
650         }
651
652         count = 0;
653         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
654                 save_connectors[count++] = *connector;
655         }
656
657         save_set.crtc = set->crtc;
658         save_set.mode = &set->crtc->mode;
659         save_set.x = set->crtc->x;
660         save_set.y = set->crtc->y;
661         save_set.fb = set->crtc->fb;
662
663         /* We should be able to check here if the fb has the same properties
664          * and then just flip_or_move it */
665         if (set->crtc->fb != set->fb) {
666                 /* If we have no fb then treat it as a full mode set */
667                 if (set->crtc->fb == NULL) {
668                         DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
669                         mode_changed = true;
670                 } else if (set->fb == NULL) {
671                         mode_changed = true;
672                 } else if (set->fb->depth != set->crtc->fb->depth) {
673                         mode_changed = true;
674                 } else if (set->fb->bits_per_pixel !=
675                            set->crtc->fb->bits_per_pixel) {
676                         mode_changed = true;
677                 } else if (set->fb->pixel_format !=
678                            set->crtc->fb->pixel_format) {
679                         mode_changed = true;
680                 } else
681                         fb_changed = true;
682         }
683
684         if (set->x != set->crtc->x || set->y != set->crtc->y)
685                 fb_changed = true;
686
687         if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
688                 DRM_DEBUG_KMS("modes are different, full mode set\n");
689                 drm_mode_debug_printmodeline(&set->crtc->mode);
690                 drm_mode_debug_printmodeline(set->mode);
691                 mode_changed = true;
692         }
693
694         /* a) traverse passed in connector list and get encoders for them */
695         count = 0;
696         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
697                 struct drm_connector_helper_funcs *connector_funcs =
698                         connector->helper_private;
699                 new_encoder = connector->encoder;
700                 for (ro = 0; ro < set->num_connectors; ro++) {
701                         if (set->connectors[ro] == connector) {
702                                 new_encoder = connector_funcs->best_encoder(connector);
703                                 /* if we can't get an encoder for a connector
704                                    we are setting now - then fail */
705                                 if (new_encoder == NULL)
706                                         /* don't break so fail path works correct */
707                                         fail = 1;
708                                 break;
709                         }
710                 }
711
712                 if (new_encoder != connector->encoder) {
713                         DRM_DEBUG_KMS("encoder changed, full mode switch\n");
714                         mode_changed = true;
715                         /* If the encoder is reused for another connector, then
716                          * the appropriate crtc will be set later.
717                          */
718                         if (connector->encoder)
719                                 connector->encoder->crtc = NULL;
720                         connector->encoder = new_encoder;
721                 }
722         }
723
724         if (fail) {
725                 ret = -EINVAL;
726                 goto fail;
727         }
728
729         count = 0;
730         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
731                 if (!connector->encoder)
732                         continue;
733
734                 if (connector->encoder->crtc == set->crtc)
735                         new_crtc = NULL;
736                 else
737                         new_crtc = connector->encoder->crtc;
738
739                 for (ro = 0; ro < set->num_connectors; ro++) {
740                         if (set->connectors[ro] == connector)
741                                 new_crtc = set->crtc;
742                 }
743
744                 /* Make sure the new CRTC will work with the encoder */
745                 if (new_crtc &&
746                     !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
747                         ret = -EINVAL;
748                         goto fail;
749                 }
750                 if (new_crtc != connector->encoder->crtc) {
751                         DRM_DEBUG_KMS("crtc changed, full mode switch\n");
752                         mode_changed = true;
753                         connector->encoder->crtc = new_crtc;
754                 }
755                 if (new_crtc) {
756                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
757                                 connector->base.id, drm_get_connector_name(connector),
758                                 new_crtc->base.id);
759                 } else {
760                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
761                                 connector->base.id, drm_get_connector_name(connector));
762                 }
763         }
764
765         /* mode_set_base is not a required function */
766         if (fb_changed && !crtc_funcs->mode_set_base)
767                 mode_changed = true;
768
769         if (mode_changed) {
770                 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
771                 if (set->crtc->enabled) {
772                         DRM_DEBUG_KMS("attempting to set mode from"
773                                         " userspace\n");
774                         drm_mode_debug_printmodeline(set->mode);
775                         old_fb = set->crtc->fb;
776                         set->crtc->fb = set->fb;
777                         if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
778                                                       set->x, set->y,
779                                                       old_fb)) {
780                                 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
781                                           set->crtc->base.id);
782                                 set->crtc->fb = old_fb;
783                                 ret = -EINVAL;
784                                 goto fail;
785                         }
786                         DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
787                         for (i = 0; i < set->num_connectors; i++) {
788                                 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
789                                               drm_get_connector_name(set->connectors[i]));
790                                 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
791                         }
792                 }
793                 drm_helper_disable_unused_functions(dev);
794         } else if (fb_changed) {
795                 set->crtc->x = set->x;
796                 set->crtc->y = set->y;
797
798                 old_fb = set->crtc->fb;
799                 if (set->crtc->fb != set->fb)
800                         set->crtc->fb = set->fb;
801                 ret = crtc_funcs->mode_set_base(set->crtc,
802                                                 set->x, set->y, old_fb);
803                 if (ret != 0) {
804                         set->crtc->fb = old_fb;
805                         goto fail;
806                 }
807         }
808
809         kfree(save_connectors);
810         kfree(save_encoders);
811         kfree(save_crtcs);
812         return 0;
813
814 fail:
815         /* Restore all previous data. */
816         count = 0;
817         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
818                 *crtc = save_crtcs[count++];
819         }
820
821         count = 0;
822         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
823                 *encoder = save_encoders[count++];
824         }
825
826         count = 0;
827         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
828                 *connector = save_connectors[count++];
829         }
830
831         /* Try to restore the config */
832         if (mode_changed &&
833             !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
834                                       save_set.y, save_set.fb))
835                 DRM_ERROR("failed to restore config after modeset failure\n");
836
837         kfree(save_connectors);
838         kfree(save_encoders);
839         kfree(save_crtcs);
840         return ret;
841 }
842 EXPORT_SYMBOL(drm_crtc_helper_set_config);
843
844 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
845 {
846         int dpms = DRM_MODE_DPMS_OFF;
847         struct drm_connector *connector;
848         struct drm_device *dev = encoder->dev;
849
850         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
851                 if (connector->encoder == encoder)
852                         if (connector->dpms < dpms)
853                                 dpms = connector->dpms;
854         return dpms;
855 }
856
857 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
858 {
859         int dpms = DRM_MODE_DPMS_OFF;
860         struct drm_connector *connector;
861         struct drm_device *dev = crtc->dev;
862
863         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
864                 if (connector->encoder && connector->encoder->crtc == crtc)
865                         if (connector->dpms < dpms)
866                                 dpms = connector->dpms;
867         return dpms;
868 }
869
870 /**
871  * drm_helper_connector_dpms() - connector dpms helper implementation
872  * @connector: affected connector
873  * @mode: DPMS mode
874  *
875  * This is the main helper function provided by the crtc helper framework for
876  * implementing the DPMS connector attribute. It computes the new desired DPMS
877  * state for all encoders and crtcs in the output mesh and calls the ->dpms()
878  * callback provided by the driver appropriately.
879  */
880 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
881 {
882         struct drm_encoder *encoder = connector->encoder;
883         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
884         int old_dpms;
885
886         if (mode == connector->dpms)
887                 return;
888
889         old_dpms = connector->dpms;
890         connector->dpms = mode;
891
892         /* from off to on, do crtc then encoder */
893         if (mode < old_dpms) {
894                 if (crtc) {
895                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
896                         if (crtc_funcs->dpms)
897                                 (*crtc_funcs->dpms) (crtc,
898                                                      drm_helper_choose_crtc_dpms(crtc));
899                 }
900                 if (encoder) {
901                         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
902                         if (encoder_funcs->dpms)
903                                 (*encoder_funcs->dpms) (encoder,
904                                                         drm_helper_choose_encoder_dpms(encoder));
905                 }
906         }
907
908         /* from on to off, do encoder then crtc */
909         if (mode > old_dpms) {
910                 if (encoder) {
911                         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
912                         if (encoder_funcs->dpms)
913                                 (*encoder_funcs->dpms) (encoder,
914                                                         drm_helper_choose_encoder_dpms(encoder));
915                 }
916                 if (crtc) {
917                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
918                         if (crtc_funcs->dpms)
919                                 (*crtc_funcs->dpms) (crtc,
920                                                      drm_helper_choose_crtc_dpms(crtc));
921                 }
922         }
923
924         return;
925 }
926 EXPORT_SYMBOL(drm_helper_connector_dpms);
927
928 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
929                                    struct drm_mode_fb_cmd2 *mode_cmd)
930 {
931         int i;
932
933         fb->width = mode_cmd->width;
934         fb->height = mode_cmd->height;
935         for (i = 0; i < 4; i++) {
936                 fb->pitches[i] = mode_cmd->pitches[i];
937                 fb->offsets[i] = mode_cmd->offsets[i];
938         }
939         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
940                                     &fb->bits_per_pixel);
941         fb->pixel_format = mode_cmd->pixel_format;
942
943         return 0;
944 }
945 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
946
947 int drm_helper_resume_force_mode(struct drm_device *dev)
948 {
949         struct drm_crtc *crtc;
950         struct drm_encoder *encoder;
951         struct drm_encoder_helper_funcs *encoder_funcs;
952         struct drm_crtc_helper_funcs *crtc_funcs;
953         int ret;
954
955         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
956
957                 if (!crtc->enabled)
958                         continue;
959
960                 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
961                                                crtc->x, crtc->y, crtc->fb);
962
963                 if (ret == false)
964                         DRM_ERROR("failed to set mode on crtc %p\n", crtc);
965
966                 /* Turn off outputs that were already powered off */
967                 if (drm_helper_choose_crtc_dpms(crtc)) {
968                         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
969
970                                 if(encoder->crtc != crtc)
971                                         continue;
972
973                                 encoder_funcs = encoder->helper_private;
974                                 if (encoder_funcs->dpms)
975                                         (*encoder_funcs->dpms) (encoder,
976                                                                 drm_helper_choose_encoder_dpms(encoder));
977                         }
978
979                         crtc_funcs = crtc->helper_private;
980                         if (crtc_funcs->dpms)
981                                 (*crtc_funcs->dpms) (crtc,
982                                                      drm_helper_choose_crtc_dpms(crtc));
983                 }
984         }
985         /* disable the unused connectors while restoring the modesetting */
986         drm_helper_disable_unused_functions(dev);
987         return 0;
988 }
989 EXPORT_SYMBOL(drm_helper_resume_force_mode);
990
991 void drm_kms_helper_hotplug_event(struct drm_device *dev)
992 {
993         /* send a uevent + call fbdev */
994 #if 0
995         drm_sysfs_hotplug_event(dev);
996 #endif
997         if (dev->mode_config.funcs->output_poll_changed)
998                 dev->mode_config.funcs->output_poll_changed(dev);
999 }
1000 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
1001
1002 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
1003 static void output_poll_execute(struct work_struct *work)
1004 {
1005         struct delayed_work *delayed_work = to_delayed_work(work);
1006         struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
1007         struct drm_connector *connector;
1008         enum drm_connector_status old_status;
1009         bool repoll = false, changed = false;
1010
1011         if (!drm_kms_helper_poll)
1012                 return;
1013
1014         mutex_lock(&dev->mode_config.mutex);
1015         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1016
1017                 /* Ignore forced connectors. */
1018                 if (connector->force)
1019                         continue;
1020
1021                 /* Ignore HPD capable connectors and connectors where we don't
1022                  * want any hotplug detection at all for polling. */
1023                 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
1024                         continue;
1025
1026                 repoll = true;
1027
1028                 old_status = connector->status;
1029                 /* if we are connected and don't want to poll for disconnect
1030                    skip it */
1031                 if (old_status == connector_status_connected &&
1032                     !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
1033                         continue;
1034
1035                 connector->status = connector->funcs->detect(connector, false);
1036                 if (old_status != connector->status) {
1037                         const char *old, *new;
1038
1039                         old = drm_get_connector_status_name(old_status);
1040                         new = drm_get_connector_status_name(connector->status);
1041
1042                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
1043                                       "status updated from %s to %s\n",
1044                                       connector->base.id,
1045                                       drm_get_connector_name(connector),
1046                                       old, new);
1047
1048                         changed = true;
1049                 }
1050         }
1051
1052         mutex_unlock(&dev->mode_config.mutex);
1053
1054         if (changed)
1055                 drm_kms_helper_hotplug_event(dev);
1056
1057         if (repoll)
1058                 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
1059 }
1060
1061 void drm_kms_helper_poll_disable(struct drm_device *dev)
1062 {
1063         if (!dev->mode_config.poll_enabled)
1064                 return;
1065         cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
1066 }
1067 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
1068
1069 void drm_kms_helper_poll_enable(struct drm_device *dev)
1070 {
1071         bool poll = false;
1072         struct drm_connector *connector;
1073
1074         if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1075                 return;
1076
1077         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1078                 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1079                                          DRM_CONNECTOR_POLL_DISCONNECT))
1080                         poll = true;
1081         }
1082
1083         if (poll)
1084                 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
1085 }
1086 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1087
1088 void drm_kms_helper_poll_init(struct drm_device *dev)
1089 {
1090         INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
1091         dev->mode_config.poll_enabled = true;
1092
1093         drm_kms_helper_poll_enable(dev);
1094 }
1095 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1096
1097 void drm_kms_helper_poll_fini(struct drm_device *dev)
1098 {
1099         drm_kms_helper_poll_disable(dev);
1100 }
1101 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1102
1103 void drm_helper_hpd_irq_event(struct drm_device *dev)
1104 {
1105         struct drm_connector *connector;
1106         enum drm_connector_status old_status;
1107         bool changed = false;
1108
1109         if (!dev->mode_config.poll_enabled)
1110                 return;
1111
1112         mutex_lock(&dev->mode_config.mutex);
1113         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1114
1115                 /* Only handle HPD capable connectors. */
1116                 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1117                         continue;
1118
1119                 old_status = connector->status;
1120
1121                 connector->status = connector->funcs->detect(connector, false);
1122                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1123                               connector->base.id,
1124                               drm_get_connector_name(connector),
1125                               drm_get_connector_status_name(old_status),
1126                               drm_get_connector_status_name(connector->status));
1127                 if (old_status != connector->status)
1128                         changed = true;
1129         }
1130
1131         mutex_unlock(&dev->mode_config.mutex);
1132
1133         if (changed)
1134                 drm_kms_helper_hotplug_event(dev);
1135 }
1136 EXPORT_SYMBOL(drm_helper_hpd_irq_event);