drm_crtc_helper.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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/kernel.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/dynamic_debug.h>
  35. #include <drm/drm_atomic.h>
  36. #include <drm/drm_atomic_helper.h>
  37. #include <drm/drm_atomic_uapi.h>
  38. #include <drm/drm_bridge.h>
  39. #include <drm/drm_crtc.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_drv.h>
  42. #include <drm/drm_edid.h>
  43. #include <drm/drm_encoder.h>
  44. #include <drm/drm_fourcc.h>
  45. #include <drm/drm_framebuffer.h>
  46. #include <drm/drm_print.h>
  47. #include <drm/drm_vblank.h>
  48. #include "drm_crtc_helper_internal.h"
  49. DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
  50. "DRM_UT_CORE",
  51. "DRM_UT_DRIVER",
  52. "DRM_UT_KMS",
  53. "DRM_UT_PRIME",
  54. "DRM_UT_ATOMIC",
  55. "DRM_UT_VBL",
  56. "DRM_UT_STATE",
  57. "DRM_UT_LEASE",
  58. "DRM_UT_DP",
  59. "DRM_UT_DRMRES");
  60. /**
  61. * DOC: overview
  62. *
  63. * The CRTC modeset helper library provides a default set_config implementation
  64. * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
  65. * the same callbacks which drivers can use to e.g. restore the modeset
  66. * configuration on resume with drm_helper_resume_force_mode().
  67. *
  68. * Note that this helper library doesn't track the current power state of CRTCs
  69. * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
  70. * though the hardware is already in the desired state. This deficiency has been
  71. * fixed in the atomic helpers.
  72. *
  73. * The driver callbacks are mostly compatible with the atomic modeset helpers,
  74. * except for the handling of the primary plane: Atomic helpers require that the
  75. * primary plane is implemented as a real standalone plane and not directly tied
  76. * to the CRTC state. For easier transition this library provides functions to
  77. * implement the old semantics required by the CRTC helpers using the new plane
  78. * and atomic helper callbacks.
  79. *
  80. * Drivers are strongly urged to convert to the atomic helpers (by way of first
  81. * converting to the plane helpers). New drivers must not use these functions
  82. * but need to implement the atomic interface instead, potentially using the
  83. * atomic helpers for that.
  84. *
  85. * These legacy modeset helpers use the same function table structures as
  86. * all other modesetting helpers. See the documentation for struct
  87. * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
  88. * &drm_connector_helper_funcs.
  89. */
  90. /**
  91. * drm_helper_encoder_in_use - check if a given encoder is in use
  92. * @encoder: encoder to check
  93. *
  94. * Checks whether @encoder is with the current mode setting output configuration
  95. * in use by any connector. This doesn't mean that it is actually enabled since
  96. * the DPMS state is tracked separately.
  97. *
  98. * Returns:
  99. * True if @encoder is used, false otherwise.
  100. */
  101. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  102. {
  103. struct drm_connector *connector;
  104. struct drm_connector_list_iter conn_iter;
  105. struct drm_device *dev = encoder->dev;
  106. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  107. /*
  108. * We can expect this mutex to be locked if we are not panicking.
  109. * Locking is currently fubar in the panic handler.
  110. */
  111. if (!oops_in_progress) {
  112. drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
  113. drm_WARN_ON(dev, !drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  114. }
  115. drm_connector_list_iter_begin(dev, &conn_iter);
  116. drm_for_each_connector_iter(connector, &conn_iter) {
  117. if (connector->encoder == encoder) {
  118. drm_connector_list_iter_end(&conn_iter);
  119. return true;
  120. }
  121. }
  122. drm_connector_list_iter_end(&conn_iter);
  123. return false;
  124. }
  125. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  126. /**
  127. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  128. * @crtc: CRTC to check
  129. *
  130. * Checks whether @crtc is with the current mode setting output configuration
  131. * in use by any connector. This doesn't mean that it is actually enabled since
  132. * the DPMS state is tracked separately.
  133. *
  134. * Returns:
  135. * True if @crtc is used, false otherwise.
  136. */
  137. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  138. {
  139. struct drm_encoder *encoder;
  140. struct drm_device *dev = crtc->dev;
  141. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  142. /*
  143. * We can expect this mutex to be locked if we are not panicking.
  144. * Locking is currently fubar in the panic handler.
  145. */
  146. if (!oops_in_progress)
  147. drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
  148. drm_for_each_encoder(encoder, dev)
  149. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  150. return true;
  151. return false;
  152. }
  153. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  154. static void
  155. drm_encoder_disable(struct drm_encoder *encoder)
  156. {
  157. const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  158. if (!encoder_funcs)
  159. return;
  160. if (encoder_funcs->disable)
  161. (*encoder_funcs->disable)(encoder);
  162. else if (encoder_funcs->dpms)
  163. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  164. }
  165. static void __drm_helper_disable_unused_functions(struct drm_device *dev)
  166. {
  167. struct drm_encoder *encoder;
  168. struct drm_crtc *crtc;
  169. drm_warn_on_modeset_not_all_locked(dev);
  170. drm_for_each_encoder(encoder, dev) {
  171. if (!drm_helper_encoder_in_use(encoder)) {
  172. drm_encoder_disable(encoder);
  173. /* disconnect encoder from any connector */
  174. encoder->crtc = NULL;
  175. }
  176. }
  177. drm_for_each_crtc(crtc, dev) {
  178. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  179. crtc->enabled = drm_helper_crtc_in_use(crtc);
  180. if (!crtc->enabled) {
  181. if (crtc_funcs->disable)
  182. (*crtc_funcs->disable)(crtc);
  183. else
  184. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  185. crtc->primary->fb = NULL;
  186. }
  187. }
  188. }
  189. /**
  190. * drm_helper_disable_unused_functions - disable unused objects
  191. * @dev: DRM device
  192. *
  193. * This function walks through the entire mode setting configuration of @dev. It
  194. * will remove any CRTC links of unused encoders and encoder links of
  195. * disconnected connectors. Then it will disable all unused encoders and CRTCs
  196. * either by calling their disable callback if available or by calling their
  197. * dpms callback with DRM_MODE_DPMS_OFF.
  198. *
  199. * NOTE:
  200. *
  201. * This function is part of the legacy modeset helper library and will cause
  202. * major confusion with atomic drivers. This is because atomic helpers guarantee
  203. * to never call ->disable() hooks on a disabled function, or ->enable() hooks
  204. * on an enabled functions. drm_helper_disable_unused_functions() on the other
  205. * hand throws such guarantees into the wind and calls disable hooks
  206. * unconditionally on unused functions.
  207. */
  208. void drm_helper_disable_unused_functions(struct drm_device *dev)
  209. {
  210. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  211. drm_modeset_lock_all(dev);
  212. __drm_helper_disable_unused_functions(dev);
  213. drm_modeset_unlock_all(dev);
  214. }
  215. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  216. /*
  217. * Check the CRTC we're going to map each output to vs. its current
  218. * CRTC. If they don't match, we have to disable the output and the CRTC
  219. * since the driver will have to re-route things.
  220. */
  221. static void
  222. drm_crtc_prepare_encoders(struct drm_device *dev)
  223. {
  224. const struct drm_encoder_helper_funcs *encoder_funcs;
  225. struct drm_encoder *encoder;
  226. drm_for_each_encoder(encoder, dev) {
  227. encoder_funcs = encoder->helper_private;
  228. if (!encoder_funcs)
  229. continue;
  230. /* Disable unused encoders */
  231. if (encoder->crtc == NULL)
  232. drm_encoder_disable(encoder);
  233. }
  234. }
  235. /**
  236. * drm_crtc_helper_set_mode - internal helper to set a mode
  237. * @crtc: CRTC to program
  238. * @mode: mode to use
  239. * @x: horizontal offset into the surface
  240. * @y: vertical offset into the surface
  241. * @old_fb: old framebuffer, for cleanup
  242. *
  243. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  244. * to fixup or reject the mode prior to trying to set it. This is an internal
  245. * helper that drivers could e.g. use to update properties that require the
  246. * entire output pipe to be disabled and re-enabled in a new configuration. For
  247. * example for changing whether audio is enabled on a hdmi link or for changing
  248. * panel fitter or dither attributes. It is also called by the
  249. * drm_crtc_helper_set_config() helper function to drive the mode setting
  250. * sequence.
  251. *
  252. * Returns:
  253. * True if the mode was set successfully, false otherwise.
  254. */
  255. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  256. struct drm_display_mode *mode,
  257. int x, int y,
  258. struct drm_framebuffer *old_fb)
  259. {
  260. struct drm_device *dev = crtc->dev;
  261. struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
  262. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  263. const struct drm_encoder_helper_funcs *encoder_funcs;
  264. int saved_x, saved_y;
  265. bool saved_enabled;
  266. struct drm_encoder *encoder;
  267. bool ret = true;
  268. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  269. drm_warn_on_modeset_not_all_locked(dev);
  270. saved_enabled = crtc->enabled;
  271. crtc->enabled = drm_helper_crtc_in_use(crtc);
  272. if (!crtc->enabled)
  273. return true;
  274. adjusted_mode = drm_mode_duplicate(dev, mode);
  275. if (!adjusted_mode) {
  276. crtc->enabled = saved_enabled;
  277. return false;
  278. }
  279. drm_mode_init(&saved_mode, &crtc->mode);
  280. drm_mode_init(&saved_hwmode, &crtc->hwmode);
  281. saved_x = crtc->x;
  282. saved_y = crtc->y;
  283. /* Update crtc values up front so the driver can rely on them for mode
  284. * setting.
  285. */
  286. drm_mode_copy(&crtc->mode, mode);
  287. crtc->x = x;
  288. crtc->y = y;
  289. /* Pass our mode to the connectors and the CRTC to give them a chance to
  290. * adjust it according to limitations or connector properties, and also
  291. * a chance to reject the mode entirely.
  292. */
  293. drm_for_each_encoder(encoder, dev) {
  294. if (encoder->crtc != crtc)
  295. continue;
  296. encoder_funcs = encoder->helper_private;
  297. if (!encoder_funcs)
  298. continue;
  299. encoder_funcs = encoder->helper_private;
  300. if (encoder_funcs->mode_fixup) {
  301. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  302. adjusted_mode))) {
  303. drm_dbg_kms(dev, "[ENCODER:%d:%s] mode fixup failed\n",
  304. encoder->base.id, encoder->name);
  305. goto done;
  306. }
  307. }
  308. }
  309. if (crtc_funcs->mode_fixup) {
  310. if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
  311. adjusted_mode))) {
  312. drm_dbg_kms(dev, "[CRTC:%d:%s] mode fixup failed\n",
  313. crtc->base.id, crtc->name);
  314. goto done;
  315. }
  316. }
  317. drm_dbg_kms(dev, "[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  318. drm_mode_copy(&crtc->hwmode, adjusted_mode);
  319. /* Prepare the encoders and CRTCs before setting the mode. */
  320. drm_for_each_encoder(encoder, dev) {
  321. if (encoder->crtc != crtc)
  322. continue;
  323. encoder_funcs = encoder->helper_private;
  324. if (!encoder_funcs)
  325. continue;
  326. /* Disable the encoders as the first thing we do. */
  327. if (encoder_funcs->prepare)
  328. encoder_funcs->prepare(encoder);
  329. }
  330. drm_crtc_prepare_encoders(dev);
  331. crtc_funcs->prepare(crtc);
  332. /* Set up the DPLL and any encoders state that needs to adjust or depend
  333. * on the DPLL.
  334. */
  335. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  336. if (!ret)
  337. goto done;
  338. drm_for_each_encoder(encoder, dev) {
  339. if (encoder->crtc != crtc)
  340. continue;
  341. encoder_funcs = encoder->helper_private;
  342. if (!encoder_funcs)
  343. continue;
  344. drm_dbg_kms(dev, "[ENCODER:%d:%s] set [MODE:%s]\n",
  345. encoder->base.id, encoder->name, mode->name);
  346. if (encoder_funcs->mode_set)
  347. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  348. }
  349. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  350. crtc_funcs->commit(crtc);
  351. drm_for_each_encoder(encoder, dev) {
  352. if (encoder->crtc != crtc)
  353. continue;
  354. encoder_funcs = encoder->helper_private;
  355. if (!encoder_funcs)
  356. continue;
  357. if (encoder_funcs->commit)
  358. encoder_funcs->commit(encoder);
  359. }
  360. /* Calculate and store various constants which
  361. * are later needed by vblank and swap-completion
  362. * timestamping. They are derived from true hwmode.
  363. */
  364. drm_calc_timestamping_constants(crtc, &crtc->hwmode);
  365. /* FIXME: add subpixel order */
  366. done:
  367. drm_mode_destroy(dev, adjusted_mode);
  368. if (!ret) {
  369. crtc->enabled = saved_enabled;
  370. drm_mode_copy(&crtc->mode, &saved_mode);
  371. drm_mode_copy(&crtc->hwmode, &saved_hwmode);
  372. crtc->x = saved_x;
  373. crtc->y = saved_y;
  374. }
  375. return ret;
  376. }
  377. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  378. /**
  379. * drm_crtc_helper_atomic_check() - Helper to check CRTC atomic-state
  380. * @crtc: CRTC to check
  381. * @state: atomic state object
  382. *
  383. * Provides a default CRTC-state check handler for CRTCs that only have
  384. * one primary plane attached to it. This is often the case for the CRTC
  385. * of simple framebuffers.
  386. *
  387. * RETURNS:
  388. * Zero on success, or an errno code otherwise.
  389. */
  390. int drm_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
  391. {
  392. struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  393. if (!new_crtc_state->enable)
  394. return 0;
  395. return drm_atomic_helper_check_crtc_primary_plane(new_crtc_state);
  396. }
  397. EXPORT_SYMBOL(drm_crtc_helper_atomic_check);
  398. static void
  399. drm_crtc_helper_disable(struct drm_crtc *crtc)
  400. {
  401. struct drm_device *dev = crtc->dev;
  402. struct drm_connector *connector;
  403. struct drm_encoder *encoder;
  404. /* Decouple all encoders and their attached connectors from this crtc */
  405. drm_for_each_encoder(encoder, dev) {
  406. struct drm_connector_list_iter conn_iter;
  407. if (encoder->crtc != crtc)
  408. continue;
  409. drm_connector_list_iter_begin(dev, &conn_iter);
  410. drm_for_each_connector_iter(connector, &conn_iter) {
  411. if (connector->encoder != encoder)
  412. continue;
  413. connector->encoder = NULL;
  414. /*
  415. * drm_helper_disable_unused_functions() ought to be
  416. * doing this, but since we've decoupled the encoder
  417. * from the connector above, the required connection
  418. * between them is henceforth no longer available.
  419. */
  420. connector->dpms = DRM_MODE_DPMS_OFF;
  421. /* we keep a reference while the encoder is bound */
  422. drm_connector_put(connector);
  423. }
  424. drm_connector_list_iter_end(&conn_iter);
  425. }
  426. __drm_helper_disable_unused_functions(dev);
  427. }
  428. /*
  429. * For connectors that support multiple encoders, either the
  430. * .atomic_best_encoder() or .best_encoder() operation must be implemented.
  431. */
  432. struct drm_encoder *
  433. drm_connector_get_single_encoder(struct drm_connector *connector)
  434. {
  435. struct drm_encoder *encoder;
  436. drm_WARN_ON(connector->dev, hweight32(connector->possible_encoders) > 1);
  437. drm_connector_for_each_possible_encoder(connector, encoder)
  438. return encoder;
  439. return NULL;
  440. }
  441. /**
  442. * drm_crtc_helper_set_config - set a new config from userspace
  443. * @set: mode set configuration
  444. * @ctx: lock acquire context, not used here
  445. *
  446. * The drm_crtc_helper_set_config() helper function implements the of
  447. * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
  448. * helpers.
  449. *
  450. * It first tries to locate the best encoder for each connector by calling the
  451. * connector @drm_connector_helper_funcs.best_encoder helper operation.
  452. *
  453. * After locating the appropriate encoders, the helper function will call the
  454. * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
  455. * or reject it completely in which case an error will be returned to the
  456. * application. If the new configuration after mode adjustment is identical to
  457. * the current configuration the helper function will return without performing
  458. * any other operation.
  459. *
  460. * If the adjusted mode is identical to the current mode but changes to the
  461. * frame buffer need to be applied, the drm_crtc_helper_set_config() function
  462. * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
  463. *
  464. * If the adjusted mode differs from the current mode, or if the
  465. * ->mode_set_base() helper operation is not provided, the helper function
  466. * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
  467. * and ->commit() CRTC and encoder helper operations, in that order.
  468. * Alternatively it can also use the dpms and disable helper operations. For
  469. * details see &struct drm_crtc_helper_funcs and struct
  470. * &drm_encoder_helper_funcs.
  471. *
  472. * This function is deprecated. New drivers must implement atomic modeset
  473. * support, for which this function is unsuitable. Instead drivers should use
  474. * drm_atomic_helper_set_config().
  475. *
  476. * Returns:
  477. * Returns 0 on success, negative errno numbers on failure.
  478. */
  479. int drm_crtc_helper_set_config(struct drm_mode_set *set,
  480. struct drm_modeset_acquire_ctx *ctx)
  481. {
  482. struct drm_device *dev;
  483. struct drm_crtc **save_encoder_crtcs, *new_crtc;
  484. struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
  485. bool mode_changed = false; /* if true do a full mode set */
  486. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  487. struct drm_connector *connector;
  488. struct drm_connector_list_iter conn_iter;
  489. int count = 0, ro, fail = 0;
  490. const struct drm_crtc_helper_funcs *crtc_funcs;
  491. struct drm_mode_set save_set;
  492. int ret;
  493. int i;
  494. BUG_ON(!set);
  495. BUG_ON(!set->crtc);
  496. BUG_ON(!set->crtc->helper_private);
  497. /* Enforce sane interface api - has been abused by the fb helper. */
  498. BUG_ON(!set->mode && set->fb);
  499. BUG_ON(set->fb && set->num_connectors == 0);
  500. crtc_funcs = set->crtc->helper_private;
  501. dev = set->crtc->dev;
  502. drm_dbg_kms(dev, "\n");
  503. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  504. if (!set->mode)
  505. set->fb = NULL;
  506. if (set->fb) {
  507. drm_dbg_kms(dev, "[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  508. set->crtc->base.id, set->crtc->name,
  509. set->fb->base.id,
  510. (int)set->num_connectors, set->x, set->y);
  511. } else {
  512. drm_dbg_kms(dev, "[CRTC:%d:%s] [NOFB]\n",
  513. set->crtc->base.id, set->crtc->name);
  514. drm_crtc_helper_disable(set->crtc);
  515. return 0;
  516. }
  517. drm_warn_on_modeset_not_all_locked(dev);
  518. /*
  519. * Allocate space for the backup of all (non-pointer) encoder and
  520. * connector data.
  521. */
  522. save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
  523. sizeof(struct drm_crtc *), GFP_KERNEL);
  524. if (!save_encoder_crtcs)
  525. return -ENOMEM;
  526. save_connector_encoders = kcalloc(dev->mode_config.num_connector,
  527. sizeof(struct drm_encoder *), GFP_KERNEL);
  528. if (!save_connector_encoders) {
  529. kfree(save_encoder_crtcs);
  530. return -ENOMEM;
  531. }
  532. /*
  533. * Copy data. Note that driver private data is not affected.
  534. * Should anything bad happen only the expected state is
  535. * restored, not the drivers personal bookkeeping.
  536. */
  537. count = 0;
  538. drm_for_each_encoder(encoder, dev) {
  539. save_encoder_crtcs[count++] = encoder->crtc;
  540. }
  541. count = 0;
  542. drm_connector_list_iter_begin(dev, &conn_iter);
  543. drm_for_each_connector_iter(connector, &conn_iter)
  544. save_connector_encoders[count++] = connector->encoder;
  545. drm_connector_list_iter_end(&conn_iter);
  546. save_set.crtc = set->crtc;
  547. save_set.mode = &set->crtc->mode;
  548. save_set.x = set->crtc->x;
  549. save_set.y = set->crtc->y;
  550. save_set.fb = set->crtc->primary->fb;
  551. /* We should be able to check here if the fb has the same properties
  552. * and then just flip_or_move it */
  553. if (set->crtc->primary->fb != set->fb) {
  554. /* If we have no fb then treat it as a full mode set */
  555. if (set->crtc->primary->fb == NULL) {
  556. drm_dbg_kms(dev, "[CRTC:%d:%s] no fb, full mode set\n",
  557. set->crtc->base.id, set->crtc->name);
  558. mode_changed = true;
  559. } else if (set->fb->format != set->crtc->primary->fb->format) {
  560. mode_changed = true;
  561. } else
  562. fb_changed = true;
  563. }
  564. if (set->x != set->crtc->x || set->y != set->crtc->y)
  565. fb_changed = true;
  566. if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
  567. drm_dbg_kms(dev, "[CRTC:%d:%s] modes are different, full mode set:\n",
  568. set->crtc->base.id, set->crtc->name);
  569. drm_dbg_kms(dev, DRM_MODE_FMT "\n", DRM_MODE_ARG(&set->crtc->mode));
  570. drm_dbg_kms(dev, DRM_MODE_FMT "\n", DRM_MODE_ARG(set->mode));
  571. mode_changed = true;
  572. }
  573. /* take a reference on all unbound connectors in set, reuse the
  574. * already taken reference for bound connectors
  575. */
  576. for (ro = 0; ro < set->num_connectors; ro++) {
  577. if (set->connectors[ro]->encoder)
  578. continue;
  579. drm_connector_get(set->connectors[ro]);
  580. }
  581. /* a) traverse passed in connector list and get encoders for them */
  582. count = 0;
  583. drm_connector_list_iter_begin(dev, &conn_iter);
  584. drm_for_each_connector_iter(connector, &conn_iter) {
  585. const struct drm_connector_helper_funcs *connector_funcs =
  586. connector->helper_private;
  587. new_encoder = connector->encoder;
  588. for (ro = 0; ro < set->num_connectors; ro++) {
  589. if (set->connectors[ro] == connector) {
  590. if (connector_funcs->best_encoder)
  591. new_encoder = connector_funcs->best_encoder(connector);
  592. else
  593. new_encoder = drm_connector_get_single_encoder(connector);
  594. /* if we can't get an encoder for a connector
  595. we are setting now - then fail */
  596. if (new_encoder == NULL)
  597. /* don't break so fail path works correct */
  598. fail = 1;
  599. if (connector->dpms != DRM_MODE_DPMS_ON) {
  600. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] DPMS not on, full mode switch\n",
  601. connector->base.id, connector->name);
  602. mode_changed = true;
  603. }
  604. break;
  605. }
  606. }
  607. if (new_encoder != connector->encoder) {
  608. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  609. connector->base.id, connector->name);
  610. mode_changed = true;
  611. /* If the encoder is reused for another connector, then
  612. * the appropriate crtc will be set later.
  613. */
  614. if (connector->encoder)
  615. connector->encoder->crtc = NULL;
  616. connector->encoder = new_encoder;
  617. }
  618. }
  619. drm_connector_list_iter_end(&conn_iter);
  620. if (fail) {
  621. ret = -EINVAL;
  622. goto fail;
  623. }
  624. count = 0;
  625. drm_connector_list_iter_begin(dev, &conn_iter);
  626. drm_for_each_connector_iter(connector, &conn_iter) {
  627. if (!connector->encoder)
  628. continue;
  629. if (connector->encoder->crtc == set->crtc)
  630. new_crtc = NULL;
  631. else
  632. new_crtc = connector->encoder->crtc;
  633. for (ro = 0; ro < set->num_connectors; ro++) {
  634. if (set->connectors[ro] == connector)
  635. new_crtc = set->crtc;
  636. }
  637. /* Make sure the new CRTC will work with the encoder */
  638. if (new_crtc &&
  639. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  640. ret = -EINVAL;
  641. drm_connector_list_iter_end(&conn_iter);
  642. goto fail;
  643. }
  644. if (new_crtc != connector->encoder->crtc) {
  645. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] CRTC changed, full mode switch\n",
  646. connector->base.id, connector->name);
  647. mode_changed = true;
  648. connector->encoder->crtc = new_crtc;
  649. }
  650. if (new_crtc) {
  651. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
  652. connector->base.id, connector->name,
  653. new_crtc->base.id, new_crtc->name);
  654. } else {
  655. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] to [NOCRTC]\n",
  656. connector->base.id, connector->name);
  657. }
  658. }
  659. drm_connector_list_iter_end(&conn_iter);
  660. /* mode_set_base is not a required function */
  661. if (fb_changed && !crtc_funcs->mode_set_base)
  662. mode_changed = true;
  663. if (mode_changed) {
  664. if (drm_helper_crtc_in_use(set->crtc)) {
  665. drm_dbg_kms(dev, "[CRTC:%d:%s] attempting to set mode from userspace: " DRM_MODE_FMT "\n",
  666. set->crtc->base.id, set->crtc->name, DRM_MODE_ARG(set->mode));
  667. set->crtc->primary->fb = set->fb;
  668. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  669. set->x, set->y,
  670. save_set.fb)) {
  671. drm_err(dev, "[CRTC:%d:%s] failed to set mode\n",
  672. set->crtc->base.id, set->crtc->name);
  673. set->crtc->primary->fb = save_set.fb;
  674. ret = -EINVAL;
  675. goto fail;
  676. }
  677. drm_dbg_kms(dev, "[CRTC:%d:%s] Setting connector DPMS state to on\n",
  678. set->crtc->base.id, set->crtc->name);
  679. for (i = 0; i < set->num_connectors; i++) {
  680. drm_dbg_kms(dev, "\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  681. set->connectors[i]->name);
  682. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  683. }
  684. }
  685. __drm_helper_disable_unused_functions(dev);
  686. } else if (fb_changed) {
  687. set->crtc->x = set->x;
  688. set->crtc->y = set->y;
  689. set->crtc->primary->fb = set->fb;
  690. ret = crtc_funcs->mode_set_base(set->crtc,
  691. set->x, set->y, save_set.fb);
  692. if (ret != 0) {
  693. set->crtc->x = save_set.x;
  694. set->crtc->y = save_set.y;
  695. set->crtc->primary->fb = save_set.fb;
  696. goto fail;
  697. }
  698. }
  699. kfree(save_connector_encoders);
  700. kfree(save_encoder_crtcs);
  701. return 0;
  702. fail:
  703. /* Restore all previous data. */
  704. count = 0;
  705. drm_for_each_encoder(encoder, dev) {
  706. encoder->crtc = save_encoder_crtcs[count++];
  707. }
  708. count = 0;
  709. drm_connector_list_iter_begin(dev, &conn_iter);
  710. drm_for_each_connector_iter(connector, &conn_iter)
  711. connector->encoder = save_connector_encoders[count++];
  712. drm_connector_list_iter_end(&conn_iter);
  713. /* after fail drop reference on all unbound connectors in set, let
  714. * bound connectors keep their reference
  715. */
  716. for (ro = 0; ro < set->num_connectors; ro++) {
  717. if (set->connectors[ro]->encoder)
  718. continue;
  719. drm_connector_put(set->connectors[ro]);
  720. }
  721. /* Try to restore the config */
  722. if (mode_changed &&
  723. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  724. save_set.y, save_set.fb))
  725. drm_err(dev, "failed to restore config after modeset failure\n");
  726. kfree(save_connector_encoders);
  727. kfree(save_encoder_crtcs);
  728. return ret;
  729. }
  730. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  731. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  732. {
  733. int dpms = DRM_MODE_DPMS_OFF;
  734. struct drm_connector *connector;
  735. struct drm_connector_list_iter conn_iter;
  736. struct drm_device *dev = encoder->dev;
  737. drm_connector_list_iter_begin(dev, &conn_iter);
  738. drm_for_each_connector_iter(connector, &conn_iter)
  739. if (connector->encoder == encoder)
  740. if (connector->dpms < dpms)
  741. dpms = connector->dpms;
  742. drm_connector_list_iter_end(&conn_iter);
  743. return dpms;
  744. }
  745. /* Helper which handles bridge ordering around encoder dpms */
  746. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  747. {
  748. const struct drm_encoder_helper_funcs *encoder_funcs;
  749. encoder_funcs = encoder->helper_private;
  750. if (!encoder_funcs)
  751. return;
  752. if (encoder_funcs->dpms)
  753. encoder_funcs->dpms(encoder, mode);
  754. }
  755. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  756. {
  757. int dpms = DRM_MODE_DPMS_OFF;
  758. struct drm_connector *connector;
  759. struct drm_connector_list_iter conn_iter;
  760. struct drm_device *dev = crtc->dev;
  761. drm_connector_list_iter_begin(dev, &conn_iter);
  762. drm_for_each_connector_iter(connector, &conn_iter)
  763. if (connector->encoder && connector->encoder->crtc == crtc)
  764. if (connector->dpms < dpms)
  765. dpms = connector->dpms;
  766. drm_connector_list_iter_end(&conn_iter);
  767. return dpms;
  768. }
  769. /**
  770. * drm_helper_connector_dpms() - connector dpms helper implementation
  771. * @connector: affected connector
  772. * @mode: DPMS mode
  773. *
  774. * The drm_helper_connector_dpms() helper function implements the
  775. * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
  776. * helpers.
  777. *
  778. * This is the main helper function provided by the CRTC helper framework for
  779. * implementing the DPMS connector attribute. It computes the new desired DPMS
  780. * state for all encoders and CRTCs in the output mesh and calls the
  781. * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
  782. * provided by the driver.
  783. *
  784. * This function is deprecated. New drivers must implement atomic modeset
  785. * support, where DPMS is handled in the DRM core.
  786. *
  787. * Returns:
  788. * Always returns 0.
  789. */
  790. int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  791. {
  792. struct drm_encoder *encoder = connector->encoder;
  793. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  794. int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  795. drm_WARN_ON(connector->dev, drm_drv_uses_atomic_modeset(connector->dev));
  796. if (mode == connector->dpms)
  797. return 0;
  798. old_dpms = connector->dpms;
  799. connector->dpms = mode;
  800. if (encoder)
  801. encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  802. /* from off to on, do crtc then encoder */
  803. if (mode < old_dpms) {
  804. if (crtc) {
  805. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  806. if (crtc_funcs->dpms)
  807. (*crtc_funcs->dpms) (crtc,
  808. drm_helper_choose_crtc_dpms(crtc));
  809. }
  810. if (encoder)
  811. drm_helper_encoder_dpms(encoder, encoder_dpms);
  812. }
  813. /* from on to off, do encoder then crtc */
  814. if (mode > old_dpms) {
  815. if (encoder)
  816. drm_helper_encoder_dpms(encoder, encoder_dpms);
  817. if (crtc) {
  818. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  819. if (crtc_funcs->dpms)
  820. (*crtc_funcs->dpms) (crtc,
  821. drm_helper_choose_crtc_dpms(crtc));
  822. }
  823. }
  824. return 0;
  825. }
  826. EXPORT_SYMBOL(drm_helper_connector_dpms);
  827. /**
  828. * drm_helper_resume_force_mode - force-restore mode setting configuration
  829. * @dev: drm_device which should be restored
  830. *
  831. * Drivers which use the mode setting helpers can use this function to
  832. * force-restore the mode setting configuration e.g. on resume or when something
  833. * else might have trampled over the hw state (like some overzealous old BIOSen
  834. * tended to do).
  835. *
  836. * This helper doesn't provide a error return value since restoring the old
  837. * config should never fail due to resource allocation issues since the driver
  838. * has successfully set the restored configuration already. Hence this should
  839. * boil down to the equivalent of a few dpms on calls, which also don't provide
  840. * an error code.
  841. *
  842. * Drivers where simply restoring an old configuration again might fail (e.g.
  843. * due to slight differences in allocating shared resources when the
  844. * configuration is restored in a different order than when userspace set it up)
  845. * need to use their own restore logic.
  846. *
  847. * This function is deprecated. New drivers should implement atomic mode-
  848. * setting and use the atomic suspend/resume helpers.
  849. *
  850. * See also:
  851. * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
  852. */
  853. void drm_helper_resume_force_mode(struct drm_device *dev)
  854. {
  855. struct drm_crtc *crtc;
  856. struct drm_encoder *encoder;
  857. const struct drm_crtc_helper_funcs *crtc_funcs;
  858. int encoder_dpms;
  859. bool ret;
  860. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  861. drm_modeset_lock_all(dev);
  862. drm_for_each_crtc(crtc, dev) {
  863. if (!crtc->enabled)
  864. continue;
  865. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  866. crtc->x, crtc->y, crtc->primary->fb);
  867. /* Restoring the old config should never fail! */
  868. if (ret == false)
  869. drm_err(dev, "failed to set mode on crtc %p\n", crtc);
  870. /* Turn off outputs that were already powered off */
  871. if (drm_helper_choose_crtc_dpms(crtc)) {
  872. drm_for_each_encoder(encoder, dev) {
  873. if(encoder->crtc != crtc)
  874. continue;
  875. encoder_dpms = drm_helper_choose_encoder_dpms(
  876. encoder);
  877. drm_helper_encoder_dpms(encoder, encoder_dpms);
  878. }
  879. crtc_funcs = crtc->helper_private;
  880. if (crtc_funcs->dpms)
  881. (*crtc_funcs->dpms) (crtc,
  882. drm_helper_choose_crtc_dpms(crtc));
  883. }
  884. }
  885. /* disable the unused connectors while restoring the modesetting */
  886. __drm_helper_disable_unused_functions(dev);
  887. drm_modeset_unlock_all(dev);
  888. }
  889. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  890. /**
  891. * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
  892. * @dev: DRM device whose CRTCs to turn off
  893. *
  894. * Drivers may want to call this on unload to ensure that all displays are
  895. * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
  896. *
  897. * Note: This should only be used by non-atomic legacy drivers. For an atomic
  898. * version look at drm_atomic_helper_shutdown().
  899. *
  900. * Returns:
  901. * Zero on success, error code on failure.
  902. */
  903. int drm_helper_force_disable_all(struct drm_device *dev)
  904. {
  905. struct drm_crtc *crtc;
  906. int ret = 0;
  907. drm_modeset_lock_all(dev);
  908. drm_for_each_crtc(crtc, dev)
  909. if (crtc->enabled) {
  910. struct drm_mode_set set = {
  911. .crtc = crtc,
  912. };
  913. ret = drm_mode_set_config_internal(&set);
  914. if (ret)
  915. goto out;
  916. }
  917. out:
  918. drm_modeset_unlock_all(dev);
  919. return ret;
  920. }
  921. EXPORT_SYMBOL(drm_helper_force_disable_all);