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