drm_crtc_helper.c 32 KB

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