drm_crtc_helper.c 35 KB

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