drm_crtc_helper.c 31 KB

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