drm_probe_helper.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <[email protected]>
  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 <[email protected]>
  28. * Dave Airlie <[email protected]>
  29. * Jesse Barnes <[email protected]>
  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. static int drm_helper_probe_get_modes(struct drm_connector *connector)
  305. {
  306. const struct drm_connector_helper_funcs *connector_funcs =
  307. connector->helper_private;
  308. int count;
  309. count = connector_funcs->get_modes(connector);
  310. /*
  311. * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
  312. * override/firmware EDID.
  313. */
  314. if (count == 0 && connector->status == connector_status_connected)
  315. count = drm_add_override_edid_modes(connector);
  316. return count;
  317. }
  318. static int __drm_helper_update_and_validate(struct drm_connector *connector,
  319. uint32_t maxX, uint32_t maxY,
  320. struct drm_modeset_acquire_ctx *ctx)
  321. {
  322. struct drm_device *dev = connector->dev;
  323. struct drm_display_mode *mode;
  324. int mode_flags = 0;
  325. int ret;
  326. drm_connector_list_update(connector);
  327. if (connector->interlace_allowed)
  328. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  329. if (connector->doublescan_allowed)
  330. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  331. if (connector->stereo_allowed)
  332. mode_flags |= DRM_MODE_FLAG_3D_MASK;
  333. list_for_each_entry(mode, &connector->modes, head) {
  334. if (mode->status != MODE_OK)
  335. continue;
  336. mode->status = drm_mode_validate_driver(dev, mode);
  337. if (mode->status != MODE_OK)
  338. continue;
  339. mode->status = drm_mode_validate_size(mode, maxX, maxY);
  340. if (mode->status != MODE_OK)
  341. continue;
  342. mode->status = drm_mode_validate_flag(mode, mode_flags);
  343. if (mode->status != MODE_OK)
  344. continue;
  345. ret = drm_mode_validate_pipeline(mode, connector, ctx,
  346. &mode->status);
  347. if (ret) {
  348. drm_dbg_kms(dev,
  349. "drm_mode_validate_pipeline failed: %d\n",
  350. ret);
  351. if (drm_WARN_ON_ONCE(dev, ret != -EDEADLK))
  352. mode->status = MODE_ERROR;
  353. else
  354. return -EDEADLK;
  355. }
  356. if (mode->status != MODE_OK)
  357. continue;
  358. mode->status = drm_mode_validate_ycbcr420(mode, connector);
  359. }
  360. return 0;
  361. }
  362. /**
  363. * drm_helper_probe_single_connector_modes - get complete set of display modes
  364. * @connector: connector to probe
  365. * @maxX: max width for modes
  366. * @maxY: max height for modes
  367. *
  368. * Based on the helper callbacks implemented by @connector in struct
  369. * &drm_connector_helper_funcs try to detect all valid modes. Modes will first
  370. * be added to the connector's probed_modes list, then culled (based on validity
  371. * and the @maxX, @maxY parameters) and put into the normal modes list.
  372. *
  373. * Intended to be used as a generic implementation of the
  374. * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
  375. * for output mode filtering and detection.
  376. *
  377. * The basic procedure is as follows
  378. *
  379. * 1. All modes currently on the connector's modes list are marked as stale
  380. *
  381. * 2. New modes are added to the connector's probed_modes list with
  382. * drm_mode_probed_add(). New modes start their life with status as OK.
  383. * Modes are added from a single source using the following priority order.
  384. *
  385. * - &drm_connector_helper_funcs.get_modes vfunc
  386. * - if the connector status is connector_status_connected, standard
  387. * VESA DMT modes up to 1024x768 are automatically added
  388. * (drm_add_modes_noedid())
  389. *
  390. * Finally modes specified via the kernel command line (video=...) are
  391. * added in addition to what the earlier probes produced
  392. * (drm_helper_probe_add_cmdline_mode()). These modes are generated
  393. * using the VESA GTF/CVT formulas.
  394. *
  395. * 3. Modes are moved from the probed_modes list to the modes list. Potential
  396. * duplicates are merged together (see drm_connector_list_update()).
  397. * After this step the probed_modes list will be empty again.
  398. *
  399. * 4. Any non-stale mode on the modes list then undergoes validation
  400. *
  401. * - drm_mode_validate_basic() performs basic sanity checks
  402. * - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
  403. * (if specified)
  404. * - drm_mode_validate_flag() checks the modes against basic connector
  405. * capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
  406. * - the optional &drm_connector_helper_funcs.mode_valid or
  407. * &drm_connector_helper_funcs.mode_valid_ctx helpers can perform driver
  408. * and/or sink specific checks
  409. * - the optional &drm_crtc_helper_funcs.mode_valid,
  410. * &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
  411. * helpers can perform driver and/or source specific checks which are also
  412. * enforced by the modeset/atomic helpers
  413. *
  414. * 5. Any mode whose status is not OK is pruned from the connector's modes list,
  415. * accompanied by a debug message indicating the reason for the mode's
  416. * rejection (see drm_mode_prune_invalid()).
  417. *
  418. * Returns:
  419. * The number of modes found on @connector.
  420. */
  421. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  422. uint32_t maxX, uint32_t maxY)
  423. {
  424. struct drm_device *dev = connector->dev;
  425. struct drm_display_mode *mode;
  426. int count = 0, ret;
  427. enum drm_connector_status old_status;
  428. struct drm_modeset_acquire_ctx ctx;
  429. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  430. drm_modeset_acquire_init(&ctx, 0);
  431. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  432. connector->name);
  433. retry:
  434. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  435. if (ret == -EDEADLK) {
  436. drm_modeset_backoff(&ctx);
  437. goto retry;
  438. } else
  439. WARN_ON(ret < 0);
  440. /* set all old modes to the stale state */
  441. list_for_each_entry(mode, &connector->modes, head)
  442. mode->status = MODE_STALE;
  443. old_status = connector->status;
  444. if (connector->force) {
  445. if (connector->force == DRM_FORCE_ON ||
  446. connector->force == DRM_FORCE_ON_DIGITAL)
  447. connector->status = connector_status_connected;
  448. else
  449. connector->status = connector_status_disconnected;
  450. if (connector->funcs->force)
  451. connector->funcs->force(connector);
  452. } else {
  453. ret = drm_helper_probe_detect(connector, &ctx, true);
  454. if (ret == -EDEADLK) {
  455. drm_modeset_backoff(&ctx);
  456. goto retry;
  457. } else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
  458. ret = connector_status_unknown;
  459. connector->status = ret;
  460. }
  461. /*
  462. * Normally either the driver's hpd code or the poll loop should
  463. * pick up any changes and fire the hotplug event. But if
  464. * userspace sneaks in a probe, we might miss a change. Hence
  465. * check here, and if anything changed start the hotplug code.
  466. */
  467. if (old_status != connector->status) {
  468. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  469. connector->base.id,
  470. connector->name,
  471. drm_get_connector_status_name(old_status),
  472. drm_get_connector_status_name(connector->status));
  473. /*
  474. * The hotplug event code might call into the fb
  475. * helpers, and so expects that we do not hold any
  476. * locks. Fire up the poll struct instead, it will
  477. * disable itself again.
  478. */
  479. dev->mode_config.delayed_event = true;
  480. if (dev->mode_config.poll_enabled)
  481. mod_delayed_work(system_wq,
  482. &dev->mode_config.output_poll_work,
  483. 0);
  484. }
  485. /* Re-enable polling in case the global poll config changed. */
  486. if (drm_kms_helper_poll != dev->mode_config.poll_running)
  487. drm_kms_helper_poll_enable(dev);
  488. dev->mode_config.poll_running = drm_kms_helper_poll;
  489. if (connector->status == connector_status_disconnected) {
  490. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  491. connector->base.id, connector->name);
  492. drm_connector_update_edid_property(connector, NULL);
  493. drm_mode_prune_invalid(dev, &connector->modes, false);
  494. goto exit;
  495. }
  496. count = drm_helper_probe_get_modes(connector);
  497. if (count == 0 && (connector->status == connector_status_connected ||
  498. connector->status == connector_status_unknown)) {
  499. count = drm_add_modes_noedid(connector, 1024, 768);
  500. /*
  501. * Section 4.2.2.6 (EDID Corruption Detection) of the DP 1.4a
  502. * Link CTS specifies that 640x480 (the official "failsafe"
  503. * mode) needs to be the default if there's no EDID.
  504. */
  505. if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
  506. drm_set_preferred_mode(connector, 640, 480);
  507. }
  508. count += drm_helper_probe_add_cmdline_mode(connector);
  509. if (count != 0) {
  510. ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
  511. if (ret == -EDEADLK) {
  512. drm_modeset_backoff(&ctx);
  513. goto retry;
  514. }
  515. }
  516. drm_mode_prune_invalid(dev, &connector->modes, true);
  517. /*
  518. * Displayport spec section 5.2.1.2 ("Video Timing Format") says that
  519. * all detachable sinks shall support 640x480 @60Hz as a fail safe
  520. * mode. If all modes were pruned, perhaps because they need more
  521. * lanes or a higher pixel clock than available, at least try to add
  522. * in 640x480.
  523. */
  524. if (list_empty(&connector->modes) &&
  525. connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
  526. count = drm_add_modes_noedid(connector, 640, 480);
  527. ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
  528. if (ret == -EDEADLK) {
  529. drm_modeset_backoff(&ctx);
  530. goto retry;
  531. }
  532. drm_mode_prune_invalid(dev, &connector->modes, true);
  533. }
  534. exit:
  535. drm_modeset_drop_locks(&ctx);
  536. drm_modeset_acquire_fini(&ctx);
  537. if (list_empty(&connector->modes))
  538. return 0;
  539. drm_mode_sort(&connector->modes);
  540. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  541. connector->name);
  542. list_for_each_entry(mode, &connector->modes, head) {
  543. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  544. drm_mode_debug_printmodeline(mode);
  545. }
  546. return count;
  547. }
  548. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  549. /**
  550. * drm_kms_helper_hotplug_event - fire off KMS hotplug events
  551. * @dev: drm_device whose connector state changed
  552. *
  553. * This function fires off the uevent for userspace and also calls the
  554. * output_poll_changed function, which is most commonly used to inform the fbdev
  555. * emulation code and allow it to update the fbcon output configuration.
  556. *
  557. * Drivers should call this from their hotplug handling code when a change is
  558. * detected. Note that this function does not do any output detection of its
  559. * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
  560. * driver already.
  561. *
  562. * This function must be called from process context with no mode
  563. * setting locks held.
  564. *
  565. * If only a single connector has changed, consider calling
  566. * drm_kms_helper_connector_hotplug_event() instead.
  567. */
  568. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  569. {
  570. /* send a uevent + call fbdev */
  571. drm_sysfs_hotplug_event(dev);
  572. if (dev->mode_config.funcs->output_poll_changed)
  573. dev->mode_config.funcs->output_poll_changed(dev);
  574. drm_client_dev_hotplug(dev);
  575. }
  576. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  577. /**
  578. * drm_kms_helper_connector_hotplug_event - fire off a KMS connector hotplug event
  579. * @connector: drm_connector which has changed
  580. *
  581. * This is the same as drm_kms_helper_hotplug_event(), except it fires a more
  582. * fine-grained uevent for a single connector.
  583. */
  584. void drm_kms_helper_connector_hotplug_event(struct drm_connector *connector)
  585. {
  586. struct drm_device *dev = connector->dev;
  587. /* send a uevent + call fbdev */
  588. drm_sysfs_connector_hotplug_event(connector);
  589. if (dev->mode_config.funcs->output_poll_changed)
  590. dev->mode_config.funcs->output_poll_changed(dev);
  591. drm_client_dev_hotplug(dev);
  592. }
  593. EXPORT_SYMBOL(drm_kms_helper_connector_hotplug_event);
  594. static void output_poll_execute(struct work_struct *work)
  595. {
  596. struct delayed_work *delayed_work = to_delayed_work(work);
  597. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  598. struct drm_connector *connector;
  599. struct drm_connector_list_iter conn_iter;
  600. enum drm_connector_status old_status;
  601. bool repoll = false, changed;
  602. u64 old_epoch_counter;
  603. if (!dev->mode_config.poll_enabled)
  604. return;
  605. /* Pick up any changes detected by the probe functions. */
  606. changed = dev->mode_config.delayed_event;
  607. dev->mode_config.delayed_event = false;
  608. if (!drm_kms_helper_poll)
  609. goto out;
  610. if (!mutex_trylock(&dev->mode_config.mutex)) {
  611. repoll = true;
  612. goto out;
  613. }
  614. drm_connector_list_iter_begin(dev, &conn_iter);
  615. drm_for_each_connector_iter(connector, &conn_iter) {
  616. /* Ignore forced connectors. */
  617. if (connector->force)
  618. continue;
  619. /* Ignore HPD capable connectors and connectors where we don't
  620. * want any hotplug detection at all for polling. */
  621. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  622. continue;
  623. old_status = connector->status;
  624. /* if we are connected and don't want to poll for disconnect
  625. skip it */
  626. if (old_status == connector_status_connected &&
  627. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  628. continue;
  629. repoll = true;
  630. old_epoch_counter = connector->epoch_counter;
  631. connector->status = drm_helper_probe_detect(connector, NULL, false);
  632. if (old_epoch_counter != connector->epoch_counter) {
  633. const char *old, *new;
  634. /*
  635. * The poll work sets force=false when calling detect so
  636. * that drivers can avoid to do disruptive tests (e.g.
  637. * when load detect cycles could cause flickering on
  638. * other, running displays). This bears the risk that we
  639. * flip-flop between unknown here in the poll work and
  640. * the real state when userspace forces a full detect
  641. * call after receiving a hotplug event due to this
  642. * change.
  643. *
  644. * Hence clamp an unknown detect status to the old
  645. * value.
  646. */
  647. if (connector->status == connector_status_unknown) {
  648. connector->status = old_status;
  649. continue;
  650. }
  651. old = drm_get_connector_status_name(old_status);
  652. new = drm_get_connector_status_name(connector->status);
  653. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  654. "status updated from %s to %s\n",
  655. connector->base.id,
  656. connector->name,
  657. old, new);
  658. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
  659. connector->base.id, connector->name,
  660. old_epoch_counter, connector->epoch_counter);
  661. changed = true;
  662. }
  663. }
  664. drm_connector_list_iter_end(&conn_iter);
  665. mutex_unlock(&dev->mode_config.mutex);
  666. out:
  667. if (changed)
  668. drm_kms_helper_hotplug_event(dev);
  669. if (repoll)
  670. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  671. }
  672. /**
  673. * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
  674. *
  675. * Determine if %current task is an output poll worker. This can be used
  676. * to select distinct code paths for output polling versus other contexts.
  677. *
  678. * One use case is to avoid a deadlock between the output poll worker and
  679. * the autosuspend worker wherein the latter waits for polling to finish
  680. * upon calling drm_kms_helper_poll_disable(), while the former waits for
  681. * runtime suspend to finish upon calling pm_runtime_get_sync() in a
  682. * connector ->detect hook.
  683. */
  684. bool drm_kms_helper_is_poll_worker(void)
  685. {
  686. struct work_struct *work = current_work();
  687. return work && work->func == output_poll_execute;
  688. }
  689. EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
  690. /**
  691. * drm_kms_helper_poll_disable - disable output polling
  692. * @dev: drm_device
  693. *
  694. * This function disables the output polling work.
  695. *
  696. * Drivers can call this helper from their device suspend implementation. It is
  697. * not an error to call this even when output polling isn't enabled or already
  698. * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
  699. *
  700. * Note that calls to enable and disable polling must be strictly ordered, which
  701. * is automatically the case when they're only call from suspend/resume
  702. * callbacks.
  703. */
  704. void drm_kms_helper_poll_disable(struct drm_device *dev)
  705. {
  706. if (!dev->mode_config.poll_enabled)
  707. return;
  708. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  709. }
  710. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  711. /**
  712. * drm_kms_helper_poll_init - initialize and enable output polling
  713. * @dev: drm_device
  714. *
  715. * This function initializes and then also enables output polling support for
  716. * @dev. Drivers which do not have reliable hotplug support in hardware can use
  717. * this helper infrastructure to regularly poll such connectors for changes in
  718. * their connection state.
  719. *
  720. * Drivers can control which connectors are polled by setting the
  721. * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
  722. * connectors where probing live outputs can result in visual distortion drivers
  723. * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
  724. * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
  725. * completely ignored by the polling logic.
  726. *
  727. * Note that a connector can be both polled and probed from the hotplug handler,
  728. * in case the hotplug interrupt is known to be unreliable.
  729. */
  730. void drm_kms_helper_poll_init(struct drm_device *dev)
  731. {
  732. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  733. dev->mode_config.poll_enabled = true;
  734. drm_kms_helper_poll_enable(dev);
  735. }
  736. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  737. /**
  738. * drm_kms_helper_poll_fini - disable output polling and clean it up
  739. * @dev: drm_device
  740. */
  741. void drm_kms_helper_poll_fini(struct drm_device *dev)
  742. {
  743. if (!dev->mode_config.poll_enabled)
  744. return;
  745. dev->mode_config.poll_enabled = false;
  746. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  747. }
  748. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  749. static bool check_connector_changed(struct drm_connector *connector)
  750. {
  751. struct drm_device *dev = connector->dev;
  752. enum drm_connector_status old_status;
  753. u64 old_epoch_counter;
  754. /* Only handle HPD capable connectors. */
  755. drm_WARN_ON(dev, !(connector->polled & DRM_CONNECTOR_POLL_HPD));
  756. drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
  757. old_status = connector->status;
  758. old_epoch_counter = connector->epoch_counter;
  759. connector->status = drm_helper_probe_detect(connector, NULL, false);
  760. if (old_epoch_counter == connector->epoch_counter) {
  761. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Same epoch counter %llu\n",
  762. connector->base.id,
  763. connector->name,
  764. connector->epoch_counter);
  765. return false;
  766. }
  767. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
  768. connector->base.id,
  769. connector->name,
  770. drm_get_connector_status_name(old_status),
  771. drm_get_connector_status_name(connector->status));
  772. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Changed epoch counter %llu => %llu\n",
  773. connector->base.id,
  774. connector->name,
  775. old_epoch_counter,
  776. connector->epoch_counter);
  777. return true;
  778. }
  779. /**
  780. * drm_connector_helper_hpd_irq_event - hotplug processing
  781. * @connector: drm_connector
  782. *
  783. * Drivers can use this helper function to run a detect cycle on a connector
  784. * which has the DRM_CONNECTOR_POLL_HPD flag set in its &polled member.
  785. *
  786. * This helper function is useful for drivers which can track hotplug
  787. * interrupts for a single connector. Drivers that want to send a
  788. * hotplug event for all connectors or can't track hotplug interrupts
  789. * per connector need to use drm_helper_hpd_irq_event().
  790. *
  791. * This function must be called from process context with no mode
  792. * setting locks held.
  793. *
  794. * Note that a connector can be both polled and probed from the hotplug
  795. * handler, in case the hotplug interrupt is known to be unreliable.
  796. *
  797. * Returns:
  798. * A boolean indicating whether the connector status changed or not
  799. */
  800. bool drm_connector_helper_hpd_irq_event(struct drm_connector *connector)
  801. {
  802. struct drm_device *dev = connector->dev;
  803. bool changed;
  804. mutex_lock(&dev->mode_config.mutex);
  805. changed = check_connector_changed(connector);
  806. mutex_unlock(&dev->mode_config.mutex);
  807. if (changed) {
  808. drm_kms_helper_connector_hotplug_event(connector);
  809. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Sent hotplug event\n",
  810. connector->base.id,
  811. connector->name);
  812. }
  813. return changed;
  814. }
  815. EXPORT_SYMBOL(drm_connector_helper_hpd_irq_event);
  816. /**
  817. * drm_helper_hpd_irq_event - hotplug processing
  818. * @dev: drm_device
  819. *
  820. * Drivers can use this helper function to run a detect cycle on all connectors
  821. * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
  822. * other connectors are ignored, which is useful to avoid reprobing fixed
  823. * panels.
  824. *
  825. * This helper function is useful for drivers which can't or don't track hotplug
  826. * interrupts for each connector.
  827. *
  828. * Drivers which support hotplug interrupts for each connector individually and
  829. * which have a more fine-grained detect logic can use
  830. * drm_connector_helper_hpd_irq_event(). Alternatively, they should bypass this
  831. * code and directly call drm_kms_helper_hotplug_event() in case the connector
  832. * state changed.
  833. *
  834. * This function must be called from process context with no mode
  835. * setting locks held.
  836. *
  837. * Note that a connector can be both polled and probed from the hotplug handler,
  838. * in case the hotplug interrupt is known to be unreliable.
  839. *
  840. * Returns:
  841. * A boolean indicating whether the connector status changed or not
  842. */
  843. bool drm_helper_hpd_irq_event(struct drm_device *dev)
  844. {
  845. struct drm_connector *connector, *first_changed_connector = NULL;
  846. struct drm_connector_list_iter conn_iter;
  847. int changed = 0;
  848. if (!dev->mode_config.poll_enabled)
  849. return false;
  850. mutex_lock(&dev->mode_config.mutex);
  851. drm_connector_list_iter_begin(dev, &conn_iter);
  852. drm_for_each_connector_iter(connector, &conn_iter) {
  853. /* Only handle HPD capable connectors. */
  854. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  855. continue;
  856. if (check_connector_changed(connector)) {
  857. if (!first_changed_connector) {
  858. drm_connector_get(connector);
  859. first_changed_connector = connector;
  860. }
  861. changed++;
  862. }
  863. }
  864. drm_connector_list_iter_end(&conn_iter);
  865. mutex_unlock(&dev->mode_config.mutex);
  866. if (changed == 1)
  867. drm_kms_helper_connector_hotplug_event(first_changed_connector);
  868. else if (changed > 0)
  869. drm_kms_helper_hotplug_event(dev);
  870. if (first_changed_connector)
  871. drm_connector_put(first_changed_connector);
  872. return changed;
  873. }
  874. EXPORT_SYMBOL(drm_helper_hpd_irq_event);
  875. /**
  876. * drm_crtc_helper_mode_valid_fixed - Validates a display mode
  877. * @crtc: the crtc
  878. * @mode: the mode to validate
  879. * @fixed_mode: the display hardware's mode
  880. *
  881. * Returns:
  882. * MODE_OK on success, or another mode-status code otherwise.
  883. */
  884. enum drm_mode_status drm_crtc_helper_mode_valid_fixed(struct drm_crtc *crtc,
  885. const struct drm_display_mode *mode,
  886. const struct drm_display_mode *fixed_mode)
  887. {
  888. if (mode->hdisplay != fixed_mode->hdisplay && mode->vdisplay != fixed_mode->vdisplay)
  889. return MODE_ONE_SIZE;
  890. else if (mode->hdisplay != fixed_mode->hdisplay)
  891. return MODE_ONE_WIDTH;
  892. else if (mode->vdisplay != fixed_mode->vdisplay)
  893. return MODE_ONE_HEIGHT;
  894. return MODE_OK;
  895. }
  896. EXPORT_SYMBOL(drm_crtc_helper_mode_valid_fixed);
  897. /**
  898. * drm_connector_helper_get_modes_from_ddc - Updates the connector's EDID
  899. * property from the connector's
  900. * DDC channel
  901. * @connector: The connector
  902. *
  903. * Returns:
  904. * The number of detected display modes.
  905. *
  906. * Uses a connector's DDC channel to retrieve EDID data and update the
  907. * connector's EDID property and display modes. Drivers can use this
  908. * function to implement struct &drm_connector_helper_funcs.get_modes
  909. * for connectors with a DDC channel.
  910. */
  911. int drm_connector_helper_get_modes_from_ddc(struct drm_connector *connector)
  912. {
  913. struct edid *edid;
  914. int count = 0;
  915. if (!connector->ddc)
  916. return 0;
  917. edid = drm_get_edid(connector, connector->ddc);
  918. // clears property if EDID is NULL
  919. drm_connector_update_edid_property(connector, edid);
  920. if (edid) {
  921. count = drm_add_edid_modes(connector, edid);
  922. kfree(edid);
  923. }
  924. return count;
  925. }
  926. EXPORT_SYMBOL(drm_connector_helper_get_modes_from_ddc);
  927. /**
  928. * drm_connector_helper_get_modes_fixed - Duplicates a display mode for a connector
  929. * @connector: the connector
  930. * @fixed_mode: the display hardware's mode
  931. *
  932. * This function duplicates a display modes for a connector. Drivers for hardware
  933. * that only supports a single fixed mode can use this function in their connector's
  934. * get_modes helper.
  935. *
  936. * Returns:
  937. * The number of created modes.
  938. */
  939. int drm_connector_helper_get_modes_fixed(struct drm_connector *connector,
  940. const struct drm_display_mode *fixed_mode)
  941. {
  942. struct drm_device *dev = connector->dev;
  943. struct drm_display_mode *mode;
  944. mode = drm_mode_duplicate(dev, fixed_mode);
  945. if (!mode) {
  946. drm_err(dev, "Failed to duplicate mode " DRM_MODE_FMT "\n",
  947. DRM_MODE_ARG(fixed_mode));
  948. return 0;
  949. }
  950. if (mode->name[0] == '\0')
  951. drm_mode_set_name(mode);
  952. mode->type |= DRM_MODE_TYPE_PREFERRED;
  953. drm_mode_probed_add(connector, mode);
  954. if (mode->width_mm)
  955. connector->display_info.width_mm = mode->width_mm;
  956. if (mode->height_mm)
  957. connector->display_info.height_mm = mode->height_mm;
  958. return 1;
  959. }
  960. EXPORT_SYMBOL(drm_connector_helper_get_modes_fixed);
  961. /**
  962. * drm_connector_helper_get_modes - Read EDID and update connector.
  963. * @connector: The connector
  964. *
  965. * Read the EDID using drm_edid_read() (which requires that connector->ddc is
  966. * set), and update the connector using the EDID.
  967. *
  968. * This can be used as the "default" connector helper .get_modes() hook if the
  969. * driver does not need any special processing. This is sets the example what
  970. * custom .get_modes() hooks should do regarding EDID read and connector update.
  971. *
  972. * Returns: Number of modes.
  973. */
  974. int drm_connector_helper_get_modes(struct drm_connector *connector)
  975. {
  976. const struct drm_edid *drm_edid;
  977. int count;
  978. drm_edid = drm_edid_read(connector);
  979. /*
  980. * Unconditionally update the connector. If the EDID was read
  981. * successfully, fill in the connector information derived from the
  982. * EDID. Otherwise, if the EDID is NULL, clear the connector
  983. * information.
  984. */
  985. count = drm_edid_connector_update(connector, drm_edid);
  986. drm_edid_free(drm_edid);
  987. return count;
  988. }
  989. EXPORT_SYMBOL(drm_connector_helper_get_modes);