drm_crtc_helper.c 33 KB

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