drm_crtc_helper.c 33 KB

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