drm/i915: properly reenable gen8 pipe IRQs
[dragonfly.git] / sys / dev / drm / drm_probe_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 /**
43  * DOC: output probing helper overview
44  *
45  * This library provides some helper code for output probing. It provides an
46  * implementation of the core connector->fill_modes interface with
47  * drm_helper_probe_single_connector_modes.
48  *
49  * It also provides support for polling connectors with a work item and for
50  * generic hotplug interrupt handling where the driver doesn't or cannot keep
51  * track of a per-connector hpd interrupt.
52  *
53  * This helper library can be used independently of the modeset helper library.
54  * Drivers can also overwrite different parts e.g. use their own hotplug
55  * handling code to avoid probing unrelated outputs.
56  */
57
58 static bool drm_kms_helper_poll = true;
59 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
60
61 static void drm_mode_validate_flag(struct drm_connector *connector,
62                                    int flags)
63 {
64         struct drm_display_mode *mode;
65
66         if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
67                       DRM_MODE_FLAG_3D_MASK))
68                 return;
69
70         list_for_each_entry(mode, &connector->modes, head) {
71                 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
72                                 !(flags & DRM_MODE_FLAG_INTERLACE))
73                         mode->status = MODE_NO_INTERLACE;
74                 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
75                                 !(flags & DRM_MODE_FLAG_DBLSCAN))
76                         mode->status = MODE_NO_DBLESCAN;
77                 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
78                                 !(flags & DRM_MODE_FLAG_3D_MASK))
79                         mode->status = MODE_NO_STEREO;
80         }
81
82         return;
83 }
84
85 static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector *connector,
86                                                               uint32_t maxX, uint32_t maxY, bool merge_type_bits)
87 {
88         struct drm_device *dev = connector->dev;
89         struct drm_display_mode *mode;
90         struct drm_connector_helper_funcs *connector_funcs =
91                 connector->helper_private;
92         int count = 0;
93         int mode_flags = 0;
94         bool verbose_prune = true;
95
96         WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
97
98         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
99                         connector->name);
100         /* set all modes to the unverified state */
101         list_for_each_entry(mode, &connector->modes, head)
102                 mode->status = MODE_UNVERIFIED;
103
104         if (connector->force) {
105                 if (connector->force == DRM_FORCE_ON)
106                         connector->status = connector_status_connected;
107                 else
108                         connector->status = connector_status_disconnected;
109                 if (connector->funcs->force)
110                         connector->funcs->force(connector);
111         } else {
112                 connector->status = connector->funcs->detect(connector, true);
113         }
114
115         /* Re-enable polling in case the global poll config changed. */
116         if (drm_kms_helper_poll != dev->mode_config.poll_running)
117                 drm_kms_helper_poll_enable(dev);
118
119         dev->mode_config.poll_running = drm_kms_helper_poll;
120
121         if (connector->status == connector_status_disconnected) {
122                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
123                         connector->base.id, connector->name);
124                 drm_mode_connector_update_edid_property(connector, NULL);
125                 verbose_prune = false;
126                 goto prune;
127         }
128
129 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
130         count = drm_load_edid_firmware(connector);
131         if (count == 0)
132 #endif
133         {
134                 if (connector->override_edid) {
135                         struct edid *edid = (struct edid *) connector->edid_blob_ptr->data;
136
137                         count = drm_add_edid_modes(connector, edid);
138                 } else
139                         count = (*connector_funcs->get_modes)(connector);
140         }
141
142         if (count == 0 && connector->status == connector_status_connected)
143                 count = drm_add_modes_noedid(connector, 1024, 768);
144         if (count == 0)
145                 goto prune;
146
147         drm_mode_connector_list_update(connector, merge_type_bits);
148
149         if (maxX && maxY)
150                 drm_mode_validate_size(dev, &connector->modes, maxX, maxY);
151
152         if (connector->interlace_allowed)
153                 mode_flags |= DRM_MODE_FLAG_INTERLACE;
154         if (connector->doublescan_allowed)
155                 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
156         if (connector->stereo_allowed)
157                 mode_flags |= DRM_MODE_FLAG_3D_MASK;
158         drm_mode_validate_flag(connector, mode_flags);
159
160         list_for_each_entry(mode, &connector->modes, head) {
161                 if (mode->status == MODE_OK && connector_funcs->mode_valid)
162                         mode->status = connector_funcs->mode_valid(connector,
163                                                                    mode);
164         }
165
166 prune:
167         drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
168
169         if (list_empty(&connector->modes))
170                 return 0;
171
172         list_for_each_entry(mode, &connector->modes, head)
173                 mode->vrefresh = drm_mode_vrefresh(mode);
174
175         drm_mode_sort(&connector->modes);
176
177         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
178                         connector->name);
179         list_for_each_entry(mode, &connector->modes, head) {
180                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
181                 drm_mode_debug_printmodeline(mode);
182         }
183
184         return count;
185 }
186
187 /**
188  * drm_helper_probe_single_connector_modes - get complete set of display modes
189  * @connector: connector to probe
190  * @maxX: max width for modes
191  * @maxY: max height for modes
192  *
193  * Based on the helper callbacks implemented by @connector try to detect all
194  * valid modes.  Modes will first be added to the connector's probed_modes list,
195  * then culled (based on validity and the @maxX, @maxY parameters) and put into
196  * the normal modes list.
197  *
198  * Intended to be use as a generic implementation of the ->fill_modes()
199  * @connector vfunc for drivers that use the crtc helpers for output mode
200  * filtering and detection.
201  *
202  * Returns:
203  * The number of modes found on @connector.
204  */
205 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
206                                             uint32_t maxX, uint32_t maxY)
207 {
208         return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true);
209 }
210 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
211
212 /**
213  * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes
214  * @connector: connector to probe
215  * @maxX: max width for modes
216  * @maxY: max height for modes
217  *
218  * This operates like drm_hehlper_probe_single_connector_modes except it
219  * replaces the mode bits instead of merging them for preferred modes.
220  */
221 int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector,
222                                             uint32_t maxX, uint32_t maxY)
223 {
224         return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false);
225 }
226 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge);
227
228 /**
229  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
230  * @dev: drm_device whose connector state changed
231  *
232  * This function fires off the uevent for userspace and also calls the
233  * output_poll_changed function, which is most commonly used to inform the fbdev
234  * emulation code and allow it to update the fbcon output configuration.
235  *
236  * Drivers should call this from their hotplug handling code when a change is
237  * detected. Note that this function does not do any output detection of its
238  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
239  * driver already.
240  *
241  * This function must be called from process context with no mode
242  * setting locks held.
243  */
244 void drm_kms_helper_hotplug_event(struct drm_device *dev)
245 {
246         /* send a uevent + call fbdev */
247         drm_sysfs_hotplug_event(dev);
248         if (dev->mode_config.funcs->output_poll_changed)
249                 dev->mode_config.funcs->output_poll_changed(dev);
250 }
251 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
252
253 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
254 static void output_poll_execute(struct work_struct *work)
255 {
256         struct delayed_work *delayed_work = to_delayed_work(work);
257         struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
258         struct drm_connector *connector;
259         enum drm_connector_status old_status;
260         bool repoll = false, changed = false;
261
262         if (!drm_kms_helper_poll)
263                 return;
264
265         mutex_lock(&dev->mode_config.mutex);
266         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
267
268                 /* Ignore forced connectors. */
269                 if (connector->force)
270                         continue;
271
272                 /* Ignore HPD capable connectors and connectors where we don't
273                  * want any hotplug detection at all for polling. */
274                 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
275                         continue;
276
277                 repoll = true;
278
279                 old_status = connector->status;
280                 /* if we are connected and don't want to poll for disconnect
281                    skip it */
282                 if (old_status == connector_status_connected &&
283                     !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
284                         continue;
285
286                 connector->status = connector->funcs->detect(connector, false);
287                 if (old_status != connector->status) {
288                         const char *old, *new;
289
290                         old = drm_get_connector_status_name(old_status);
291                         new = drm_get_connector_status_name(connector->status);
292
293                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
294                                       "status updated from %s to %s\n",
295                                       connector->base.id,
296                                       connector->name,
297                                       old, new);
298
299                         changed = true;
300                 }
301         }
302
303         mutex_unlock(&dev->mode_config.mutex);
304
305         if (changed)
306                 drm_kms_helper_hotplug_event(dev);
307
308         if (repoll)
309                 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
310 }
311
312 /**
313  * drm_kms_helper_poll_disable - disable output polling
314  * @dev: drm_device
315  *
316  * This function disables the output polling work.
317  *
318  * Drivers can call this helper from their device suspend implementation. It is
319  * not an error to call this even when output polling isn't enabled or arlready
320  * disabled.
321  */
322 void drm_kms_helper_poll_disable(struct drm_device *dev)
323 {
324         if (!dev->mode_config.poll_enabled)
325                 return;
326         cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
327 }
328 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
329
330 /**
331  * drm_kms_helper_poll_enable - re-enable output polling.
332  * @dev: drm_device
333  *
334  * This function re-enables the output polling work.
335  *
336  * Drivers can call this helper from their device resume implementation. It is
337  * an error to call this when the output polling support has not yet been set
338  * up.
339  */
340 void drm_kms_helper_poll_enable(struct drm_device *dev)
341 {
342         bool poll = false;
343         struct drm_connector *connector;
344
345         if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
346                 return;
347
348         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
349                 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
350                                          DRM_CONNECTOR_POLL_DISCONNECT))
351                         poll = true;
352         }
353
354         if (poll)
355                 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
356 }
357 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
358
359 /**
360  * drm_kms_helper_poll_init - initialize and enable output polling
361  * @dev: drm_device
362  *
363  * This function intializes and then also enables output polling support for
364  * @dev. Drivers which do not have reliable hotplug support in hardware can use
365  * this helper infrastructure to regularly poll such connectors for changes in
366  * their connection state.
367  *
368  * Drivers can control which connectors are polled by setting the
369  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
370  * connectors where probing live outputs can result in visual distortion drivers
371  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
372  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
373  * completely ignored by the polling logic.
374  *
375  * Note that a connector can be both polled and probed from the hotplug handler,
376  * in case the hotplug interrupt is known to be unreliable.
377  */
378 void drm_kms_helper_poll_init(struct drm_device *dev)
379 {
380         INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
381         dev->mode_config.poll_enabled = true;
382
383         drm_kms_helper_poll_enable(dev);
384 }
385 EXPORT_SYMBOL(drm_kms_helper_poll_init);
386
387 /**
388  * drm_kms_helper_poll_fini - disable output polling and clean it up
389  * @dev: drm_device
390  */
391 void drm_kms_helper_poll_fini(struct drm_device *dev)
392 {
393         drm_kms_helper_poll_disable(dev);
394 }
395 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
396
397 /**
398  * drm_helper_hpd_irq_event - hotplug processing
399  * @dev: drm_device
400  *
401  * Drivers can use this helper function to run a detect cycle on all connectors
402  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
403  * other connectors are ignored, which is useful to avoid reprobing fixed
404  * panels.
405  *
406  * This helper function is useful for drivers which can't or don't track hotplug
407  * interrupts for each connector.
408  *
409  * Drivers which support hotplug interrupts for each connector individually and
410  * which have a more fine-grained detect logic should bypass this code and
411  * directly call drm_kms_helper_hotplug_event() in case the connector state
412  * changed.
413  *
414  * This function must be called from process context with no mode
415  * setting locks held.
416  *
417  * Note that a connector can be both polled and probed from the hotplug handler,
418  * in case the hotplug interrupt is known to be unreliable.
419  */
420 bool drm_helper_hpd_irq_event(struct drm_device *dev)
421 {
422         struct drm_connector *connector;
423         enum drm_connector_status old_status;
424         bool changed = false;
425
426         if (!dev->mode_config.poll_enabled)
427                 return false;
428
429         mutex_lock(&dev->mode_config.mutex);
430         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
431
432                 /* Only handle HPD capable connectors. */
433                 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
434                         continue;
435
436                 old_status = connector->status;
437
438                 connector->status = connector->funcs->detect(connector, false);
439                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
440                               connector->base.id,
441                               connector->name,
442                               drm_get_connector_status_name(old_status),
443                               drm_get_connector_status_name(connector->status));
444                 if (old_status != connector->status)
445                         changed = true;
446         }
447
448         mutex_unlock(&dev->mode_config.mutex);
449
450         if (changed)
451                 drm_kms_helper_hotplug_event(dev);
452
453         return changed;
454 }
455 EXPORT_SYMBOL(drm_helper_hpd_irq_event);