drm_crtc_helper.c 30 KB

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