drm_crtc_helper.c 33 KB

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