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