drm_crtc_helper.c 31 KB

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