drm_crtc_helper.c 31 KB

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