drm_probe_helper.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. #include <linux/export.h>
  32. #include <linux/moduleparam.h>
  33. #include <drm/drm_bridge.h>
  34. #include <drm/drm_client.h>
  35. #include <drm/drm_crtc.h>
  36. #include <drm/drm_edid.h>
  37. #include <drm/drm_fb_helper.h>
  38. #include <drm/drm_fourcc.h>
  39. #include <drm/drm_modeset_helper_vtables.h>
  40. #include <drm/drm_print.h>
  41. #include <drm/drm_probe_helper.h>
  42. #include <drm/drm_sysfs.h>
  43. #include "drm_crtc_helper_internal.h"
  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 &drm_connector_funcs.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. * The probe helpers share the function table structures with other display
  60. * helper libraries. See &struct drm_connector_helper_funcs for the details.
  61. */
  62. static bool drm_kms_helper_poll = true;
  63. module_param_named(poll, drm_kms_helper_poll, bool, 0600);
  64. static enum drm_mode_status
  65. drm_mode_validate_flag(const struct drm_display_mode *mode,
  66. int flags)
  67. {
  68. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  69. !(flags & DRM_MODE_FLAG_INTERLACE))
  70. return MODE_NO_INTERLACE;
  71. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  72. !(flags & DRM_MODE_FLAG_DBLSCAN))
  73. return MODE_NO_DBLESCAN;
  74. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
  75. !(flags & DRM_MODE_FLAG_3D_MASK))
  76. return MODE_NO_STEREO;
  77. return MODE_OK;
  78. }
  79. static int
  80. drm_mode_validate_pipeline(struct drm_display_mode *mode,
  81. struct drm_connector *connector,
  82. struct drm_modeset_acquire_ctx *ctx,
  83. enum drm_mode_status *status)
  84. {
  85. struct drm_device *dev = connector->dev;
  86. struct drm_encoder *encoder;
  87. int ret;
  88. /* Step 1: Validate against connector */
  89. ret = drm_connector_mode_valid(connector, mode, ctx, status);
  90. if (ret || *status != MODE_OK)
  91. return ret;
  92. /* Step 2: Validate against encoders and crtcs */
  93. drm_connector_for_each_possible_encoder(connector, encoder) {
  94. struct drm_bridge *bridge;
  95. struct drm_crtc *crtc;
  96. *status = drm_encoder_mode_valid(encoder, mode);
  97. if (*status != MODE_OK) {
  98. /* No point in continuing for crtc check as this encoder
  99. * will not accept the mode anyway. If all encoders
  100. * reject the mode then, at exit, ret will not be
  101. * MODE_OK. */
  102. continue;
  103. }
  104. bridge = drm_bridge_chain_get_first_bridge(encoder);
  105. *status = drm_bridge_chain_mode_valid(bridge,
  106. &connector->display_info,
  107. mode);
  108. if (*status != MODE_OK) {
  109. /* There is also no point in continuing for crtc check
  110. * here. */
  111. continue;
  112. }
  113. drm_for_each_crtc(crtc, dev) {
  114. if (!drm_encoder_crtc_ok(encoder, crtc))
  115. continue;
  116. *status = drm_crtc_mode_valid(crtc, mode);
  117. if (*status == MODE_OK) {
  118. /* If we get to this point there is at least
  119. * one combination of encoder+crtc that works
  120. * for this mode. Lets return now. */
  121. return 0;
  122. }
  123. }
  124. }
  125. return 0;
  126. }
  127. static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
  128. {
  129. struct drm_cmdline_mode *cmdline_mode;
  130. struct drm_display_mode *mode;
  131. cmdline_mode = &connector->cmdline_mode;
  132. if (!cmdline_mode->specified)
  133. return 0;
  134. /* Only add a GTF mode if we find no matching probed modes */
  135. list_for_each_entry(mode, &connector->probed_modes, head) {
  136. if (mode->hdisplay != cmdline_mode->xres ||
  137. mode->vdisplay != cmdline_mode->yres)
  138. continue;
  139. if (cmdline_mode->refresh_specified) {
  140. /* The probed mode's vrefresh is set until later */
  141. if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
  142. continue;
  143. }
  144. /* Mark the matching mode as being preferred by the user */
  145. mode->type |= DRM_MODE_TYPE_USERDEF;
  146. return 0;
  147. }
  148. mode = drm_mode_create_from_cmdline_mode(connector->dev,
  149. cmdline_mode);
  150. if (mode == NULL)
  151. return 0;
  152. drm_mode_probed_add(connector, mode);
  153. return 1;
  154. }
  155. enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
  156. const struct drm_display_mode *mode)
  157. {
  158. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  159. if (!crtc_funcs || !crtc_funcs->mode_valid)
  160. return MODE_OK;
  161. return crtc_funcs->mode_valid(crtc, mode);
  162. }
  163. enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
  164. const struct drm_display_mode *mode)
  165. {
  166. const struct drm_encoder_helper_funcs *encoder_funcs =
  167. encoder->helper_private;
  168. if (!encoder_funcs || !encoder_funcs->mode_valid)
  169. return MODE_OK;
  170. return encoder_funcs->mode_valid(encoder, mode);
  171. }
  172. int
  173. drm_connector_mode_valid(struct drm_connector *connector,
  174. struct drm_display_mode *mode,
  175. struct drm_modeset_acquire_ctx *ctx,
  176. enum drm_mode_status *status)
  177. {
  178. const struct drm_connector_helper_funcs *connector_funcs =
  179. connector->helper_private;
  180. int ret = 0;
  181. if (!connector_funcs)
  182. *status = MODE_OK;
  183. else if (connector_funcs->mode_valid_ctx)
  184. ret = connector_funcs->mode_valid_ctx(connector, mode, ctx,
  185. status);
  186. else if (connector_funcs->mode_valid)
  187. *status = connector_funcs->mode_valid(connector, mode);
  188. else
  189. *status = MODE_OK;
  190. return ret;
  191. }
  192. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  193. /**
  194. * drm_kms_helper_poll_enable - re-enable output polling.
  195. * @dev: drm_device
  196. *
  197. * This function re-enables the output polling work, after it has been
  198. * temporarily disabled using drm_kms_helper_poll_disable(), for example over
  199. * suspend/resume.
  200. *
  201. * Drivers can call this helper from their device resume implementation. It is
  202. * not an error to call this even when output polling isn't enabled.
  203. *
  204. * Note that calls to enable and disable polling must be strictly ordered, which
  205. * is automatically the case when they're only call from suspend/resume
  206. * callbacks.
  207. */
  208. void drm_kms_helper_poll_enable(struct drm_device *dev)
  209. {
  210. bool poll = false;
  211. struct drm_connector *connector;
  212. struct drm_connector_list_iter conn_iter;
  213. unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
  214. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  215. return;
  216. drm_connector_list_iter_begin(dev, &conn_iter);
  217. drm_for_each_connector_iter(connector, &conn_iter) {
  218. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  219. DRM_CONNECTOR_POLL_DISCONNECT))
  220. poll = true;
  221. }
  222. drm_connector_list_iter_end(&conn_iter);
  223. if (dev->mode_config.delayed_event) {
  224. /*
  225. * FIXME:
  226. *
  227. * Use short (1s) delay to handle the initial delayed event.
  228. * This delay should not be needed, but Optimus/nouveau will
  229. * fail in a mysterious way if the delayed event is handled as
  230. * soon as possible like it is done in
  231. * drm_helper_probe_single_connector_modes() in case the poll
  232. * was enabled before.
  233. */
  234. poll = true;
  235. delay = HZ;
  236. }
  237. if (poll)
  238. schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
  239. }
  240. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  241. static enum drm_connector_status
  242. drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
  243. {
  244. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  245. struct drm_modeset_acquire_ctx ctx;
  246. int ret;
  247. drm_modeset_acquire_init(&ctx, 0);
  248. retry:
  249. ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
  250. if (!ret) {
  251. if (funcs->detect_ctx)
  252. ret = funcs->detect_ctx(connector, &ctx, force);
  253. else if (connector->funcs->detect)
  254. ret = connector->funcs->detect(connector, force);
  255. else
  256. ret = connector_status_connected;
  257. }
  258. if (ret == -EDEADLK) {
  259. drm_modeset_backoff(&ctx);
  260. goto retry;
  261. }
  262. if (WARN_ON(ret < 0))
  263. ret = connector_status_unknown;
  264. if (ret != connector->status)
  265. connector->epoch_counter += 1;
  266. drm_modeset_drop_locks(&ctx);
  267. drm_modeset_acquire_fini(&ctx);
  268. return ret;
  269. }
  270. /**
  271. * drm_helper_probe_detect - probe connector status
  272. * @connector: connector to probe
  273. * @ctx: acquire_ctx, or NULL to let this function handle locking.
  274. * @force: Whether destructive probe operations should be performed.
  275. *
  276. * This function calls the detect callbacks of the connector.
  277. * This function returns &drm_connector_status, or
  278. * if @ctx is set, it might also return -EDEADLK.
  279. */
  280. int
  281. drm_helper_probe_detect(struct drm_connector *connector,
  282. struct drm_modeset_acquire_ctx *ctx,
  283. bool force)
  284. {
  285. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  286. struct drm_device *dev = connector->dev;
  287. int ret;
  288. if (!ctx)
  289. return drm_helper_probe_detect_ctx(connector, force);
  290. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
  291. if (ret)
  292. return ret;
  293. if (funcs->detect_ctx)
  294. ret = funcs->detect_ctx(connector, ctx, force);
  295. else if (connector->funcs->detect)
  296. ret = connector->funcs->detect(connector, force);
  297. else
  298. ret = connector_status_connected;
  299. if (ret != connector->status)
  300. connector->epoch_counter += 1;
  301. return ret;
  302. }
  303. EXPORT_SYMBOL(drm_helper_probe_detect);
  304. /**
  305. * drm_helper_probe_single_connector_modes - get complete set of display modes
  306. * @connector: connector to probe
  307. * @maxX: max width for modes
  308. * @maxY: max height for modes
  309. *
  310. * Based on the helper callbacks implemented by @connector in struct
  311. * &drm_connector_helper_funcs try to detect all valid modes. Modes will first
  312. * be added to the connector's probed_modes list, then culled (based on validity
  313. * and the @maxX, @maxY parameters) and put into the normal modes list.
  314. *
  315. * Intended to be used as a generic implementation of the
  316. * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
  317. * for output mode filtering and detection.
  318. *
  319. * The basic procedure is as follows
  320. *
  321. * 1. All modes currently on the connector's modes list are marked as stale
  322. *
  323. * 2. New modes are added to the connector's probed_modes list with
  324. * drm_mode_probed_add(). New modes start their life with status as OK.
  325. * Modes are added from a single source using the following priority order.
  326. *
  327. * - &drm_connector_helper_funcs.get_modes vfunc
  328. * - if the connector status is connector_status_connected, standard
  329. * VESA DMT modes up to 1024x768 are automatically added
  330. * (drm_add_modes_noedid())
  331. *
  332. * Finally modes specified via the kernel command line (video=...) are
  333. * added in addition to what the earlier probes produced
  334. * (drm_helper_probe_add_cmdline_mode()). These modes are generated
  335. * using the VESA GTF/CVT formulas.
  336. *
  337. * 3. Modes are moved from the probed_modes list to the modes list. Potential
  338. * duplicates are merged together (see drm_connector_list_update()).
  339. * After this step the probed_modes list will be empty again.
  340. *
  341. * 4. Any non-stale mode on the modes list then undergoes validation
  342. *
  343. * - drm_mode_validate_basic() performs basic sanity checks
  344. * - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
  345. * (if specified)
  346. * - drm_mode_validate_flag() checks the modes against basic connector
  347. * capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
  348. * - the optional &drm_connector_helper_funcs.mode_valid or
  349. * &drm_connector_helper_funcs.mode_valid_ctx helpers can perform driver
  350. * and/or sink specific checks
  351. * - the optional &drm_crtc_helper_funcs.mode_valid,
  352. * &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
  353. * helpers can perform driver and/or source specific checks which are also
  354. * enforced by the modeset/atomic helpers
  355. *
  356. * 5. Any mode whose status is not OK is pruned from the connector's modes list,
  357. * accompanied by a debug message indicating the reason for the mode's
  358. * rejection (see drm_mode_prune_invalid()).
  359. *
  360. * Returns:
  361. * The number of modes found on @connector.
  362. */
  363. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  364. uint32_t maxX, uint32_t maxY)
  365. {
  366. struct drm_device *dev = connector->dev;
  367. struct drm_display_mode *mode;
  368. const struct drm_connector_helper_funcs *connector_funcs =
  369. connector->helper_private;
  370. int count = 0, ret;
  371. int mode_flags = 0;
  372. bool verbose_prune = true;
  373. enum drm_connector_status old_status;
  374. struct drm_modeset_acquire_ctx ctx;
  375. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  376. drm_modeset_acquire_init(&ctx, 0);
  377. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  378. connector->name);
  379. retry:
  380. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  381. if (ret == -EDEADLK) {
  382. drm_modeset_backoff(&ctx);
  383. goto retry;
  384. } else
  385. WARN_ON(ret < 0);
  386. /* set all old modes to the stale state */
  387. list_for_each_entry(mode, &connector->modes, head)
  388. mode->status = MODE_STALE;
  389. old_status = connector->status;
  390. if (connector->force) {
  391. if (connector->force == DRM_FORCE_ON ||
  392. connector->force == DRM_FORCE_ON_DIGITAL)
  393. connector->status = connector_status_connected;
  394. else
  395. connector->status = connector_status_disconnected;
  396. if (connector->funcs->force)
  397. connector->funcs->force(connector);
  398. } else {
  399. ret = drm_helper_probe_detect(connector, &ctx, true);
  400. if (ret == -EDEADLK) {
  401. drm_modeset_backoff(&ctx);
  402. goto retry;
  403. } else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
  404. ret = connector_status_unknown;
  405. connector->status = ret;
  406. }
  407. /*
  408. * Normally either the driver's hpd code or the poll loop should
  409. * pick up any changes and fire the hotplug event. But if
  410. * userspace sneaks in a probe, we might miss a change. Hence
  411. * check here, and if anything changed start the hotplug code.
  412. */
  413. if (old_status != connector->status) {
  414. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  415. connector->base.id,
  416. connector->name,
  417. drm_get_connector_status_name(old_status),
  418. drm_get_connector_status_name(connector->status));
  419. /*
  420. * The hotplug event code might call into the fb
  421. * helpers, and so expects that we do not hold any
  422. * locks. Fire up the poll struct instead, it will
  423. * disable itself again.
  424. */
  425. dev->mode_config.delayed_event = true;
  426. if (dev->mode_config.poll_enabled)
  427. schedule_delayed_work(&dev->mode_config.output_poll_work,
  428. 0);
  429. }
  430. /* Re-enable polling in case the global poll config changed. */
  431. if (drm_kms_helper_poll != dev->mode_config.poll_running)
  432. drm_kms_helper_poll_enable(dev);
  433. dev->mode_config.poll_running = drm_kms_helper_poll;
  434. if (connector->status == connector_status_disconnected) {
  435. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  436. connector->base.id, connector->name);
  437. drm_connector_update_edid_property(connector, NULL);
  438. verbose_prune = false;
  439. goto prune;
  440. }
  441. count = (*connector_funcs->get_modes)(connector);
  442. /*
  443. * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
  444. * override/firmware EDID.
  445. */
  446. if (count == 0 && connector->status == connector_status_connected)
  447. count = drm_add_override_edid_modes(connector);
  448. if (count == 0 && connector->status == connector_status_connected)
  449. count = drm_add_modes_noedid(connector, 1024, 768);
  450. count += drm_helper_probe_add_cmdline_mode(connector);
  451. if (count == 0)
  452. goto prune;
  453. drm_connector_list_update(connector);
  454. if (connector->interlace_allowed)
  455. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  456. if (connector->doublescan_allowed)
  457. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  458. if (connector->stereo_allowed)
  459. mode_flags |= DRM_MODE_FLAG_3D_MASK;
  460. list_for_each_entry(mode, &connector->modes, head) {
  461. if (mode->status != MODE_OK)
  462. continue;
  463. mode->status = drm_mode_validate_driver(dev, mode);
  464. if (mode->status != MODE_OK)
  465. continue;
  466. mode->status = drm_mode_validate_size(mode, maxX, maxY);
  467. if (mode->status != MODE_OK)
  468. continue;
  469. mode->status = drm_mode_validate_flag(mode, mode_flags);
  470. if (mode->status != MODE_OK)
  471. continue;
  472. ret = drm_mode_validate_pipeline(mode, connector, &ctx,
  473. &mode->status);
  474. if (ret) {
  475. drm_dbg_kms(dev,
  476. "drm_mode_validate_pipeline failed: %d\n",
  477. ret);
  478. if (drm_WARN_ON_ONCE(dev, ret != -EDEADLK)) {
  479. mode->status = MODE_ERROR;
  480. } else {
  481. drm_modeset_backoff(&ctx);
  482. goto retry;
  483. }
  484. }
  485. if (mode->status != MODE_OK)
  486. continue;
  487. mode->status = drm_mode_validate_ycbcr420(mode, connector);
  488. }
  489. prune:
  490. drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
  491. drm_modeset_drop_locks(&ctx);
  492. drm_modeset_acquire_fini(&ctx);
  493. if (list_empty(&connector->modes))
  494. return 0;
  495. drm_mode_sort(&connector->modes);
  496. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  497. connector->name);
  498. list_for_each_entry(mode, &connector->modes, head) {
  499. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  500. drm_mode_debug_printmodeline(mode);
  501. }
  502. return count;
  503. }
  504. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  505. /**
  506. * drm_kms_helper_hotplug_event - fire off KMS hotplug events
  507. * @dev: drm_device whose connector state changed
  508. *
  509. * This function fires off the uevent for userspace and also calls the
  510. * output_poll_changed function, which is most commonly used to inform the fbdev
  511. * emulation code and allow it to update the fbcon output configuration.
  512. *
  513. * Drivers should call this from their hotplug handling code when a change is
  514. * detected. Note that this function does not do any output detection of its
  515. * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
  516. * driver already.
  517. *
  518. * This function must be called from process context with no mode
  519. * setting locks held.
  520. */
  521. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  522. {
  523. /* send a uevent + call fbdev */
  524. drm_sysfs_hotplug_event(dev);
  525. if (dev->mode_config.funcs->output_poll_changed)
  526. dev->mode_config.funcs->output_poll_changed(dev);
  527. drm_client_dev_hotplug(dev);
  528. }
  529. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  530. static void output_poll_execute(struct work_struct *work)
  531. {
  532. struct delayed_work *delayed_work = to_delayed_work(work);
  533. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  534. struct drm_connector *connector;
  535. struct drm_connector_list_iter conn_iter;
  536. enum drm_connector_status old_status;
  537. bool repoll = false, changed;
  538. u64 old_epoch_counter;
  539. if (!dev->mode_config.poll_enabled)
  540. return;
  541. /* Pick up any changes detected by the probe functions. */
  542. changed = dev->mode_config.delayed_event;
  543. dev->mode_config.delayed_event = false;
  544. if (!drm_kms_helper_poll)
  545. goto out;
  546. if (!mutex_trylock(&dev->mode_config.mutex)) {
  547. repoll = true;
  548. goto out;
  549. }
  550. drm_connector_list_iter_begin(dev, &conn_iter);
  551. drm_for_each_connector_iter(connector, &conn_iter) {
  552. /* Ignore forced connectors. */
  553. if (connector->force)
  554. continue;
  555. /* Ignore HPD capable connectors and connectors where we don't
  556. * want any hotplug detection at all for polling. */
  557. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  558. continue;
  559. old_status = connector->status;
  560. /* if we are connected and don't want to poll for disconnect
  561. skip it */
  562. if (old_status == connector_status_connected &&
  563. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  564. continue;
  565. repoll = true;
  566. old_epoch_counter = connector->epoch_counter;
  567. connector->status = drm_helper_probe_detect(connector, NULL, false);
  568. if (old_epoch_counter != connector->epoch_counter) {
  569. const char *old, *new;
  570. /*
  571. * The poll work sets force=false when calling detect so
  572. * that drivers can avoid to do disruptive tests (e.g.
  573. * when load detect cycles could cause flickering on
  574. * other, running displays). This bears the risk that we
  575. * flip-flop between unknown here in the poll work and
  576. * the real state when userspace forces a full detect
  577. * call after receiving a hotplug event due to this
  578. * change.
  579. *
  580. * Hence clamp an unknown detect status to the old
  581. * value.
  582. */
  583. if (connector->status == connector_status_unknown) {
  584. connector->status = old_status;
  585. continue;
  586. }
  587. old = drm_get_connector_status_name(old_status);
  588. new = drm_get_connector_status_name(connector->status);
  589. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  590. "status updated from %s to %s\n",
  591. connector->base.id,
  592. connector->name,
  593. old, new);
  594. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
  595. connector->base.id, connector->name,
  596. old_epoch_counter, connector->epoch_counter);
  597. changed = true;
  598. }
  599. }
  600. drm_connector_list_iter_end(&conn_iter);
  601. mutex_unlock(&dev->mode_config.mutex);
  602. out:
  603. if (changed)
  604. drm_kms_helper_hotplug_event(dev);
  605. if (repoll)
  606. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  607. }
  608. /**
  609. * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
  610. *
  611. * Determine if %current task is an output poll worker. This can be used
  612. * to select distinct code paths for output polling versus other contexts.
  613. *
  614. * One use case is to avoid a deadlock between the output poll worker and
  615. * the autosuspend worker wherein the latter waits for polling to finish
  616. * upon calling drm_kms_helper_poll_disable(), while the former waits for
  617. * runtime suspend to finish upon calling pm_runtime_get_sync() in a
  618. * connector ->detect hook.
  619. */
  620. bool drm_kms_helper_is_poll_worker(void)
  621. {
  622. struct work_struct *work = current_work();
  623. return work && work->func == output_poll_execute;
  624. }
  625. EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
  626. /**
  627. * drm_kms_helper_poll_disable - disable output polling
  628. * @dev: drm_device
  629. *
  630. * This function disables the output polling work.
  631. *
  632. * Drivers can call this helper from their device suspend implementation. It is
  633. * not an error to call this even when output polling isn't enabled or already
  634. * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
  635. *
  636. * Note that calls to enable and disable polling must be strictly ordered, which
  637. * is automatically the case when they're only call from suspend/resume
  638. * callbacks.
  639. */
  640. void drm_kms_helper_poll_disable(struct drm_device *dev)
  641. {
  642. if (!dev->mode_config.poll_enabled)
  643. return;
  644. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  645. }
  646. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  647. /**
  648. * drm_kms_helper_poll_init - initialize and enable output polling
  649. * @dev: drm_device
  650. *
  651. * This function intializes and then also enables output polling support for
  652. * @dev. Drivers which do not have reliable hotplug support in hardware can use
  653. * this helper infrastructure to regularly poll such connectors for changes in
  654. * their connection state.
  655. *
  656. * Drivers can control which connectors are polled by setting the
  657. * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
  658. * connectors where probing live outputs can result in visual distortion drivers
  659. * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
  660. * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
  661. * completely ignored by the polling logic.
  662. *
  663. * Note that a connector can be both polled and probed from the hotplug handler,
  664. * in case the hotplug interrupt is known to be unreliable.
  665. */
  666. void drm_kms_helper_poll_init(struct drm_device *dev)
  667. {
  668. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  669. dev->mode_config.poll_enabled = true;
  670. drm_kms_helper_poll_enable(dev);
  671. }
  672. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  673. /**
  674. * drm_kms_helper_poll_fini - disable output polling and clean it up
  675. * @dev: drm_device
  676. */
  677. void drm_kms_helper_poll_fini(struct drm_device *dev)
  678. {
  679. if (!dev->mode_config.poll_enabled)
  680. return;
  681. dev->mode_config.poll_enabled = false;
  682. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  683. }
  684. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  685. /**
  686. * drm_helper_hpd_irq_event - hotplug processing
  687. * @dev: drm_device
  688. *
  689. * Drivers can use this helper function to run a detect cycle on all connectors
  690. * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
  691. * other connectors are ignored, which is useful to avoid reprobing fixed
  692. * panels.
  693. *
  694. * This helper function is useful for drivers which can't or don't track hotplug
  695. * interrupts for each connector.
  696. *
  697. * Drivers which support hotplug interrupts for each connector individually and
  698. * which have a more fine-grained detect logic should bypass this code and
  699. * directly call drm_kms_helper_hotplug_event() in case the connector state
  700. * changed.
  701. *
  702. * This function must be called from process context with no mode
  703. * setting locks held.
  704. *
  705. * Note that a connector can be both polled and probed from the hotplug handler,
  706. * in case the hotplug interrupt is known to be unreliable.
  707. */
  708. bool drm_helper_hpd_irq_event(struct drm_device *dev)
  709. {
  710. struct drm_connector *connector;
  711. struct drm_connector_list_iter conn_iter;
  712. enum drm_connector_status old_status;
  713. bool changed = false;
  714. u64 old_epoch_counter;
  715. if (!dev->mode_config.poll_enabled)
  716. return false;
  717. mutex_lock(&dev->mode_config.mutex);
  718. drm_connector_list_iter_begin(dev, &conn_iter);
  719. drm_for_each_connector_iter(connector, &conn_iter) {
  720. /* Only handle HPD capable connectors. */
  721. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  722. continue;
  723. old_status = connector->status;
  724. old_epoch_counter = connector->epoch_counter;
  725. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] Old epoch counter %llu\n", connector->base.id,
  726. connector->name,
  727. old_epoch_counter);
  728. connector->status = drm_helper_probe_detect(connector, NULL, false);
  729. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  730. connector->base.id,
  731. connector->name,
  732. drm_get_connector_status_name(old_status),
  733. drm_get_connector_status_name(connector->status));
  734. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] New epoch counter %llu\n",
  735. connector->base.id,
  736. connector->name,
  737. connector->epoch_counter);
  738. /*
  739. * Check if epoch counter had changed, meaning that we need
  740. * to send a uevent.
  741. */
  742. if (old_epoch_counter != connector->epoch_counter)
  743. changed = true;
  744. }
  745. drm_connector_list_iter_end(&conn_iter);
  746. mutex_unlock(&dev->mode_config.mutex);
  747. if (changed) {
  748. drm_kms_helper_hotplug_event(dev);
  749. DRM_DEBUG_KMS("Sent hotplug event\n");
  750. }
  751. return changed;
  752. }
  753. EXPORT_SYMBOL(drm_helper_hpd_irq_event);