Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2006-2008 Intel Corporation
0003  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
0004  *
0005  * DRM core CRTC related functions
0006  *
0007  * Permission to use, copy, modify, distribute, and sell this software and its
0008  * documentation for any purpose is hereby granted without fee, provided that
0009  * the above copyright notice appear in all copies and that both that copyright
0010  * notice and this permission notice appear in supporting documentation, and
0011  * that the name of the copyright holders not be used in advertising or
0012  * publicity pertaining to distribution of the software without specific,
0013  * written prior permission.  The copyright holders make no representations
0014  * about the suitability of this software for any purpose.  It is provided "as
0015  * is" without express or implied warranty.
0016  *
0017  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
0018  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
0019  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
0020  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
0021  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
0022  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
0023  * OF THIS SOFTWARE.
0024  *
0025  * Authors:
0026  *      Keith Packard
0027  *  Eric Anholt <eric@anholt.net>
0028  *      Dave Airlie <airlied@linux.ie>
0029  *      Jesse Barnes <jesse.barnes@intel.com>
0030  */
0031 
0032 #include <linux/export.h>
0033 #include <linux/moduleparam.h>
0034 
0035 #include <drm/drm_bridge.h>
0036 #include <drm/drm_client.h>
0037 #include <drm/drm_crtc.h>
0038 #include <drm/drm_edid.h>
0039 #include <drm/drm_fb_helper.h>
0040 #include <drm/drm_fourcc.h>
0041 #include <drm/drm_modeset_helper_vtables.h>
0042 #include <drm/drm_print.h>
0043 #include <drm/drm_probe_helper.h>
0044 #include <drm/drm_sysfs.h>
0045 
0046 #include "drm_crtc_helper_internal.h"
0047 
0048 /**
0049  * DOC: output probing helper overview
0050  *
0051  * This library provides some helper code for output probing. It provides an
0052  * implementation of the core &drm_connector_funcs.fill_modes interface with
0053  * drm_helper_probe_single_connector_modes().
0054  *
0055  * It also provides support for polling connectors with a work item and for
0056  * generic hotplug interrupt handling where the driver doesn't or cannot keep
0057  * track of a per-connector hpd interrupt.
0058  *
0059  * This helper library can be used independently of the modeset helper library.
0060  * Drivers can also overwrite different parts e.g. use their own hotplug
0061  * handling code to avoid probing unrelated outputs.
0062  *
0063  * The probe helpers share the function table structures with other display
0064  * helper libraries. See &struct drm_connector_helper_funcs for the details.
0065  */
0066 
0067 static bool drm_kms_helper_poll = true;
0068 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
0069 
0070 static enum drm_mode_status
0071 drm_mode_validate_flag(const struct drm_display_mode *mode,
0072                int flags)
0073 {
0074     if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
0075         !(flags & DRM_MODE_FLAG_INTERLACE))
0076         return MODE_NO_INTERLACE;
0077 
0078     if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
0079         !(flags & DRM_MODE_FLAG_DBLSCAN))
0080         return MODE_NO_DBLESCAN;
0081 
0082     if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
0083         !(flags & DRM_MODE_FLAG_3D_MASK))
0084         return MODE_NO_STEREO;
0085 
0086     return MODE_OK;
0087 }
0088 
0089 static int
0090 drm_mode_validate_pipeline(struct drm_display_mode *mode,
0091                struct drm_connector *connector,
0092                struct drm_modeset_acquire_ctx *ctx,
0093                enum drm_mode_status *status)
0094 {
0095     struct drm_device *dev = connector->dev;
0096     struct drm_encoder *encoder;
0097     int ret;
0098 
0099     /* Step 1: Validate against connector */
0100     ret = drm_connector_mode_valid(connector, mode, ctx, status);
0101     if (ret || *status != MODE_OK)
0102         return ret;
0103 
0104     /* Step 2: Validate against encoders and crtcs */
0105     drm_connector_for_each_possible_encoder(connector, encoder) {
0106         struct drm_bridge *bridge;
0107         struct drm_crtc *crtc;
0108 
0109         *status = drm_encoder_mode_valid(encoder, mode);
0110         if (*status != MODE_OK) {
0111             /* No point in continuing for crtc check as this encoder
0112              * will not accept the mode anyway. If all encoders
0113              * reject the mode then, at exit, ret will not be
0114              * MODE_OK. */
0115             continue;
0116         }
0117 
0118         bridge = drm_bridge_chain_get_first_bridge(encoder);
0119         *status = drm_bridge_chain_mode_valid(bridge,
0120                               &connector->display_info,
0121                               mode);
0122         if (*status != MODE_OK) {
0123             /* There is also no point in continuing for crtc check
0124              * here. */
0125             continue;
0126         }
0127 
0128         drm_for_each_crtc(crtc, dev) {
0129             if (!drm_encoder_crtc_ok(encoder, crtc))
0130                 continue;
0131 
0132             *status = drm_crtc_mode_valid(crtc, mode);
0133             if (*status == MODE_OK) {
0134                 /* If we get to this point there is at least
0135                  * one combination of encoder+crtc that works
0136                  * for this mode. Lets return now. */
0137                 return 0;
0138             }
0139         }
0140     }
0141 
0142     return 0;
0143 }
0144 
0145 static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
0146 {
0147     struct drm_cmdline_mode *cmdline_mode;
0148     struct drm_display_mode *mode;
0149 
0150     cmdline_mode = &connector->cmdline_mode;
0151     if (!cmdline_mode->specified)
0152         return 0;
0153 
0154     /* Only add a GTF mode if we find no matching probed modes */
0155     list_for_each_entry(mode, &connector->probed_modes, head) {
0156         if (mode->hdisplay != cmdline_mode->xres ||
0157             mode->vdisplay != cmdline_mode->yres)
0158             continue;
0159 
0160         if (cmdline_mode->refresh_specified) {
0161             /* The probed mode's vrefresh is set until later */
0162             if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
0163                 continue;
0164         }
0165 
0166         /* Mark the matching mode as being preferred by the user */
0167         mode->type |= DRM_MODE_TYPE_USERDEF;
0168         return 0;
0169     }
0170 
0171     mode = drm_mode_create_from_cmdline_mode(connector->dev,
0172                          cmdline_mode);
0173     if (mode == NULL)
0174         return 0;
0175 
0176     drm_mode_probed_add(connector, mode);
0177     return 1;
0178 }
0179 
0180 enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
0181                      const struct drm_display_mode *mode)
0182 {
0183     const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
0184 
0185     if (!crtc_funcs || !crtc_funcs->mode_valid)
0186         return MODE_OK;
0187 
0188     return crtc_funcs->mode_valid(crtc, mode);
0189 }
0190 
0191 enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
0192                         const struct drm_display_mode *mode)
0193 {
0194     const struct drm_encoder_helper_funcs *encoder_funcs =
0195         encoder->helper_private;
0196 
0197     if (!encoder_funcs || !encoder_funcs->mode_valid)
0198         return MODE_OK;
0199 
0200     return encoder_funcs->mode_valid(encoder, mode);
0201 }
0202 
0203 int
0204 drm_connector_mode_valid(struct drm_connector *connector,
0205              struct drm_display_mode *mode,
0206              struct drm_modeset_acquire_ctx *ctx,
0207              enum drm_mode_status *status)
0208 {
0209     const struct drm_connector_helper_funcs *connector_funcs =
0210         connector->helper_private;
0211     int ret = 0;
0212 
0213     if (!connector_funcs)
0214         *status = MODE_OK;
0215     else if (connector_funcs->mode_valid_ctx)
0216         ret = connector_funcs->mode_valid_ctx(connector, mode, ctx,
0217                               status);
0218     else if (connector_funcs->mode_valid)
0219         *status = connector_funcs->mode_valid(connector, mode);
0220     else
0221         *status = MODE_OK;
0222 
0223     return ret;
0224 }
0225 
0226 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
0227 /**
0228  * drm_kms_helper_poll_enable - re-enable output polling.
0229  * @dev: drm_device
0230  *
0231  * This function re-enables the output polling work, after it has been
0232  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
0233  * suspend/resume.
0234  *
0235  * Drivers can call this helper from their device resume implementation. It is
0236  * not an error to call this even when output polling isn't enabled.
0237  *
0238  * Note that calls to enable and disable polling must be strictly ordered, which
0239  * is automatically the case when they're only call from suspend/resume
0240  * callbacks.
0241  */
0242 void drm_kms_helper_poll_enable(struct drm_device *dev)
0243 {
0244     bool poll = false;
0245     struct drm_connector *connector;
0246     struct drm_connector_list_iter conn_iter;
0247     unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
0248 
0249     if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
0250         return;
0251 
0252     drm_connector_list_iter_begin(dev, &conn_iter);
0253     drm_for_each_connector_iter(connector, &conn_iter) {
0254         if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
0255                      DRM_CONNECTOR_POLL_DISCONNECT))
0256             poll = true;
0257     }
0258     drm_connector_list_iter_end(&conn_iter);
0259 
0260     if (dev->mode_config.delayed_event) {
0261         /*
0262          * FIXME:
0263          *
0264          * Use short (1s) delay to handle the initial delayed event.
0265          * This delay should not be needed, but Optimus/nouveau will
0266          * fail in a mysterious way if the delayed event is handled as
0267          * soon as possible like it is done in
0268          * drm_helper_probe_single_connector_modes() in case the poll
0269          * was enabled before.
0270          */
0271         poll = true;
0272         delay = HZ;
0273     }
0274 
0275     if (poll)
0276         schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
0277 }
0278 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
0279 
0280 static enum drm_connector_status
0281 drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
0282 {
0283     const struct drm_connector_helper_funcs *funcs = connector->helper_private;
0284     struct drm_modeset_acquire_ctx ctx;
0285     int ret;
0286 
0287     drm_modeset_acquire_init(&ctx, 0);
0288 
0289 retry:
0290     ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
0291     if (!ret) {
0292         if (funcs->detect_ctx)
0293             ret = funcs->detect_ctx(connector, &ctx, force);
0294         else if (connector->funcs->detect)
0295             ret = connector->funcs->detect(connector, force);
0296         else
0297             ret = connector_status_connected;
0298     }
0299 
0300     if (ret == -EDEADLK) {
0301         drm_modeset_backoff(&ctx);
0302         goto retry;
0303     }
0304 
0305     if (WARN_ON(ret < 0))
0306         ret = connector_status_unknown;
0307 
0308     if (ret != connector->status)
0309         connector->epoch_counter += 1;
0310 
0311     drm_modeset_drop_locks(&ctx);
0312     drm_modeset_acquire_fini(&ctx);
0313 
0314     return ret;
0315 }
0316 
0317 /**
0318  * drm_helper_probe_detect - probe connector status
0319  * @connector: connector to probe
0320  * @ctx: acquire_ctx, or NULL to let this function handle locking.
0321  * @force: Whether destructive probe operations should be performed.
0322  *
0323  * This function calls the detect callbacks of the connector.
0324  * This function returns &drm_connector_status, or
0325  * if @ctx is set, it might also return -EDEADLK.
0326  */
0327 int
0328 drm_helper_probe_detect(struct drm_connector *connector,
0329             struct drm_modeset_acquire_ctx *ctx,
0330             bool force)
0331 {
0332     const struct drm_connector_helper_funcs *funcs = connector->helper_private;
0333     struct drm_device *dev = connector->dev;
0334     int ret;
0335 
0336     if (!ctx)
0337         return drm_helper_probe_detect_ctx(connector, force);
0338 
0339     ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
0340     if (ret)
0341         return ret;
0342 
0343     if (funcs->detect_ctx)
0344         ret = funcs->detect_ctx(connector, ctx, force);
0345     else if (connector->funcs->detect)
0346         ret = connector->funcs->detect(connector, force);
0347     else
0348         ret = connector_status_connected;
0349 
0350     if (ret != connector->status)
0351         connector->epoch_counter += 1;
0352 
0353     return ret;
0354 }
0355 EXPORT_SYMBOL(drm_helper_probe_detect);
0356 
0357 static int drm_helper_probe_get_modes(struct drm_connector *connector)
0358 {
0359     const struct drm_connector_helper_funcs *connector_funcs =
0360         connector->helper_private;
0361     int count;
0362 
0363     count = connector_funcs->get_modes(connector);
0364 
0365     /*
0366      * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
0367      * override/firmware EDID.
0368      */
0369     if (count == 0 && connector->status == connector_status_connected)
0370         count = drm_add_override_edid_modes(connector);
0371 
0372     return count;
0373 }
0374 
0375 static int __drm_helper_update_and_validate(struct drm_connector *connector,
0376                         uint32_t maxX, uint32_t maxY,
0377                         struct drm_modeset_acquire_ctx *ctx)
0378 {
0379     struct drm_device *dev = connector->dev;
0380     struct drm_display_mode *mode;
0381     int mode_flags = 0;
0382     int ret;
0383 
0384     drm_connector_list_update(connector);
0385 
0386     if (connector->interlace_allowed)
0387         mode_flags |= DRM_MODE_FLAG_INTERLACE;
0388     if (connector->doublescan_allowed)
0389         mode_flags |= DRM_MODE_FLAG_DBLSCAN;
0390     if (connector->stereo_allowed)
0391         mode_flags |= DRM_MODE_FLAG_3D_MASK;
0392 
0393     list_for_each_entry(mode, &connector->modes, head) {
0394         if (mode->status != MODE_OK)
0395             continue;
0396 
0397         mode->status = drm_mode_validate_driver(dev, mode);
0398         if (mode->status != MODE_OK)
0399             continue;
0400 
0401         mode->status = drm_mode_validate_size(mode, maxX, maxY);
0402         if (mode->status != MODE_OK)
0403             continue;
0404 
0405         mode->status = drm_mode_validate_flag(mode, mode_flags);
0406         if (mode->status != MODE_OK)
0407             continue;
0408 
0409         ret = drm_mode_validate_pipeline(mode, connector, ctx,
0410                          &mode->status);
0411         if (ret) {
0412             drm_dbg_kms(dev,
0413                     "drm_mode_validate_pipeline failed: %d\n",
0414                     ret);
0415 
0416             if (drm_WARN_ON_ONCE(dev, ret != -EDEADLK))
0417                 mode->status = MODE_ERROR;
0418             else
0419                 return -EDEADLK;
0420         }
0421 
0422         if (mode->status != MODE_OK)
0423             continue;
0424         mode->status = drm_mode_validate_ycbcr420(mode, connector);
0425     }
0426 
0427     return 0;
0428 }
0429 
0430 /**
0431  * drm_helper_probe_single_connector_modes - get complete set of display modes
0432  * @connector: connector to probe
0433  * @maxX: max width for modes
0434  * @maxY: max height for modes
0435  *
0436  * Based on the helper callbacks implemented by @connector in struct
0437  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
0438  * be added to the connector's probed_modes list, then culled (based on validity
0439  * and the @maxX, @maxY parameters) and put into the normal modes list.
0440  *
0441  * Intended to be used as a generic implementation of the
0442  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
0443  * for output mode filtering and detection.
0444  *
0445  * The basic procedure is as follows
0446  *
0447  * 1. All modes currently on the connector's modes list are marked as stale
0448  *
0449  * 2. New modes are added to the connector's probed_modes list with
0450  *    drm_mode_probed_add(). New modes start their life with status as OK.
0451  *    Modes are added from a single source using the following priority order.
0452  *
0453  *    - &drm_connector_helper_funcs.get_modes vfunc
0454  *    - if the connector status is connector_status_connected, standard
0455  *      VESA DMT modes up to 1024x768 are automatically added
0456  *      (drm_add_modes_noedid())
0457  *
0458  *    Finally modes specified via the kernel command line (video=...) are
0459  *    added in addition to what the earlier probes produced
0460  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
0461  *    using the VESA GTF/CVT formulas.
0462  *
0463  * 3. Modes are moved from the probed_modes list to the modes list. Potential
0464  *    duplicates are merged together (see drm_connector_list_update()).
0465  *    After this step the probed_modes list will be empty again.
0466  *
0467  * 4. Any non-stale mode on the modes list then undergoes validation
0468  *
0469  *    - drm_mode_validate_basic() performs basic sanity checks
0470  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
0471  *      (if specified)
0472  *    - drm_mode_validate_flag() checks the modes against basic connector
0473  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
0474  *    - the optional &drm_connector_helper_funcs.mode_valid or
0475  *      &drm_connector_helper_funcs.mode_valid_ctx helpers can perform driver
0476  *      and/or sink specific checks
0477  *    - the optional &drm_crtc_helper_funcs.mode_valid,
0478  *      &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
0479  *      helpers can perform driver and/or source specific checks which are also
0480  *      enforced by the modeset/atomic helpers
0481  *
0482  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
0483  *    accompanied by a debug message indicating the reason for the mode's
0484  *    rejection (see drm_mode_prune_invalid()).
0485  *
0486  * Returns:
0487  * The number of modes found on @connector.
0488  */
0489 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
0490                         uint32_t maxX, uint32_t maxY)
0491 {
0492     struct drm_device *dev = connector->dev;
0493     struct drm_display_mode *mode;
0494     int count = 0, ret;
0495     enum drm_connector_status old_status;
0496     struct drm_modeset_acquire_ctx ctx;
0497 
0498     WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
0499 
0500     drm_modeset_acquire_init(&ctx, 0);
0501 
0502     DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
0503             connector->name);
0504 
0505 retry:
0506     ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
0507     if (ret == -EDEADLK) {
0508         drm_modeset_backoff(&ctx);
0509         goto retry;
0510     } else
0511         WARN_ON(ret < 0);
0512 
0513     /* set all old modes to the stale state */
0514     list_for_each_entry(mode, &connector->modes, head)
0515         mode->status = MODE_STALE;
0516 
0517     old_status = connector->status;
0518 
0519     if (connector->force) {
0520         if (connector->force == DRM_FORCE_ON ||
0521             connector->force == DRM_FORCE_ON_DIGITAL)
0522             connector->status = connector_status_connected;
0523         else
0524             connector->status = connector_status_disconnected;
0525         if (connector->funcs->force)
0526             connector->funcs->force(connector);
0527     } else {
0528         ret = drm_helper_probe_detect(connector, &ctx, true);
0529 
0530         if (ret == -EDEADLK) {
0531             drm_modeset_backoff(&ctx);
0532             goto retry;
0533         } else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
0534             ret = connector_status_unknown;
0535 
0536         connector->status = ret;
0537     }
0538 
0539     /*
0540      * Normally either the driver's hpd code or the poll loop should
0541      * pick up any changes and fire the hotplug event. But if
0542      * userspace sneaks in a probe, we might miss a change. Hence
0543      * check here, and if anything changed start the hotplug code.
0544      */
0545     if (old_status != connector->status) {
0546         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
0547                   connector->base.id,
0548                   connector->name,
0549                   drm_get_connector_status_name(old_status),
0550                   drm_get_connector_status_name(connector->status));
0551 
0552         /*
0553          * The hotplug event code might call into the fb
0554          * helpers, and so expects that we do not hold any
0555          * locks. Fire up the poll struct instead, it will
0556          * disable itself again.
0557          */
0558         dev->mode_config.delayed_event = true;
0559         if (dev->mode_config.poll_enabled)
0560             schedule_delayed_work(&dev->mode_config.output_poll_work,
0561                           0);
0562     }
0563 
0564     /* Re-enable polling in case the global poll config changed. */
0565     if (drm_kms_helper_poll != dev->mode_config.poll_running)
0566         drm_kms_helper_poll_enable(dev);
0567 
0568     dev->mode_config.poll_running = drm_kms_helper_poll;
0569 
0570     if (connector->status == connector_status_disconnected) {
0571         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
0572             connector->base.id, connector->name);
0573         drm_connector_update_edid_property(connector, NULL);
0574         drm_mode_prune_invalid(dev, &connector->modes, false);
0575         goto exit;
0576     }
0577 
0578     count = drm_helper_probe_get_modes(connector);
0579 
0580     if (count == 0 && (connector->status == connector_status_connected ||
0581                connector->status == connector_status_unknown)) {
0582         count = drm_add_modes_noedid(connector, 1024, 768);
0583 
0584         /*
0585          * Section 4.2.2.6 (EDID Corruption Detection) of the DP 1.4a
0586          * Link CTS specifies that 640x480 (the official "failsafe"
0587          * mode) needs to be the default if there's no EDID.
0588          */
0589         if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
0590             drm_set_preferred_mode(connector, 640, 480);
0591     }
0592     count += drm_helper_probe_add_cmdline_mode(connector);
0593     if (count != 0) {
0594         ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
0595         if (ret == -EDEADLK) {
0596             drm_modeset_backoff(&ctx);
0597             goto retry;
0598         }
0599     }
0600 
0601     drm_mode_prune_invalid(dev, &connector->modes, true);
0602 
0603     /*
0604      * Displayport spec section 5.2.1.2 ("Video Timing Format") says that
0605      * all detachable sinks shall support 640x480 @60Hz as a fail safe
0606      * mode. If all modes were pruned, perhaps because they need more
0607      * lanes or a higher pixel clock than available, at least try to add
0608      * in 640x480.
0609      */
0610     if (list_empty(&connector->modes) &&
0611         connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
0612         count = drm_add_modes_noedid(connector, 640, 480);
0613         ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
0614         if (ret == -EDEADLK) {
0615             drm_modeset_backoff(&ctx);
0616             goto retry;
0617         }
0618         drm_mode_prune_invalid(dev, &connector->modes, true);
0619     }
0620 
0621 exit:
0622     drm_modeset_drop_locks(&ctx);
0623     drm_modeset_acquire_fini(&ctx);
0624 
0625     if (list_empty(&connector->modes))
0626         return 0;
0627 
0628     drm_mode_sort(&connector->modes);
0629 
0630     DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
0631             connector->name);
0632     list_for_each_entry(mode, &connector->modes, head) {
0633         drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
0634         drm_mode_debug_printmodeline(mode);
0635     }
0636 
0637     return count;
0638 }
0639 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
0640 
0641 /**
0642  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
0643  * @dev: drm_device whose connector state changed
0644  *
0645  * This function fires off the uevent for userspace and also calls the
0646  * output_poll_changed function, which is most commonly used to inform the fbdev
0647  * emulation code and allow it to update the fbcon output configuration.
0648  *
0649  * Drivers should call this from their hotplug handling code when a change is
0650  * detected. Note that this function does not do any output detection of its
0651  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
0652  * driver already.
0653  *
0654  * This function must be called from process context with no mode
0655  * setting locks held.
0656  *
0657  * If only a single connector has changed, consider calling
0658  * drm_kms_helper_connector_hotplug_event() instead.
0659  */
0660 void drm_kms_helper_hotplug_event(struct drm_device *dev)
0661 {
0662     /* send a uevent + call fbdev */
0663     drm_sysfs_hotplug_event(dev);
0664     if (dev->mode_config.funcs->output_poll_changed)
0665         dev->mode_config.funcs->output_poll_changed(dev);
0666 
0667     drm_client_dev_hotplug(dev);
0668 }
0669 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
0670 
0671 /**
0672  * drm_kms_helper_connector_hotplug_event - fire off a KMS connector hotplug event
0673  * @connector: drm_connector which has changed
0674  *
0675  * This is the same as drm_kms_helper_hotplug_event(), except it fires a more
0676  * fine-grained uevent for a single connector.
0677  */
0678 void drm_kms_helper_connector_hotplug_event(struct drm_connector *connector)
0679 {
0680     struct drm_device *dev = connector->dev;
0681 
0682     /* send a uevent + call fbdev */
0683     drm_sysfs_connector_hotplug_event(connector);
0684     if (dev->mode_config.funcs->output_poll_changed)
0685         dev->mode_config.funcs->output_poll_changed(dev);
0686 
0687     drm_client_dev_hotplug(dev);
0688 }
0689 EXPORT_SYMBOL(drm_kms_helper_connector_hotplug_event);
0690 
0691 static void output_poll_execute(struct work_struct *work)
0692 {
0693     struct delayed_work *delayed_work = to_delayed_work(work);
0694     struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
0695     struct drm_connector *connector;
0696     struct drm_connector_list_iter conn_iter;
0697     enum drm_connector_status old_status;
0698     bool repoll = false, changed;
0699     u64 old_epoch_counter;
0700 
0701     if (!dev->mode_config.poll_enabled)
0702         return;
0703 
0704     /* Pick up any changes detected by the probe functions. */
0705     changed = dev->mode_config.delayed_event;
0706     dev->mode_config.delayed_event = false;
0707 
0708     if (!drm_kms_helper_poll)
0709         goto out;
0710 
0711     if (!mutex_trylock(&dev->mode_config.mutex)) {
0712         repoll = true;
0713         goto out;
0714     }
0715 
0716     drm_connector_list_iter_begin(dev, &conn_iter);
0717     drm_for_each_connector_iter(connector, &conn_iter) {
0718         /* Ignore forced connectors. */
0719         if (connector->force)
0720             continue;
0721 
0722         /* Ignore HPD capable connectors and connectors where we don't
0723          * want any hotplug detection at all for polling. */
0724         if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
0725             continue;
0726 
0727         old_status = connector->status;
0728         /* if we are connected and don't want to poll for disconnect
0729            skip it */
0730         if (old_status == connector_status_connected &&
0731             !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
0732             continue;
0733 
0734         repoll = true;
0735 
0736         old_epoch_counter = connector->epoch_counter;
0737         connector->status = drm_helper_probe_detect(connector, NULL, false);
0738         if (old_epoch_counter != connector->epoch_counter) {
0739             const char *old, *new;
0740 
0741             /*
0742              * The poll work sets force=false when calling detect so
0743              * that drivers can avoid to do disruptive tests (e.g.
0744              * when load detect cycles could cause flickering on
0745              * other, running displays). This bears the risk that we
0746              * flip-flop between unknown here in the poll work and
0747              * the real state when userspace forces a full detect
0748              * call after receiving a hotplug event due to this
0749              * change.
0750              *
0751              * Hence clamp an unknown detect status to the old
0752              * value.
0753              */
0754             if (connector->status == connector_status_unknown) {
0755                 connector->status = old_status;
0756                 continue;
0757             }
0758 
0759             old = drm_get_connector_status_name(old_status);
0760             new = drm_get_connector_status_name(connector->status);
0761 
0762             DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
0763                       "status updated from %s to %s\n",
0764                       connector->base.id,
0765                       connector->name,
0766                       old, new);
0767             DRM_DEBUG_KMS("[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
0768                       connector->base.id, connector->name,
0769                       old_epoch_counter, connector->epoch_counter);
0770 
0771             changed = true;
0772         }
0773     }
0774     drm_connector_list_iter_end(&conn_iter);
0775 
0776     mutex_unlock(&dev->mode_config.mutex);
0777 
0778 out:
0779     if (changed)
0780         drm_kms_helper_hotplug_event(dev);
0781 
0782     if (repoll)
0783         schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
0784 }
0785 
0786 /**
0787  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
0788  *
0789  * Determine if %current task is an output poll worker.  This can be used
0790  * to select distinct code paths for output polling versus other contexts.
0791  *
0792  * One use case is to avoid a deadlock between the output poll worker and
0793  * the autosuspend worker wherein the latter waits for polling to finish
0794  * upon calling drm_kms_helper_poll_disable(), while the former waits for
0795  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
0796  * connector ->detect hook.
0797  */
0798 bool drm_kms_helper_is_poll_worker(void)
0799 {
0800     struct work_struct *work = current_work();
0801 
0802     return work && work->func == output_poll_execute;
0803 }
0804 EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
0805 
0806 /**
0807  * drm_kms_helper_poll_disable - disable output polling
0808  * @dev: drm_device
0809  *
0810  * This function disables the output polling work.
0811  *
0812  * Drivers can call this helper from their device suspend implementation. It is
0813  * not an error to call this even when output polling isn't enabled or already
0814  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
0815  *
0816  * Note that calls to enable and disable polling must be strictly ordered, which
0817  * is automatically the case when they're only call from suspend/resume
0818  * callbacks.
0819  */
0820 void drm_kms_helper_poll_disable(struct drm_device *dev)
0821 {
0822     if (!dev->mode_config.poll_enabled)
0823         return;
0824     cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
0825 }
0826 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
0827 
0828 /**
0829  * drm_kms_helper_poll_init - initialize and enable output polling
0830  * @dev: drm_device
0831  *
0832  * This function initializes and then also enables output polling support for
0833  * @dev. Drivers which do not have reliable hotplug support in hardware can use
0834  * this helper infrastructure to regularly poll such connectors for changes in
0835  * their connection state.
0836  *
0837  * Drivers can control which connectors are polled by setting the
0838  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
0839  * connectors where probing live outputs can result in visual distortion drivers
0840  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
0841  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
0842  * completely ignored by the polling logic.
0843  *
0844  * Note that a connector can be both polled and probed from the hotplug handler,
0845  * in case the hotplug interrupt is known to be unreliable.
0846  */
0847 void drm_kms_helper_poll_init(struct drm_device *dev)
0848 {
0849     INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
0850     dev->mode_config.poll_enabled = true;
0851 
0852     drm_kms_helper_poll_enable(dev);
0853 }
0854 EXPORT_SYMBOL(drm_kms_helper_poll_init);
0855 
0856 /**
0857  * drm_kms_helper_poll_fini - disable output polling and clean it up
0858  * @dev: drm_device
0859  */
0860 void drm_kms_helper_poll_fini(struct drm_device *dev)
0861 {
0862     if (!dev->mode_config.poll_enabled)
0863         return;
0864 
0865     dev->mode_config.poll_enabled = false;
0866     cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
0867 }
0868 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
0869 
0870 static bool check_connector_changed(struct drm_connector *connector)
0871 {
0872     struct drm_device *dev = connector->dev;
0873     enum drm_connector_status old_status;
0874     u64 old_epoch_counter;
0875 
0876     /* Only handle HPD capable connectors. */
0877     drm_WARN_ON(dev, !(connector->polled & DRM_CONNECTOR_POLL_HPD));
0878 
0879     drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
0880 
0881     old_status = connector->status;
0882     old_epoch_counter = connector->epoch_counter;
0883     connector->status = drm_helper_probe_detect(connector, NULL, false);
0884 
0885     if (old_epoch_counter == connector->epoch_counter) {
0886         drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Same epoch counter %llu\n",
0887                 connector->base.id,
0888                 connector->name,
0889                 connector->epoch_counter);
0890 
0891         return false;
0892     }
0893 
0894     drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
0895             connector->base.id,
0896             connector->name,
0897             drm_get_connector_status_name(old_status),
0898             drm_get_connector_status_name(connector->status));
0899 
0900     drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Changed epoch counter %llu => %llu\n",
0901             connector->base.id,
0902             connector->name,
0903             old_epoch_counter,
0904             connector->epoch_counter);
0905 
0906     return true;
0907 }
0908 
0909 /**
0910  * drm_connector_helper_hpd_irq_event - hotplug processing
0911  * @connector: drm_connector
0912  *
0913  * Drivers can use this helper function to run a detect cycle on a connector
0914  * which has the DRM_CONNECTOR_POLL_HPD flag set in its &polled member.
0915  *
0916  * This helper function is useful for drivers which can track hotplug
0917  * interrupts for a single connector. Drivers that want to send a
0918  * hotplug event for all connectors or can't track hotplug interrupts
0919  * per connector need to use drm_helper_hpd_irq_event().
0920  *
0921  * This function must be called from process context with no mode
0922  * setting locks held.
0923  *
0924  * Note that a connector can be both polled and probed from the hotplug
0925  * handler, in case the hotplug interrupt is known to be unreliable.
0926  *
0927  * Returns:
0928  * A boolean indicating whether the connector status changed or not
0929  */
0930 bool drm_connector_helper_hpd_irq_event(struct drm_connector *connector)
0931 {
0932     struct drm_device *dev = connector->dev;
0933     bool changed;
0934 
0935     mutex_lock(&dev->mode_config.mutex);
0936     changed = check_connector_changed(connector);
0937     mutex_unlock(&dev->mode_config.mutex);
0938 
0939     if (changed) {
0940         drm_kms_helper_connector_hotplug_event(connector);
0941         drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Sent hotplug event\n",
0942                 connector->base.id,
0943                 connector->name);
0944     }
0945 
0946     return changed;
0947 }
0948 EXPORT_SYMBOL(drm_connector_helper_hpd_irq_event);
0949 
0950 /**
0951  * drm_helper_hpd_irq_event - hotplug processing
0952  * @dev: drm_device
0953  *
0954  * Drivers can use this helper function to run a detect cycle on all connectors
0955  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
0956  * other connectors are ignored, which is useful to avoid reprobing fixed
0957  * panels.
0958  *
0959  * This helper function is useful for drivers which can't or don't track hotplug
0960  * interrupts for each connector.
0961  *
0962  * Drivers which support hotplug interrupts for each connector individually and
0963  * which have a more fine-grained detect logic can use
0964  * drm_connector_helper_hpd_irq_event(). Alternatively, they should bypass this
0965  * code and directly call drm_kms_helper_hotplug_event() in case the connector
0966  * state changed.
0967  *
0968  * This function must be called from process context with no mode
0969  * setting locks held.
0970  *
0971  * Note that a connector can be both polled and probed from the hotplug handler,
0972  * in case the hotplug interrupt is known to be unreliable.
0973  *
0974  * Returns:
0975  * A boolean indicating whether the connector status changed or not
0976  */
0977 bool drm_helper_hpd_irq_event(struct drm_device *dev)
0978 {
0979     struct drm_connector *connector, *first_changed_connector = NULL;
0980     struct drm_connector_list_iter conn_iter;
0981     int changed = 0;
0982 
0983     if (!dev->mode_config.poll_enabled)
0984         return false;
0985 
0986     mutex_lock(&dev->mode_config.mutex);
0987     drm_connector_list_iter_begin(dev, &conn_iter);
0988     drm_for_each_connector_iter(connector, &conn_iter) {
0989         /* Only handle HPD capable connectors. */
0990         if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
0991             continue;
0992 
0993         if (check_connector_changed(connector)) {
0994             if (!first_changed_connector) {
0995                 drm_connector_get(connector);
0996                 first_changed_connector = connector;
0997             }
0998 
0999             changed++;
1000         }
1001     }
1002     drm_connector_list_iter_end(&conn_iter);
1003     mutex_unlock(&dev->mode_config.mutex);
1004 
1005     if (changed == 1)
1006         drm_kms_helper_connector_hotplug_event(first_changed_connector);
1007     else if (changed > 0)
1008         drm_kms_helper_hotplug_event(dev);
1009 
1010     if (first_changed_connector)
1011         drm_connector_put(first_changed_connector);
1012 
1013     return changed;
1014 }
1015 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1016 
1017 /**
1018  * drm_connector_helper_get_modes_from_ddc - Updates the connector's EDID
1019  *                                           property from the connector's
1020  *                                           DDC channel
1021  * @connector: The connector
1022  *
1023  * Returns:
1024  * The number of detected display modes.
1025  *
1026  * Uses a connector's DDC channel to retrieve EDID data and update the
1027  * connector's EDID property and display modes. Drivers can use this
1028  * function to implement struct &drm_connector_helper_funcs.get_modes
1029  * for connectors with a DDC channel.
1030  */
1031 int drm_connector_helper_get_modes_from_ddc(struct drm_connector *connector)
1032 {
1033     struct edid *edid;
1034     int count = 0;
1035 
1036     if (!connector->ddc)
1037         return 0;
1038 
1039     edid = drm_get_edid(connector, connector->ddc);
1040 
1041     // clears property if EDID is NULL
1042     drm_connector_update_edid_property(connector, edid);
1043 
1044     if (edid) {
1045         count = drm_add_edid_modes(connector, edid);
1046         kfree(edid);
1047     }
1048 
1049     return count;
1050 }
1051 EXPORT_SYMBOL(drm_connector_helper_get_modes_from_ddc);
1052 
1053 /**
1054  * drm_connector_helper_get_modes - Read EDID and update connector.
1055  * @connector: The connector
1056  *
1057  * Read the EDID using drm_edid_read() (which requires that connector->ddc is
1058  * set), and update the connector using the EDID.
1059  *
1060  * This can be used as the "default" connector helper .get_modes() hook if the
1061  * driver does not need any special processing. This is sets the example what
1062  * custom .get_modes() hooks should do regarding EDID read and connector update.
1063  *
1064  * Returns: Number of modes.
1065  */
1066 int drm_connector_helper_get_modes(struct drm_connector *connector)
1067 {
1068     const struct drm_edid *drm_edid;
1069     int count;
1070 
1071     drm_edid = drm_edid_read(connector);
1072 
1073     /*
1074      * Unconditionally update the connector. If the EDID was read
1075      * successfully, fill in the connector information derived from the
1076      * EDID. Otherwise, if the EDID is NULL, clear the connector
1077      * information.
1078      */
1079     count = drm_edid_connector_update(connector, drm_edid);
1080 
1081     drm_edid_free(drm_edid);
1082 
1083     return count;
1084 }
1085 EXPORT_SYMBOL(drm_connector_helper_get_modes);