drm_crtc_helper.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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_fourcc.h>
  38. #include <drm/drm_crtc_helper.h>
  39. #include <drm/drm_fb_helper.h>
  40. #include <drm/drm_plane_helper.h>
  41. #include <drm/drm_atomic_helper.h>
  42. #include <drm/drm_edid.h>
  43. /**
  44. * DOC: overview
  45. *
  46. * The CRTC modeset helper library provides a default set_config implementation
  47. * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
  48. * the same callbacks which drivers can use to e.g. restore the modeset
  49. * configuration on resume with drm_helper_resume_force_mode().
  50. *
  51. * Note that this helper library doesn't track the current power state of CRTCs
  52. * and encoders. It can call callbacks like ->dpms() even though the hardware is
  53. * already in the desired state. This deficiency has been fixed in the atomic
  54. * helpers.
  55. *
  56. * The driver callbacks are mostly compatible with the atomic modeset helpers,
  57. * except for the handling of the primary plane: Atomic helpers require that the
  58. * primary plane is implemented as a real standalone plane and not directly tied
  59. * to the CRTC state. For easier transition this library provides functions to
  60. * implement the old semantics required by the CRTC helpers using the new plane
  61. * and atomic helper callbacks.
  62. *
  63. * Drivers are strongly urged to convert to the atomic helpers (by way of first
  64. * converting to the plane helpers). New drivers must not use these functions
  65. * but need to implement the atomic interface instead, potentially using the
  66. * atomic helpers for that.
  67. *
  68. * These legacy modeset helpers use the same function table structures as
  69. * all other modesetting helpers. See the documentation for struct
  70. * &drm_crtc_helper_funcs, struct &drm_encoder_helper_funcs and struct
  71. * &drm_connector_helper_funcs.
  72. */
  73. /**
  74. * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  75. * connector list
  76. * @dev: drm device to operate on
  77. *
  78. * Some userspace presumes that the first connected connector is the main
  79. * display, where it's supposed to display e.g. the login screen. For
  80. * laptops, this should be the main panel. Use this function to sort all
  81. * (eDP/LVDS) panels to the front of the connector list, instead of
  82. * painstakingly trying to initialize them in the right order.
  83. */
  84. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  85. {
  86. struct drm_connector *connector, *tmp;
  87. struct list_head panel_list;
  88. INIT_LIST_HEAD(&panel_list);
  89. list_for_each_entry_safe(connector, tmp,
  90. &dev->mode_config.connector_list, head) {
  91. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  92. connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  93. list_move_tail(&connector->head, &panel_list);
  94. }
  95. list_splice(&panel_list, &dev->mode_config.connector_list);
  96. }
  97. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  98. /**
  99. * drm_helper_encoder_in_use - check if a given encoder is in use
  100. * @encoder: encoder to check
  101. *
  102. * Checks whether @encoder is with the current mode setting output configuration
  103. * in use by any connector. This doesn't mean that it is actually enabled since
  104. * the DPMS state is tracked separately.
  105. *
  106. * Returns:
  107. * True if @encoder is used, false otherwise.
  108. */
  109. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  110. {
  111. struct drm_connector *connector;
  112. struct drm_device *dev = encoder->dev;
  113. /*
  114. * We can expect this mutex to be locked if we are not panicking.
  115. * Locking is currently fubar in the panic handler.
  116. */
  117. if (!oops_in_progress) {
  118. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  119. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  120. }
  121. drm_for_each_connector(connector, dev)
  122. if (connector->encoder == encoder)
  123. return true;
  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. /*
  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. WARN_ON(!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. drm_bridge_disable(encoder->bridge);
  159. if (encoder_funcs->disable)
  160. (*encoder_funcs->disable)(encoder);
  161. else
  162. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  163. drm_bridge_post_disable(encoder->bridge);
  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_modeset_lock_all(dev);
  211. __drm_helper_disable_unused_functions(dev);
  212. drm_modeset_unlock_all(dev);
  213. }
  214. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  215. /*
  216. * Check the CRTC we're going to map each output to vs. its current
  217. * CRTC. If they don't match, we have to disable the output and the CRTC
  218. * since the driver will have to re-route things.
  219. */
  220. static void
  221. drm_crtc_prepare_encoders(struct drm_device *dev)
  222. {
  223. const struct drm_encoder_helper_funcs *encoder_funcs;
  224. struct drm_encoder *encoder;
  225. drm_for_each_encoder(encoder, dev) {
  226. encoder_funcs = encoder->helper_private;
  227. /* Disable unused encoders */
  228. if (encoder->crtc == NULL)
  229. drm_encoder_disable(encoder);
  230. /* Disable encoders whose CRTC is about to change */
  231. if (encoder_funcs->get_crtc &&
  232. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  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. 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. saved_mode = crtc->mode;
  280. 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. 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. ret = drm_bridge_mode_fixup(encoder->bridge,
  297. mode, adjusted_mode);
  298. if (!ret) {
  299. DRM_DEBUG_KMS("Bridge fixup failed\n");
  300. goto done;
  301. }
  302. encoder_funcs = encoder->helper_private;
  303. if (encoder_funcs->mode_fixup) {
  304. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  305. adjusted_mode))) {
  306. DRM_DEBUG_KMS("Encoder fixup failed\n");
  307. goto done;
  308. }
  309. }
  310. }
  311. if (crtc_funcs->mode_fixup) {
  312. if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
  313. adjusted_mode))) {
  314. DRM_DEBUG_KMS("CRTC fixup failed\n");
  315. goto done;
  316. }
  317. }
  318. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  319. crtc->hwmode = *adjusted_mode;
  320. /* Prepare the encoders and CRTCs before setting the mode. */
  321. drm_for_each_encoder(encoder, dev) {
  322. if (encoder->crtc != crtc)
  323. continue;
  324. drm_bridge_disable(encoder->bridge);
  325. encoder_funcs = encoder->helper_private;
  326. /* Disable the encoders as the first thing we do. */
  327. encoder_funcs->prepare(encoder);
  328. drm_bridge_post_disable(encoder->bridge);
  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. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  342. encoder->base.id, encoder->name,
  343. mode->base.id, mode->name);
  344. encoder_funcs = encoder->helper_private;
  345. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  346. drm_bridge_mode_set(encoder->bridge, 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. drm_bridge_pre_enable(encoder->bridge);
  354. encoder_funcs = encoder->helper_private;
  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. if (encoder->crtc != crtc)
  385. continue;
  386. drm_for_each_connector(connector, dev) {
  387. if (connector->encoder != encoder)
  388. continue;
  389. connector->encoder = NULL;
  390. /*
  391. * drm_helper_disable_unused_functions() ought to be
  392. * doing this, but since we've decoupled the encoder
  393. * from the connector above, the required connection
  394. * between them is henceforth no longer available.
  395. */
  396. connector->dpms = DRM_MODE_DPMS_OFF;
  397. }
  398. }
  399. __drm_helper_disable_unused_functions(dev);
  400. }
  401. /**
  402. * drm_crtc_helper_set_config - set a new config from userspace
  403. * @set: mode set configuration
  404. *
  405. * The drm_crtc_helper_set_config() helper function implements the set_config
  406. * callback of struct &drm_crtc_funcs for drivers using the legacy CRTC helpers.
  407. *
  408. * It first tries to locate the best encoder for each connector by calling the
  409. * connector ->best_encoder() (struct &drm_connector_helper_funcs) helper
  410. * operation.
  411. *
  412. * After locating the appropriate encoders, the helper function will call the
  413. * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
  414. * or reject it completely in which case an error will be returned to the
  415. * application. If the new configuration after mode adjustment is identical to
  416. * the current configuration the helper function will return without performing
  417. * any other operation.
  418. *
  419. * If the adjusted mode is identical to the current mode but changes to the
  420. * frame buffer need to be applied, the drm_crtc_helper_set_config() function
  421. * will call the CRTC ->mode_set_base() (struct &drm_crtc_helper_funcs) helper
  422. * operation.
  423. *
  424. * If the adjusted mode differs from the current mode, or if the
  425. * ->mode_set_base() helper operation is not provided, the helper function
  426. * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
  427. * and ->commit() CRTC and encoder helper operations, in that order.
  428. * Alternatively it can also use the dpms and disable helper operations. For
  429. * details see struct &drm_crtc_helper_funcs and struct
  430. * &drm_encoder_helper_funcs.
  431. *
  432. * This function is deprecated. New drivers must implement atomic modeset
  433. * support, for which this function is unsuitable. Instead drivers should use
  434. * drm_atomic_helper_set_config().
  435. *
  436. * Returns:
  437. * Returns 0 on success, negative errno numbers on failure.
  438. */
  439. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  440. {
  441. struct drm_device *dev;
  442. struct drm_crtc *new_crtc;
  443. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  444. bool mode_changed = false; /* if true do a full mode set */
  445. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  446. struct drm_connector *save_connectors, *connector;
  447. int count = 0, ro, fail = 0;
  448. const struct drm_crtc_helper_funcs *crtc_funcs;
  449. struct drm_mode_set save_set;
  450. int ret;
  451. int i;
  452. DRM_DEBUG_KMS("\n");
  453. BUG_ON(!set);
  454. BUG_ON(!set->crtc);
  455. BUG_ON(!set->crtc->helper_private);
  456. /* Enforce sane interface api - has been abused by the fb helper. */
  457. BUG_ON(!set->mode && set->fb);
  458. BUG_ON(set->fb && set->num_connectors == 0);
  459. crtc_funcs = set->crtc->helper_private;
  460. if (!set->mode)
  461. set->fb = NULL;
  462. if (set->fb) {
  463. DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  464. set->crtc->base.id, set->crtc->name,
  465. set->fb->base.id,
  466. (int)set->num_connectors, set->x, set->y);
  467. } else {
  468. DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
  469. set->crtc->base.id, set->crtc->name);
  470. drm_crtc_helper_disable(set->crtc);
  471. return 0;
  472. }
  473. dev = set->crtc->dev;
  474. drm_warn_on_modeset_not_all_locked(dev);
  475. /*
  476. * Allocate space for the backup of all (non-pointer) encoder and
  477. * connector data.
  478. */
  479. save_encoders = kzalloc(dev->mode_config.num_encoder *
  480. sizeof(struct drm_encoder), GFP_KERNEL);
  481. if (!save_encoders)
  482. return -ENOMEM;
  483. save_connectors = kzalloc(dev->mode_config.num_connector *
  484. sizeof(struct drm_connector), GFP_KERNEL);
  485. if (!save_connectors) {
  486. kfree(save_encoders);
  487. return -ENOMEM;
  488. }
  489. /*
  490. * Copy data. Note that driver private data is not affected.
  491. * Should anything bad happen only the expected state is
  492. * restored, not the drivers personal bookkeeping.
  493. */
  494. count = 0;
  495. drm_for_each_encoder(encoder, dev) {
  496. save_encoders[count++] = *encoder;
  497. }
  498. count = 0;
  499. drm_for_each_connector(connector, dev) {
  500. save_connectors[count++] = *connector;
  501. }
  502. save_set.crtc = set->crtc;
  503. save_set.mode = &set->crtc->mode;
  504. save_set.x = set->crtc->x;
  505. save_set.y = set->crtc->y;
  506. save_set.fb = set->crtc->primary->fb;
  507. /* We should be able to check here if the fb has the same properties
  508. * and then just flip_or_move it */
  509. if (set->crtc->primary->fb != set->fb) {
  510. /* If we have no fb then treat it as a full mode set */
  511. if (set->crtc->primary->fb == NULL) {
  512. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  513. mode_changed = true;
  514. } else if (set->fb->pixel_format !=
  515. set->crtc->primary->fb->pixel_format) {
  516. mode_changed = true;
  517. } else
  518. fb_changed = true;
  519. }
  520. if (set->x != set->crtc->x || set->y != set->crtc->y)
  521. fb_changed = true;
  522. if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
  523. DRM_DEBUG_KMS("modes are different, full mode set\n");
  524. drm_mode_debug_printmodeline(&set->crtc->mode);
  525. drm_mode_debug_printmodeline(set->mode);
  526. mode_changed = true;
  527. }
  528. /* a) traverse passed in connector list and get encoders for them */
  529. count = 0;
  530. drm_for_each_connector(connector, dev) {
  531. const struct drm_connector_helper_funcs *connector_funcs =
  532. connector->helper_private;
  533. new_encoder = connector->encoder;
  534. for (ro = 0; ro < set->num_connectors; ro++) {
  535. if (set->connectors[ro] == connector) {
  536. new_encoder = connector_funcs->best_encoder(connector);
  537. /* if we can't get an encoder for a connector
  538. we are setting now - then fail */
  539. if (new_encoder == NULL)
  540. /* don't break so fail path works correct */
  541. fail = 1;
  542. if (connector->dpms != DRM_MODE_DPMS_ON) {
  543. DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
  544. mode_changed = true;
  545. }
  546. break;
  547. }
  548. }
  549. if (new_encoder != connector->encoder) {
  550. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  551. mode_changed = true;
  552. /* If the encoder is reused for another connector, then
  553. * the appropriate crtc will be set later.
  554. */
  555. if (connector->encoder)
  556. connector->encoder->crtc = NULL;
  557. connector->encoder = new_encoder;
  558. }
  559. }
  560. if (fail) {
  561. ret = -EINVAL;
  562. goto fail;
  563. }
  564. count = 0;
  565. drm_for_each_connector(connector, dev) {
  566. if (!connector->encoder)
  567. continue;
  568. if (connector->encoder->crtc == set->crtc)
  569. new_crtc = NULL;
  570. else
  571. new_crtc = connector->encoder->crtc;
  572. for (ro = 0; ro < set->num_connectors; ro++) {
  573. if (set->connectors[ro] == connector)
  574. new_crtc = set->crtc;
  575. }
  576. /* Make sure the new CRTC will work with the encoder */
  577. if (new_crtc &&
  578. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  579. ret = -EINVAL;
  580. goto fail;
  581. }
  582. if (new_crtc != connector->encoder->crtc) {
  583. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  584. mode_changed = true;
  585. connector->encoder->crtc = new_crtc;
  586. }
  587. if (new_crtc) {
  588. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
  589. connector->base.id, connector->name,
  590. new_crtc->base.id, new_crtc->name);
  591. } else {
  592. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  593. connector->base.id, connector->name);
  594. }
  595. }
  596. /* mode_set_base is not a required function */
  597. if (fb_changed && !crtc_funcs->mode_set_base)
  598. mode_changed = true;
  599. if (mode_changed) {
  600. if (drm_helper_crtc_in_use(set->crtc)) {
  601. DRM_DEBUG_KMS("attempting to set mode from"
  602. " userspace\n");
  603. drm_mode_debug_printmodeline(set->mode);
  604. set->crtc->primary->fb = set->fb;
  605. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  606. set->x, set->y,
  607. save_set.fb)) {
  608. DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
  609. set->crtc->base.id, set->crtc->name);
  610. set->crtc->primary->fb = save_set.fb;
  611. ret = -EINVAL;
  612. goto fail;
  613. }
  614. DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  615. for (i = 0; i < set->num_connectors; i++) {
  616. DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  617. set->connectors[i]->name);
  618. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  619. }
  620. }
  621. __drm_helper_disable_unused_functions(dev);
  622. } else if (fb_changed) {
  623. set->crtc->x = set->x;
  624. set->crtc->y = set->y;
  625. set->crtc->primary->fb = set->fb;
  626. ret = crtc_funcs->mode_set_base(set->crtc,
  627. set->x, set->y, save_set.fb);
  628. if (ret != 0) {
  629. set->crtc->x = save_set.x;
  630. set->crtc->y = save_set.y;
  631. set->crtc->primary->fb = save_set.fb;
  632. goto fail;
  633. }
  634. }
  635. kfree(save_connectors);
  636. kfree(save_encoders);
  637. return 0;
  638. fail:
  639. /* Restore all previous data. */
  640. count = 0;
  641. drm_for_each_encoder(encoder, dev) {
  642. *encoder = save_encoders[count++];
  643. }
  644. count = 0;
  645. drm_for_each_connector(connector, dev) {
  646. *connector = save_connectors[count++];
  647. }
  648. /* Try to restore the config */
  649. if (mode_changed &&
  650. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  651. save_set.y, save_set.fb))
  652. DRM_ERROR("failed to restore config after modeset failure\n");
  653. kfree(save_connectors);
  654. kfree(save_encoders);
  655. return ret;
  656. }
  657. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  658. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  659. {
  660. int dpms = DRM_MODE_DPMS_OFF;
  661. struct drm_connector *connector;
  662. struct drm_device *dev = encoder->dev;
  663. drm_for_each_connector(connector, dev)
  664. if (connector->encoder == encoder)
  665. if (connector->dpms < dpms)
  666. dpms = connector->dpms;
  667. return dpms;
  668. }
  669. /* Helper which handles bridge ordering around encoder dpms */
  670. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  671. {
  672. struct drm_bridge *bridge = encoder->bridge;
  673. const struct drm_encoder_helper_funcs *encoder_funcs;
  674. if (mode == DRM_MODE_DPMS_ON)
  675. drm_bridge_pre_enable(bridge);
  676. else
  677. drm_bridge_disable(bridge);
  678. encoder_funcs = encoder->helper_private;
  679. if (encoder_funcs->dpms)
  680. encoder_funcs->dpms(encoder, mode);
  681. if (mode == DRM_MODE_DPMS_ON)
  682. drm_bridge_enable(bridge);
  683. else
  684. drm_bridge_post_disable(bridge);
  685. }
  686. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  687. {
  688. int dpms = DRM_MODE_DPMS_OFF;
  689. struct drm_connector *connector;
  690. struct drm_device *dev = crtc->dev;
  691. drm_for_each_connector(connector, dev)
  692. if (connector->encoder && connector->encoder->crtc == crtc)
  693. if (connector->dpms < dpms)
  694. dpms = connector->dpms;
  695. return dpms;
  696. }
  697. /**
  698. * drm_helper_connector_dpms() - connector dpms helper implementation
  699. * @connector: affected connector
  700. * @mode: DPMS mode
  701. *
  702. * The drm_helper_connector_dpms() helper function implements the ->dpms()
  703. * callback of struct &drm_connector_funcs for drivers using the legacy CRTC helpers.
  704. *
  705. * This is the main helper function provided by the CRTC helper framework for
  706. * implementing the DPMS connector attribute. It computes the new desired DPMS
  707. * state for all encoders and CRTCs in the output mesh and calls the ->dpms()
  708. * callbacks provided by the driver in struct &drm_crtc_helper_funcs and struct
  709. * &drm_encoder_helper_funcs appropriately.
  710. *
  711. * This function is deprecated. New drivers must implement atomic modeset
  712. * support, for which this function is unsuitable. Instead drivers should use
  713. * drm_atomic_helper_connector_dpms().
  714. *
  715. * Returns:
  716. * Always returns 0.
  717. */
  718. int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  719. {
  720. struct drm_encoder *encoder = connector->encoder;
  721. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  722. int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  723. if (mode == connector->dpms)
  724. return 0;
  725. old_dpms = connector->dpms;
  726. connector->dpms = mode;
  727. if (encoder)
  728. encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  729. /* from off to on, do crtc then encoder */
  730. if (mode < old_dpms) {
  731. if (crtc) {
  732. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  733. if (crtc_funcs->dpms)
  734. (*crtc_funcs->dpms) (crtc,
  735. drm_helper_choose_crtc_dpms(crtc));
  736. }
  737. if (encoder)
  738. drm_helper_encoder_dpms(encoder, encoder_dpms);
  739. }
  740. /* from on to off, do encoder then crtc */
  741. if (mode > old_dpms) {
  742. if (encoder)
  743. drm_helper_encoder_dpms(encoder, encoder_dpms);
  744. if (crtc) {
  745. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  746. if (crtc_funcs->dpms)
  747. (*crtc_funcs->dpms) (crtc,
  748. drm_helper_choose_crtc_dpms(crtc));
  749. }
  750. }
  751. return 0;
  752. }
  753. EXPORT_SYMBOL(drm_helper_connector_dpms);
  754. /**
  755. * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
  756. * @fb: drm_framebuffer object to fill out
  757. * @mode_cmd: metadata from the userspace fb creation request
  758. *
  759. * This helper can be used in a drivers fb_create callback to pre-fill the fb's
  760. * metadata fields.
  761. */
  762. void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  763. const struct drm_mode_fb_cmd2 *mode_cmd)
  764. {
  765. int i;
  766. fb->width = mode_cmd->width;
  767. fb->height = mode_cmd->height;
  768. for (i = 0; i < 4; i++) {
  769. fb->pitches[i] = mode_cmd->pitches[i];
  770. fb->offsets[i] = mode_cmd->offsets[i];
  771. fb->modifier[i] = mode_cmd->modifier[i];
  772. }
  773. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  774. &fb->bits_per_pixel);
  775. fb->pixel_format = mode_cmd->pixel_format;
  776. fb->flags = mode_cmd->flags;
  777. }
  778. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  779. /**
  780. * drm_helper_resume_force_mode - force-restore mode setting configuration
  781. * @dev: drm_device which should be restored
  782. *
  783. * Drivers which use the mode setting helpers can use this function to
  784. * force-restore the mode setting configuration e.g. on resume or when something
  785. * else might have trampled over the hw state (like some overzealous old BIOSen
  786. * tended to do).
  787. *
  788. * This helper doesn't provide a error return value since restoring the old
  789. * config should never fail due to resource allocation issues since the driver
  790. * has successfully set the restored configuration already. Hence this should
  791. * boil down to the equivalent of a few dpms on calls, which also don't provide
  792. * an error code.
  793. *
  794. * Drivers where simply restoring an old configuration again might fail (e.g.
  795. * due to slight differences in allocating shared resources when the
  796. * configuration is restored in a different order than when userspace set it up)
  797. * need to use their own restore logic.
  798. *
  799. * This function is deprecated. New drivers should implement atomic mode-
  800. * setting and use the atomic suspend/resume helpers.
  801. *
  802. * See also:
  803. * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
  804. */
  805. void drm_helper_resume_force_mode(struct drm_device *dev)
  806. {
  807. struct drm_crtc *crtc;
  808. struct drm_encoder *encoder;
  809. const struct drm_crtc_helper_funcs *crtc_funcs;
  810. int encoder_dpms;
  811. bool ret;
  812. drm_modeset_lock_all(dev);
  813. drm_for_each_crtc(crtc, dev) {
  814. if (!crtc->enabled)
  815. continue;
  816. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  817. crtc->x, crtc->y, crtc->primary->fb);
  818. /* Restoring the old config should never fail! */
  819. if (ret == false)
  820. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  821. /* Turn off outputs that were already powered off */
  822. if (drm_helper_choose_crtc_dpms(crtc)) {
  823. drm_for_each_encoder(encoder, dev) {
  824. if(encoder->crtc != crtc)
  825. continue;
  826. encoder_dpms = drm_helper_choose_encoder_dpms(
  827. encoder);
  828. drm_helper_encoder_dpms(encoder, encoder_dpms);
  829. }
  830. crtc_funcs = crtc->helper_private;
  831. if (crtc_funcs->dpms)
  832. (*crtc_funcs->dpms) (crtc,
  833. drm_helper_choose_crtc_dpms(crtc));
  834. }
  835. }
  836. /* disable the unused connectors while restoring the modesetting */
  837. __drm_helper_disable_unused_functions(dev);
  838. drm_modeset_unlock_all(dev);
  839. }
  840. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  841. /**
  842. * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
  843. * @crtc: DRM CRTC
  844. * @mode: DRM display mode which userspace requested
  845. * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
  846. * @x: x offset of the CRTC scanout area on the underlying framebuffer
  847. * @y: y offset of the CRTC scanout area on the underlying framebuffer
  848. * @old_fb: previous framebuffer
  849. *
  850. * This function implements a callback useable as the ->mode_set callback
  851. * required by the CRTC helpers. Besides the atomic plane helper functions for
  852. * the primary plane the driver must also provide the ->mode_set_nofb callback
  853. * to set up the CRTC.
  854. *
  855. * This is a transitional helper useful for converting drivers to the atomic
  856. * interfaces.
  857. */
  858. int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  859. struct drm_display_mode *adjusted_mode, int x, int y,
  860. struct drm_framebuffer *old_fb)
  861. {
  862. struct drm_crtc_state *crtc_state;
  863. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  864. int ret;
  865. if (crtc->funcs->atomic_duplicate_state)
  866. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  867. else {
  868. if (!crtc->state)
  869. drm_atomic_helper_crtc_reset(crtc);
  870. crtc_state = drm_atomic_helper_crtc_duplicate_state(crtc);
  871. }
  872. if (!crtc_state)
  873. return -ENOMEM;
  874. crtc_state->planes_changed = true;
  875. crtc_state->mode_changed = true;
  876. ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
  877. if (ret)
  878. goto out;
  879. drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
  880. if (crtc_funcs->atomic_check) {
  881. ret = crtc_funcs->atomic_check(crtc, crtc_state);
  882. if (ret)
  883. goto out;
  884. }
  885. swap(crtc->state, crtc_state);
  886. crtc_funcs->mode_set_nofb(crtc);
  887. ret = drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
  888. out:
  889. if (crtc_state) {
  890. if (crtc->funcs->atomic_destroy_state)
  891. crtc->funcs->atomic_destroy_state(crtc, crtc_state);
  892. else
  893. drm_atomic_helper_crtc_destroy_state(crtc, crtc_state);
  894. }
  895. return ret;
  896. }
  897. EXPORT_SYMBOL(drm_helper_crtc_mode_set);
  898. /**
  899. * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
  900. * @crtc: DRM CRTC
  901. * @x: x offset of the CRTC scanout area on the underlying framebuffer
  902. * @y: y offset of the CRTC scanout area on the underlying framebuffer
  903. * @old_fb: previous framebuffer
  904. *
  905. * This function implements a callback useable as the ->mode_set_base used
  906. * required by the CRTC helpers. The driver must provide the atomic plane helper
  907. * functions for the primary plane.
  908. *
  909. * This is a transitional helper useful for converting drivers to the atomic
  910. * interfaces.
  911. */
  912. int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  913. struct drm_framebuffer *old_fb)
  914. {
  915. struct drm_plane_state *plane_state;
  916. struct drm_plane *plane = crtc->primary;
  917. if (plane->funcs->atomic_duplicate_state)
  918. plane_state = plane->funcs->atomic_duplicate_state(plane);
  919. else if (plane->state)
  920. plane_state = drm_atomic_helper_plane_duplicate_state(plane);
  921. else
  922. plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
  923. if (!plane_state)
  924. return -ENOMEM;
  925. plane_state->plane = plane;
  926. plane_state->crtc = crtc;
  927. drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb);
  928. plane_state->crtc_x = 0;
  929. plane_state->crtc_y = 0;
  930. plane_state->crtc_h = crtc->mode.vdisplay;
  931. plane_state->crtc_w = crtc->mode.hdisplay;
  932. plane_state->src_x = x << 16;
  933. plane_state->src_y = y << 16;
  934. plane_state->src_h = crtc->mode.vdisplay << 16;
  935. plane_state->src_w = crtc->mode.hdisplay << 16;
  936. return drm_plane_helper_commit(plane, plane_state, old_fb);
  937. }
  938. EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);
  939. /**
  940. * drm_helper_crtc_enable_color_mgmt - enable color management properties
  941. * @crtc: DRM CRTC
  942. * @degamma_lut_size: the size of the degamma lut (before CSC)
  943. * @gamma_lut_size: the size of the gamma lut (after CSC)
  944. *
  945. * This function lets the driver enable the color correction properties on a
  946. * CRTC. This includes 3 degamma, csc and gamma properties that userspace can
  947. * set and 2 size properties to inform the userspace of the lut sizes.
  948. */
  949. void drm_helper_crtc_enable_color_mgmt(struct drm_crtc *crtc,
  950. int degamma_lut_size,
  951. int gamma_lut_size)
  952. {
  953. struct drm_device *dev = crtc->dev;
  954. struct drm_mode_config *config = &dev->mode_config;
  955. drm_object_attach_property(&crtc->base,
  956. config->degamma_lut_property, 0);
  957. drm_object_attach_property(&crtc->base,
  958. config->ctm_property, 0);
  959. drm_object_attach_property(&crtc->base,
  960. config->gamma_lut_property, 0);
  961. drm_object_attach_property(&crtc->base,
  962. config->degamma_lut_size_property,
  963. degamma_lut_size);
  964. drm_object_attach_property(&crtc->base,
  965. config->gamma_lut_size_property,
  966. gamma_lut_size);
  967. }
  968. EXPORT_SYMBOL(drm_helper_crtc_enable_color_mgmt);