drm_probe_helper.c 19 KB

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