drm_crtc_helper.c 26 KB

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