drm_crtc_helper.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  40. MODULE_DESCRIPTION("DRM KMS helper");
  41. MODULE_LICENSE("GPL and additional rights");
  42. /**
  43. * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  44. * connector list
  45. * @dev: drm device to operate on
  46. *
  47. * Some userspace presumes that the first connected connector is the main
  48. * display, where it's supposed to display e.g. the login screen. For
  49. * laptops, this should be the main panel. Use this function to sort all
  50. * (eDP/LVDS) panels to the front of the connector list, instead of
  51. * painstakingly trying to initialize them in the right order.
  52. */
  53. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  54. {
  55. struct drm_connector *connector, *tmp;
  56. struct list_head panel_list;
  57. INIT_LIST_HEAD(&panel_list);
  58. list_for_each_entry_safe(connector, tmp,
  59. &dev->mode_config.connector_list, head) {
  60. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  61. connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  62. list_move_tail(&connector->head, &panel_list);
  63. }
  64. list_splice(&panel_list, &dev->mode_config.connector_list);
  65. }
  66. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  67. static bool drm_kms_helper_poll = true;
  68. module_param_named(poll, drm_kms_helper_poll, bool, 0600);
  69. static void drm_mode_validate_flag(struct drm_connector *connector,
  70. int flags)
  71. {
  72. struct drm_display_mode *mode;
  73. if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
  74. DRM_MODE_FLAG_3D_MASK))
  75. return;
  76. list_for_each_entry(mode, &connector->modes, head) {
  77. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  78. !(flags & DRM_MODE_FLAG_INTERLACE))
  79. mode->status = MODE_NO_INTERLACE;
  80. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  81. !(flags & DRM_MODE_FLAG_DBLSCAN))
  82. mode->status = MODE_NO_DBLESCAN;
  83. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
  84. !(flags & DRM_MODE_FLAG_3D_MASK))
  85. mode->status = MODE_NO_STEREO;
  86. }
  87. return;
  88. }
  89. /**
  90. * drm_helper_probe_single_connector_modes - get complete set of display modes
  91. * @connector: connector to probe
  92. * @maxX: max width for modes
  93. * @maxY: max height for modes
  94. *
  95. * LOCKING:
  96. * Caller must hold mode config lock.
  97. *
  98. * Based on the helper callbacks implemented by @connector try to detect all
  99. * valid modes. Modes will first be added to the connector's probed_modes list,
  100. * then culled (based on validity and the @maxX, @maxY parameters) and put into
  101. * the normal modes list.
  102. *
  103. * Intended to be use as a generic implementation of the ->fill_modes()
  104. * @connector vfunc for drivers that use the crtc helpers for output mode
  105. * filtering and detection.
  106. *
  107. * RETURNS:
  108. * Number of modes found on @connector.
  109. */
  110. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  111. uint32_t maxX, uint32_t maxY)
  112. {
  113. struct drm_device *dev = connector->dev;
  114. struct drm_display_mode *mode;
  115. struct drm_connector_helper_funcs *connector_funcs =
  116. connector->helper_private;
  117. int count = 0;
  118. int mode_flags = 0;
  119. bool verbose_prune = true;
  120. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  121. drm_get_connector_name(connector));
  122. /* set all modes to the unverified state */
  123. list_for_each_entry(mode, &connector->modes, head)
  124. mode->status = MODE_UNVERIFIED;
  125. if (connector->force) {
  126. if (connector->force == DRM_FORCE_ON)
  127. connector->status = connector_status_connected;
  128. else
  129. connector->status = connector_status_disconnected;
  130. if (connector->funcs->force)
  131. connector->funcs->force(connector);
  132. } else {
  133. connector->status = connector->funcs->detect(connector, true);
  134. }
  135. /* Re-enable polling in case the global poll config changed. */
  136. if (drm_kms_helper_poll != dev->mode_config.poll_running)
  137. drm_kms_helper_poll_enable(dev);
  138. dev->mode_config.poll_running = drm_kms_helper_poll;
  139. if (connector->status == connector_status_disconnected) {
  140. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  141. connector->base.id, drm_get_connector_name(connector));
  142. drm_mode_connector_update_edid_property(connector, NULL);
  143. verbose_prune = false;
  144. goto prune;
  145. }
  146. #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
  147. count = drm_load_edid_firmware(connector);
  148. if (count == 0)
  149. #endif
  150. count = (*connector_funcs->get_modes)(connector);
  151. if (count == 0 && connector->status == connector_status_connected)
  152. count = drm_add_modes_noedid(connector, 1024, 768);
  153. if (count == 0)
  154. goto prune;
  155. drm_mode_connector_list_update(connector);
  156. if (maxX && maxY)
  157. drm_mode_validate_size(dev, &connector->modes, maxX,
  158. maxY, 0);
  159. if (connector->interlace_allowed)
  160. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  161. if (connector->doublescan_allowed)
  162. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  163. if (connector->stereo_allowed)
  164. mode_flags |= DRM_MODE_FLAG_3D_MASK;
  165. drm_mode_validate_flag(connector, mode_flags);
  166. list_for_each_entry(mode, &connector->modes, head) {
  167. if (mode->status == MODE_OK)
  168. mode->status = connector_funcs->mode_valid(connector,
  169. mode);
  170. }
  171. prune:
  172. drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
  173. if (list_empty(&connector->modes))
  174. return 0;
  175. list_for_each_entry(mode, &connector->modes, head)
  176. mode->vrefresh = drm_mode_vrefresh(mode);
  177. drm_mode_sort(&connector->modes);
  178. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  179. drm_get_connector_name(connector));
  180. list_for_each_entry(mode, &connector->modes, head) {
  181. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  182. drm_mode_debug_printmodeline(mode);
  183. }
  184. return count;
  185. }
  186. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  187. /**
  188. * drm_helper_encoder_in_use - check if a given encoder is in use
  189. * @encoder: encoder to check
  190. *
  191. * LOCKING:
  192. * Caller must hold mode config lock.
  193. *
  194. * Walk @encoders's DRM device's mode_config and see if it's in use.
  195. *
  196. * RETURNS:
  197. * True if @encoder is part of the mode_config, false otherwise.
  198. */
  199. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  200. {
  201. struct drm_connector *connector;
  202. struct drm_device *dev = encoder->dev;
  203. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  204. if (connector->encoder == encoder)
  205. return true;
  206. return false;
  207. }
  208. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  209. /**
  210. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  211. * @crtc: CRTC to check
  212. *
  213. * LOCKING:
  214. * Caller must hold mode config lock.
  215. *
  216. * Walk @crtc's DRM device's mode_config and see if it's in use.
  217. *
  218. * RETURNS:
  219. * True if @crtc is part of the mode_config, false otherwise.
  220. */
  221. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  222. {
  223. struct drm_encoder *encoder;
  224. struct drm_device *dev = crtc->dev;
  225. /* FIXME: Locking around list access? */
  226. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  227. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  228. return true;
  229. return false;
  230. }
  231. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  232. static void
  233. drm_encoder_disable(struct drm_encoder *encoder)
  234. {
  235. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  236. if (encoder->bridge)
  237. encoder->bridge->funcs->disable(encoder->bridge);
  238. if (encoder_funcs->disable)
  239. (*encoder_funcs->disable)(encoder);
  240. else
  241. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  242. if (encoder->bridge)
  243. encoder->bridge->funcs->post_disable(encoder->bridge);
  244. }
  245. /**
  246. * drm_helper_disable_unused_functions - disable unused objects
  247. * @dev: DRM device
  248. *
  249. * LOCKING:
  250. * Caller must hold mode config lock.
  251. *
  252. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  253. * by calling its dpms function, which should power it off.
  254. */
  255. void drm_helper_disable_unused_functions(struct drm_device *dev)
  256. {
  257. struct drm_encoder *encoder;
  258. struct drm_connector *connector;
  259. struct drm_crtc *crtc;
  260. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  261. if (!connector->encoder)
  262. continue;
  263. if (connector->status == connector_status_disconnected)
  264. connector->encoder = NULL;
  265. }
  266. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  267. if (!drm_helper_encoder_in_use(encoder)) {
  268. drm_encoder_disable(encoder);
  269. /* disconnector encoder from any connector */
  270. encoder->crtc = NULL;
  271. }
  272. }
  273. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  274. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  275. crtc->enabled = drm_helper_crtc_in_use(crtc);
  276. if (!crtc->enabled) {
  277. if (crtc_funcs->disable)
  278. (*crtc_funcs->disable)(crtc);
  279. else
  280. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  281. crtc->fb = NULL;
  282. }
  283. }
  284. }
  285. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  286. /*
  287. * Check the CRTC we're going to map each output to vs. its current
  288. * CRTC. If they don't match, we have to disable the output and the CRTC
  289. * since the driver will have to re-route things.
  290. */
  291. static void
  292. drm_crtc_prepare_encoders(struct drm_device *dev)
  293. {
  294. struct drm_encoder_helper_funcs *encoder_funcs;
  295. struct drm_encoder *encoder;
  296. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  297. encoder_funcs = encoder->helper_private;
  298. /* Disable unused encoders */
  299. if (encoder->crtc == NULL)
  300. drm_encoder_disable(encoder);
  301. /* Disable encoders whose CRTC is about to change */
  302. if (encoder_funcs->get_crtc &&
  303. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  304. drm_encoder_disable(encoder);
  305. }
  306. }
  307. /**
  308. * drm_crtc_helper_set_mode - internal helper to set a mode
  309. * @crtc: CRTC to program
  310. * @mode: mode to use
  311. * @x: horizontal offset into the surface
  312. * @y: vertical offset into the surface
  313. * @old_fb: old framebuffer, for cleanup
  314. *
  315. * LOCKING:
  316. * Caller must hold mode config lock.
  317. *
  318. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  319. * to fixup or reject the mode prior to trying to set it. This is an internal
  320. * helper that drivers could e.g. use to update properties that require the
  321. * entire output pipe to be disabled and re-enabled in a new configuration. For
  322. * example for changing whether audio is enabled on a hdmi link or for changing
  323. * panel fitter or dither attributes. It is also called by the
  324. * drm_crtc_helper_set_config() helper function to drive the mode setting
  325. * sequence.
  326. *
  327. * RETURNS:
  328. * True if the mode was set successfully, or false otherwise.
  329. */
  330. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  331. struct drm_display_mode *mode,
  332. int x, int y,
  333. struct drm_framebuffer *old_fb)
  334. {
  335. struct drm_device *dev = crtc->dev;
  336. struct drm_display_mode *adjusted_mode, saved_mode;
  337. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  338. struct drm_encoder_helper_funcs *encoder_funcs;
  339. int saved_x, saved_y;
  340. bool saved_enabled;
  341. struct drm_encoder *encoder;
  342. bool ret = true;
  343. saved_enabled = crtc->enabled;
  344. crtc->enabled = drm_helper_crtc_in_use(crtc);
  345. if (!crtc->enabled)
  346. return true;
  347. adjusted_mode = drm_mode_duplicate(dev, mode);
  348. if (!adjusted_mode) {
  349. crtc->enabled = saved_enabled;
  350. return false;
  351. }
  352. saved_mode = crtc->mode;
  353. saved_x = crtc->x;
  354. saved_y = crtc->y;
  355. /* Update crtc values up front so the driver can rely on them for mode
  356. * setting.
  357. */
  358. crtc->mode = *mode;
  359. crtc->x = x;
  360. crtc->y = y;
  361. /* Pass our mode to the connectors and the CRTC to give them a chance to
  362. * adjust it according to limitations or connector properties, and also
  363. * a chance to reject the mode entirely.
  364. */
  365. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  366. if (encoder->crtc != crtc)
  367. continue;
  368. if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
  369. ret = encoder->bridge->funcs->mode_fixup(
  370. encoder->bridge, mode, adjusted_mode);
  371. if (!ret) {
  372. DRM_DEBUG_KMS("Bridge fixup failed\n");
  373. goto done;
  374. }
  375. }
  376. encoder_funcs = encoder->helper_private;
  377. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  378. adjusted_mode))) {
  379. DRM_DEBUG_KMS("Encoder fixup failed\n");
  380. goto done;
  381. }
  382. }
  383. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  384. DRM_DEBUG_KMS("CRTC fixup failed\n");
  385. goto done;
  386. }
  387. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  388. /* Prepare the encoders and CRTCs before setting the mode. */
  389. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  390. if (encoder->crtc != crtc)
  391. continue;
  392. if (encoder->bridge)
  393. encoder->bridge->funcs->disable(encoder->bridge);
  394. encoder_funcs = encoder->helper_private;
  395. /* Disable the encoders as the first thing we do. */
  396. encoder_funcs->prepare(encoder);
  397. if (encoder->bridge)
  398. encoder->bridge->funcs->post_disable(encoder->bridge);
  399. }
  400. drm_crtc_prepare_encoders(dev);
  401. crtc_funcs->prepare(crtc);
  402. /* Set up the DPLL and any encoders state that needs to adjust or depend
  403. * on the DPLL.
  404. */
  405. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  406. if (!ret)
  407. goto done;
  408. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  409. if (encoder->crtc != crtc)
  410. continue;
  411. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  412. encoder->base.id, drm_get_encoder_name(encoder),
  413. mode->base.id, mode->name);
  414. encoder_funcs = encoder->helper_private;
  415. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  416. if (encoder->bridge && encoder->bridge->funcs->mode_set)
  417. encoder->bridge->funcs->mode_set(encoder->bridge, mode,
  418. adjusted_mode);
  419. }
  420. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  421. crtc_funcs->commit(crtc);
  422. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  423. if (encoder->crtc != crtc)
  424. continue;
  425. if (encoder->bridge)
  426. encoder->bridge->funcs->pre_enable(encoder->bridge);
  427. encoder_funcs = encoder->helper_private;
  428. encoder_funcs->commit(encoder);
  429. if (encoder->bridge)
  430. encoder->bridge->funcs->enable(encoder->bridge);
  431. }
  432. /* Store real post-adjustment hardware mode. */
  433. crtc->hwmode = *adjusted_mode;
  434. /* Calculate and store various constants which
  435. * are later needed by vblank and swap-completion
  436. * timestamping. They are derived from true hwmode.
  437. */
  438. drm_calc_timestamping_constants(crtc, &crtc->hwmode);
  439. /* FIXME: add subpixel order */
  440. done:
  441. drm_mode_destroy(dev, adjusted_mode);
  442. if (!ret) {
  443. crtc->enabled = saved_enabled;
  444. crtc->mode = saved_mode;
  445. crtc->x = saved_x;
  446. crtc->y = saved_y;
  447. }
  448. return ret;
  449. }
  450. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  451. static int
  452. drm_crtc_helper_disable(struct drm_crtc *crtc)
  453. {
  454. struct drm_device *dev = crtc->dev;
  455. struct drm_connector *connector;
  456. struct drm_encoder *encoder;
  457. /* Decouple all encoders and their attached connectors from this crtc */
  458. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  459. if (encoder->crtc != crtc)
  460. continue;
  461. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  462. if (connector->encoder != encoder)
  463. continue;
  464. connector->encoder = NULL;
  465. /*
  466. * drm_helper_disable_unused_functions() ought to be
  467. * doing this, but since we've decoupled the encoder
  468. * from the connector above, the required connection
  469. * between them is henceforth no longer available.
  470. */
  471. connector->dpms = DRM_MODE_DPMS_OFF;
  472. }
  473. }
  474. drm_helper_disable_unused_functions(dev);
  475. return 0;
  476. }
  477. /**
  478. * drm_crtc_helper_set_config - set a new config from userspace
  479. * @set: mode set configuration
  480. *
  481. * LOCKING:
  482. * Caller must hold mode config lock.
  483. *
  484. * Setup a new configuration, provided by the upper layers (either an ioctl call
  485. * from userspace or internally e.g. from the fbdev support code) in @set, and
  486. * enable it. This is the main helper functions for drivers that implement
  487. * kernel mode setting with the crtc helper functions and the assorted
  488. * ->prepare(), ->modeset() and ->commit() helper callbacks.
  489. *
  490. * RETURNS:
  491. * Returns 0 on success, -ERRNO on failure.
  492. */
  493. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  494. {
  495. struct drm_device *dev;
  496. struct drm_crtc *new_crtc;
  497. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  498. bool mode_changed = false; /* if true do a full mode set */
  499. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  500. struct drm_connector *save_connectors, *connector;
  501. int count = 0, ro, fail = 0;
  502. struct drm_crtc_helper_funcs *crtc_funcs;
  503. struct drm_mode_set save_set;
  504. int ret;
  505. int i;
  506. DRM_DEBUG_KMS("\n");
  507. BUG_ON(!set);
  508. BUG_ON(!set->crtc);
  509. BUG_ON(!set->crtc->helper_private);
  510. /* Enforce sane interface api - has been abused by the fb helper. */
  511. BUG_ON(!set->mode && set->fb);
  512. BUG_ON(set->fb && set->num_connectors == 0);
  513. crtc_funcs = set->crtc->helper_private;
  514. if (!set->mode)
  515. set->fb = NULL;
  516. if (set->fb) {
  517. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  518. set->crtc->base.id, set->fb->base.id,
  519. (int)set->num_connectors, set->x, set->y);
  520. } else {
  521. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  522. return drm_crtc_helper_disable(set->crtc);
  523. }
  524. dev = set->crtc->dev;
  525. /*
  526. * Allocate space for the backup of all (non-pointer) encoder and
  527. * connector data.
  528. */
  529. save_encoders = kzalloc(dev->mode_config.num_encoder *
  530. sizeof(struct drm_encoder), GFP_KERNEL);
  531. if (!save_encoders)
  532. return -ENOMEM;
  533. save_connectors = kzalloc(dev->mode_config.num_connector *
  534. sizeof(struct drm_connector), GFP_KERNEL);
  535. if (!save_connectors) {
  536. kfree(save_encoders);
  537. return -ENOMEM;
  538. }
  539. /*
  540. * Copy data. Note that driver private data is not affected.
  541. * Should anything bad happen only the expected state is
  542. * restored, not the drivers personal bookkeeping.
  543. */
  544. count = 0;
  545. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  546. save_encoders[count++] = *encoder;
  547. }
  548. count = 0;
  549. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  550. save_connectors[count++] = *connector;
  551. }
  552. save_set.crtc = set->crtc;
  553. save_set.mode = &set->crtc->mode;
  554. save_set.x = set->crtc->x;
  555. save_set.y = set->crtc->y;
  556. save_set.fb = set->crtc->fb;
  557. /* We should be able to check here if the fb has the same properties
  558. * and then just flip_or_move it */
  559. if (set->crtc->fb != set->fb) {
  560. /* If we have no fb then treat it as a full mode set */
  561. if (set->crtc->fb == NULL) {
  562. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  563. mode_changed = true;
  564. } else if (set->fb == NULL) {
  565. mode_changed = true;
  566. } else if (set->fb->pixel_format !=
  567. set->crtc->fb->pixel_format) {
  568. mode_changed = true;
  569. } else
  570. fb_changed = true;
  571. }
  572. if (set->x != set->crtc->x || set->y != set->crtc->y)
  573. fb_changed = true;
  574. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  575. DRM_DEBUG_KMS("modes are different, full mode set\n");
  576. drm_mode_debug_printmodeline(&set->crtc->mode);
  577. drm_mode_debug_printmodeline(set->mode);
  578. mode_changed = true;
  579. }
  580. /* a) traverse passed in connector list and get encoders for them */
  581. count = 0;
  582. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  583. struct drm_connector_helper_funcs *connector_funcs =
  584. connector->helper_private;
  585. new_encoder = connector->encoder;
  586. for (ro = 0; ro < set->num_connectors; ro++) {
  587. if (set->connectors[ro] == connector) {
  588. new_encoder = connector_funcs->best_encoder(connector);
  589. /* if we can't get an encoder for a connector
  590. we are setting now - then fail */
  591. if (new_encoder == NULL)
  592. /* don't break so fail path works correct */
  593. fail = 1;
  594. break;
  595. if (connector->dpms != DRM_MODE_DPMS_ON) {
  596. DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
  597. mode_changed = true;
  598. }
  599. }
  600. }
  601. if (new_encoder != connector->encoder) {
  602. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  603. mode_changed = true;
  604. /* If the encoder is reused for another connector, then
  605. * the appropriate crtc will be set later.
  606. */
  607. if (connector->encoder)
  608. connector->encoder->crtc = NULL;
  609. connector->encoder = new_encoder;
  610. }
  611. }
  612. if (fail) {
  613. ret = -EINVAL;
  614. goto fail;
  615. }
  616. count = 0;
  617. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  618. if (!connector->encoder)
  619. continue;
  620. if (connector->encoder->crtc == set->crtc)
  621. new_crtc = NULL;
  622. else
  623. new_crtc = connector->encoder->crtc;
  624. for (ro = 0; ro < set->num_connectors; ro++) {
  625. if (set->connectors[ro] == connector)
  626. new_crtc = set->crtc;
  627. }
  628. /* Make sure the new CRTC will work with the encoder */
  629. if (new_crtc &&
  630. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  631. ret = -EINVAL;
  632. goto fail;
  633. }
  634. if (new_crtc != connector->encoder->crtc) {
  635. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  636. mode_changed = true;
  637. connector->encoder->crtc = new_crtc;
  638. }
  639. if (new_crtc) {
  640. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  641. connector->base.id, drm_get_connector_name(connector),
  642. new_crtc->base.id);
  643. } else {
  644. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  645. connector->base.id, drm_get_connector_name(connector));
  646. }
  647. }
  648. /* mode_set_base is not a required function */
  649. if (fb_changed && !crtc_funcs->mode_set_base)
  650. mode_changed = true;
  651. if (mode_changed) {
  652. if (drm_helper_crtc_in_use(set->crtc)) {
  653. DRM_DEBUG_KMS("attempting to set mode from"
  654. " userspace\n");
  655. drm_mode_debug_printmodeline(set->mode);
  656. set->crtc->fb = set->fb;
  657. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  658. set->x, set->y,
  659. save_set.fb)) {
  660. DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  661. set->crtc->base.id);
  662. set->crtc->fb = save_set.fb;
  663. ret = -EINVAL;
  664. goto fail;
  665. }
  666. DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  667. for (i = 0; i < set->num_connectors; i++) {
  668. DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  669. drm_get_connector_name(set->connectors[i]));
  670. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  671. }
  672. }
  673. drm_helper_disable_unused_functions(dev);
  674. } else if (fb_changed) {
  675. set->crtc->x = set->x;
  676. set->crtc->y = set->y;
  677. set->crtc->fb = set->fb;
  678. ret = crtc_funcs->mode_set_base(set->crtc,
  679. set->x, set->y, save_set.fb);
  680. if (ret != 0) {
  681. set->crtc->x = save_set.x;
  682. set->crtc->y = save_set.y;
  683. set->crtc->fb = save_set.fb;
  684. goto fail;
  685. }
  686. }
  687. kfree(save_connectors);
  688. kfree(save_encoders);
  689. return 0;
  690. fail:
  691. /* Restore all previous data. */
  692. count = 0;
  693. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  694. *encoder = save_encoders[count++];
  695. }
  696. count = 0;
  697. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  698. *connector = save_connectors[count++];
  699. }
  700. /* Try to restore the config */
  701. if (mode_changed &&
  702. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  703. save_set.y, save_set.fb))
  704. DRM_ERROR("failed to restore config after modeset failure\n");
  705. kfree(save_connectors);
  706. kfree(save_encoders);
  707. return ret;
  708. }
  709. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  710. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  711. {
  712. int dpms = DRM_MODE_DPMS_OFF;
  713. struct drm_connector *connector;
  714. struct drm_device *dev = encoder->dev;
  715. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  716. if (connector->encoder == encoder)
  717. if (connector->dpms < dpms)
  718. dpms = connector->dpms;
  719. return dpms;
  720. }
  721. /* Helper which handles bridge ordering around encoder dpms */
  722. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  723. {
  724. struct drm_bridge *bridge = encoder->bridge;
  725. struct drm_encoder_helper_funcs *encoder_funcs;
  726. if (bridge) {
  727. if (mode == DRM_MODE_DPMS_ON)
  728. bridge->funcs->pre_enable(bridge);
  729. else
  730. bridge->funcs->disable(bridge);
  731. }
  732. encoder_funcs = encoder->helper_private;
  733. if (encoder_funcs->dpms)
  734. encoder_funcs->dpms(encoder, mode);
  735. if (bridge) {
  736. if (mode == DRM_MODE_DPMS_ON)
  737. bridge->funcs->enable(bridge);
  738. else
  739. bridge->funcs->post_disable(bridge);
  740. }
  741. }
  742. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  743. {
  744. int dpms = DRM_MODE_DPMS_OFF;
  745. struct drm_connector *connector;
  746. struct drm_device *dev = crtc->dev;
  747. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  748. if (connector->encoder && connector->encoder->crtc == crtc)
  749. if (connector->dpms < dpms)
  750. dpms = connector->dpms;
  751. return dpms;
  752. }
  753. /**
  754. * drm_helper_connector_dpms() - connector dpms helper implementation
  755. * @connector: affected connector
  756. * @mode: DPMS mode
  757. *
  758. * This is the main helper function provided by the crtc helper framework for
  759. * implementing the DPMS connector attribute. It computes the new desired DPMS
  760. * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  761. * callback provided by the driver appropriately.
  762. */
  763. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  764. {
  765. struct drm_encoder *encoder = connector->encoder;
  766. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  767. int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  768. if (mode == connector->dpms)
  769. return;
  770. old_dpms = connector->dpms;
  771. connector->dpms = mode;
  772. if (encoder)
  773. encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  774. /* from off to on, do crtc then encoder */
  775. if (mode < old_dpms) {
  776. if (crtc) {
  777. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  778. if (crtc_funcs->dpms)
  779. (*crtc_funcs->dpms) (crtc,
  780. drm_helper_choose_crtc_dpms(crtc));
  781. }
  782. if (encoder)
  783. drm_helper_encoder_dpms(encoder, encoder_dpms);
  784. }
  785. /* from on to off, do encoder then crtc */
  786. if (mode > old_dpms) {
  787. if (encoder)
  788. drm_helper_encoder_dpms(encoder, encoder_dpms);
  789. if (crtc) {
  790. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  791. if (crtc_funcs->dpms)
  792. (*crtc_funcs->dpms) (crtc,
  793. drm_helper_choose_crtc_dpms(crtc));
  794. }
  795. }
  796. return;
  797. }
  798. EXPORT_SYMBOL(drm_helper_connector_dpms);
  799. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  800. struct drm_mode_fb_cmd2 *mode_cmd)
  801. {
  802. int i;
  803. fb->width = mode_cmd->width;
  804. fb->height = mode_cmd->height;
  805. for (i = 0; i < 4; i++) {
  806. fb->pitches[i] = mode_cmd->pitches[i];
  807. fb->offsets[i] = mode_cmd->offsets[i];
  808. }
  809. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  810. &fb->bits_per_pixel);
  811. fb->pixel_format = mode_cmd->pixel_format;
  812. return 0;
  813. }
  814. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  815. int drm_helper_resume_force_mode(struct drm_device *dev)
  816. {
  817. struct drm_crtc *crtc;
  818. struct drm_encoder *encoder;
  819. struct drm_crtc_helper_funcs *crtc_funcs;
  820. int ret, encoder_dpms;
  821. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  822. if (!crtc->enabled)
  823. continue;
  824. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  825. crtc->x, crtc->y, crtc->fb);
  826. if (ret == false)
  827. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  828. /* Turn off outputs that were already powered off */
  829. if (drm_helper_choose_crtc_dpms(crtc)) {
  830. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  831. if(encoder->crtc != crtc)
  832. continue;
  833. encoder_dpms = drm_helper_choose_encoder_dpms(
  834. encoder);
  835. drm_helper_encoder_dpms(encoder, encoder_dpms);
  836. }
  837. crtc_funcs = crtc->helper_private;
  838. if (crtc_funcs->dpms)
  839. (*crtc_funcs->dpms) (crtc,
  840. drm_helper_choose_crtc_dpms(crtc));
  841. }
  842. }
  843. /* disable the unused connectors while restoring the modesetting */
  844. drm_helper_disable_unused_functions(dev);
  845. return 0;
  846. }
  847. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  848. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  849. {
  850. /* send a uevent + call fbdev */
  851. drm_sysfs_hotplug_event(dev);
  852. if (dev->mode_config.funcs->output_poll_changed)
  853. dev->mode_config.funcs->output_poll_changed(dev);
  854. }
  855. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  856. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  857. static void output_poll_execute(struct work_struct *work)
  858. {
  859. struct delayed_work *delayed_work = to_delayed_work(work);
  860. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  861. struct drm_connector *connector;
  862. enum drm_connector_status old_status;
  863. bool repoll = false, changed = false;
  864. if (!drm_kms_helper_poll)
  865. return;
  866. mutex_lock(&dev->mode_config.mutex);
  867. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  868. /* Ignore forced connectors. */
  869. if (connector->force)
  870. continue;
  871. /* Ignore HPD capable connectors and connectors where we don't
  872. * want any hotplug detection at all for polling. */
  873. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  874. continue;
  875. repoll = true;
  876. old_status = connector->status;
  877. /* if we are connected and don't want to poll for disconnect
  878. skip it */
  879. if (old_status == connector_status_connected &&
  880. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  881. continue;
  882. connector->status = connector->funcs->detect(connector, false);
  883. if (old_status != connector->status) {
  884. const char *old, *new;
  885. old = drm_get_connector_status_name(old_status);
  886. new = drm_get_connector_status_name(connector->status);
  887. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  888. "status updated from %s to %s\n",
  889. connector->base.id,
  890. drm_get_connector_name(connector),
  891. old, new);
  892. changed = true;
  893. }
  894. }
  895. mutex_unlock(&dev->mode_config.mutex);
  896. if (changed)
  897. drm_kms_helper_hotplug_event(dev);
  898. if (repoll)
  899. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  900. }
  901. void drm_kms_helper_poll_disable(struct drm_device *dev)
  902. {
  903. if (!dev->mode_config.poll_enabled)
  904. return;
  905. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  906. }
  907. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  908. void drm_kms_helper_poll_enable(struct drm_device *dev)
  909. {
  910. bool poll = false;
  911. struct drm_connector *connector;
  912. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  913. return;
  914. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  915. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  916. DRM_CONNECTOR_POLL_DISCONNECT))
  917. poll = true;
  918. }
  919. if (poll)
  920. schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  921. }
  922. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  923. void drm_kms_helper_poll_init(struct drm_device *dev)
  924. {
  925. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  926. dev->mode_config.poll_enabled = true;
  927. drm_kms_helper_poll_enable(dev);
  928. }
  929. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  930. void drm_kms_helper_poll_fini(struct drm_device *dev)
  931. {
  932. drm_kms_helper_poll_disable(dev);
  933. }
  934. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  935. bool drm_helper_hpd_irq_event(struct drm_device *dev)
  936. {
  937. struct drm_connector *connector;
  938. enum drm_connector_status old_status;
  939. bool changed = false;
  940. if (!dev->mode_config.poll_enabled)
  941. return false;
  942. mutex_lock(&dev->mode_config.mutex);
  943. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  944. /* Only handle HPD capable connectors. */
  945. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  946. continue;
  947. old_status = connector->status;
  948. connector->status = connector->funcs->detect(connector, false);
  949. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  950. connector->base.id,
  951. drm_get_connector_name(connector),
  952. drm_get_connector_status_name(old_status),
  953. drm_get_connector_status_name(connector->status));
  954. if (old_status != connector->status)
  955. changed = true;
  956. }
  957. mutex_unlock(&dev->mode_config.mutex);
  958. if (changed)
  959. drm_kms_helper_hotplug_event(dev);
  960. return changed;
  961. }
  962. EXPORT_SYMBOL(drm_helper_hpd_irq_event);