drm_crtc_helper.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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/export.h>
  32. #include <linux/moduleparam.h>
  33. #include <drm/drmP.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_fourcc.h>
  36. #include <drm/drm_crtc_helper.h>
  37. #include <drm/drm_fb_helper.h>
  38. #include <drm/drm_edid.h>
  39. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  40. MODULE_DESCRIPTION("DRM KMS helper");
  41. MODULE_LICENSE("GPL and additional rights");
  42. /**
  43. * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  44. * connector list
  45. * @dev: drm device to operate on
  46. *
  47. * Some userspace presumes that the first connected connector is the main
  48. * display, where it's supposed to display e.g. the login screen. For
  49. * laptops, this should be the main panel. Use this function to sort all
  50. * (eDP/LVDS) panels to the front of the connector list, instead of
  51. * painstakingly trying to initialize them in the right order.
  52. */
  53. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  54. {
  55. struct drm_connector *connector, *tmp;
  56. struct list_head panel_list;
  57. INIT_LIST_HEAD(&panel_list);
  58. list_for_each_entry_safe(connector, tmp,
  59. &dev->mode_config.connector_list, head) {
  60. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  61. connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  62. list_move_tail(&connector->head, &panel_list);
  63. }
  64. list_splice(&panel_list, &dev->mode_config.connector_list);
  65. }
  66. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  67. static bool drm_kms_helper_poll = true;
  68. module_param_named(poll, drm_kms_helper_poll, bool, 0600);
  69. static void drm_mode_validate_flag(struct drm_connector *connector,
  70. int flags)
  71. {
  72. struct drm_display_mode *mode;
  73. if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
  74. DRM_MODE_FLAG_3D_MASK))
  75. return;
  76. list_for_each_entry(mode, &connector->modes, head) {
  77. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  78. !(flags & DRM_MODE_FLAG_INTERLACE))
  79. mode->status = MODE_NO_INTERLACE;
  80. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  81. !(flags & DRM_MODE_FLAG_DBLSCAN))
  82. mode->status = MODE_NO_DBLESCAN;
  83. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
  84. !(flags & DRM_MODE_FLAG_3D_MASK))
  85. mode->status = MODE_NO_STEREO;
  86. }
  87. return;
  88. }
  89. /**
  90. * drm_helper_probe_single_connector_modes - get complete set of display modes
  91. * @connector: connector to probe
  92. * @maxX: max width for modes
  93. * @maxY: max height for modes
  94. *
  95. * LOCKING:
  96. * Caller must hold mode config lock.
  97. *
  98. * Based on the helper callbacks implemented by @connector try to detect all
  99. * valid modes. Modes will first be added to the connector's probed_modes list,
  100. * then culled (based on validity and the @maxX, @maxY parameters) and put into
  101. * the normal modes list.
  102. *
  103. * Intended to be use as a generic implementation of the ->fill_modes()
  104. * @connector vfunc for drivers that use the crtc helpers for output mode
  105. * filtering and detection.
  106. *
  107. * RETURNS:
  108. * Number of modes found on @connector.
  109. */
  110. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  111. uint32_t maxX, uint32_t maxY)
  112. {
  113. struct drm_device *dev = connector->dev;
  114. struct drm_display_mode *mode;
  115. struct drm_connector_helper_funcs *connector_funcs =
  116. connector->helper_private;
  117. int count = 0;
  118. int mode_flags = 0;
  119. bool verbose_prune = true;
  120. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  121. drm_get_connector_name(connector));
  122. /* set all modes to the unverified state */
  123. list_for_each_entry(mode, &connector->modes, head)
  124. mode->status = MODE_UNVERIFIED;
  125. if (connector->force) {
  126. if (connector->force == DRM_FORCE_ON)
  127. connector->status = connector_status_connected;
  128. else
  129. connector->status = connector_status_disconnected;
  130. if (connector->funcs->force)
  131. connector->funcs->force(connector);
  132. } else {
  133. connector->status = connector->funcs->detect(connector, true);
  134. }
  135. /* Re-enable polling in case the global poll config changed. */
  136. if (drm_kms_helper_poll != dev->mode_config.poll_running)
  137. drm_kms_helper_poll_enable(dev);
  138. dev->mode_config.poll_running = drm_kms_helper_poll;
  139. if (connector->status == connector_status_disconnected) {
  140. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  141. connector->base.id, drm_get_connector_name(connector));
  142. drm_mode_connector_update_edid_property(connector, NULL);
  143. verbose_prune = false;
  144. goto prune;
  145. }
  146. #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
  147. count = drm_load_edid_firmware(connector);
  148. if (count == 0)
  149. #endif
  150. count = (*connector_funcs->get_modes)(connector);
  151. if (count == 0 && connector->status == connector_status_connected)
  152. count = drm_add_modes_noedid(connector, 1024, 768);
  153. if (count == 0)
  154. goto prune;
  155. drm_mode_connector_list_update(connector);
  156. if (maxX && maxY)
  157. drm_mode_validate_size(dev, &connector->modes, maxX,
  158. maxY, 0);
  159. if (connector->interlace_allowed)
  160. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  161. if (connector->doublescan_allowed)
  162. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  163. if (connector->stereo_allowed)
  164. mode_flags |= DRM_MODE_FLAG_3D_MASK;
  165. drm_mode_validate_flag(connector, mode_flags);
  166. list_for_each_entry(mode, &connector->modes, head) {
  167. if (mode->status == MODE_OK)
  168. mode->status = connector_funcs->mode_valid(connector,
  169. mode);
  170. }
  171. prune:
  172. drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
  173. if (list_empty(&connector->modes))
  174. return 0;
  175. list_for_each_entry(mode, &connector->modes, head)
  176. mode->vrefresh = drm_mode_vrefresh(mode);
  177. drm_mode_sort(&connector->modes);
  178. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  179. drm_get_connector_name(connector));
  180. list_for_each_entry(mode, &connector->modes, head) {
  181. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  182. drm_mode_debug_printmodeline(mode);
  183. }
  184. return count;
  185. }
  186. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  187. /**
  188. * drm_helper_encoder_in_use - check if a given encoder is in use
  189. * @encoder: encoder to check
  190. *
  191. * LOCKING:
  192. * Caller must hold mode config lock.
  193. *
  194. * Walk @encoders's DRM device's mode_config and see if it's in use.
  195. *
  196. * RETURNS:
  197. * True if @encoder is part of the mode_config, false otherwise.
  198. */
  199. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  200. {
  201. struct drm_connector *connector;
  202. struct drm_device *dev = encoder->dev;
  203. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  204. if (connector->encoder == encoder)
  205. return true;
  206. return false;
  207. }
  208. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  209. /**
  210. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  211. * @crtc: CRTC to check
  212. *
  213. * LOCKING:
  214. * Caller must hold mode config lock.
  215. *
  216. * Walk @crtc's DRM device's mode_config and see if it's in use.
  217. *
  218. * RETURNS:
  219. * True if @crtc is part of the mode_config, false otherwise.
  220. */
  221. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  222. {
  223. struct drm_encoder *encoder;
  224. struct drm_device *dev = crtc->dev;
  225. /* FIXME: Locking around list access? */
  226. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  227. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  228. return true;
  229. return false;
  230. }
  231. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  232. static void
  233. drm_encoder_disable(struct drm_encoder *encoder)
  234. {
  235. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  236. if (encoder->bridge)
  237. encoder->bridge->funcs->disable(encoder->bridge);
  238. if (encoder_funcs->disable)
  239. (*encoder_funcs->disable)(encoder);
  240. else
  241. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  242. if (encoder->bridge)
  243. encoder->bridge->funcs->post_disable(encoder->bridge);
  244. }
  245. /**
  246. * drm_helper_disable_unused_functions - disable unused objects
  247. * @dev: DRM device
  248. *
  249. * LOCKING:
  250. * Caller must hold mode config lock.
  251. *
  252. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  253. * by calling its dpms function, which should power it off.
  254. */
  255. void drm_helper_disable_unused_functions(struct drm_device *dev)
  256. {
  257. struct drm_encoder *encoder;
  258. struct drm_connector *connector;
  259. struct drm_crtc *crtc;
  260. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  261. if (!connector->encoder)
  262. continue;
  263. if (connector->status == connector_status_disconnected)
  264. connector->encoder = NULL;
  265. }
  266. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  267. if (!drm_helper_encoder_in_use(encoder)) {
  268. drm_encoder_disable(encoder);
  269. /* disconnector encoder from any connector */
  270. encoder->crtc = NULL;
  271. }
  272. }
  273. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  274. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  275. crtc->enabled = drm_helper_crtc_in_use(crtc);
  276. if (!crtc->enabled) {
  277. if (crtc_funcs->disable)
  278. (*crtc_funcs->disable)(crtc);
  279. else
  280. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  281. crtc->fb = NULL;
  282. }
  283. }
  284. }
  285. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  286. /**
  287. * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  288. * @encoder: encoder to test
  289. * @crtc: crtc to test
  290. *
  291. * Return false if @encoder can't be driven by @crtc, true otherwise.
  292. */
  293. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  294. struct drm_crtc *crtc)
  295. {
  296. struct drm_device *dev;
  297. struct drm_crtc *tmp;
  298. int crtc_mask = 1;
  299. WARN(!crtc, "checking null crtc?\n");
  300. dev = crtc->dev;
  301. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  302. if (tmp == crtc)
  303. break;
  304. crtc_mask <<= 1;
  305. }
  306. if (encoder->possible_crtcs & crtc_mask)
  307. return true;
  308. return false;
  309. }
  310. /*
  311. * Check the CRTC we're going to map each output to vs. its current
  312. * CRTC. If they don't match, we have to disable the output and the CRTC
  313. * since the driver will have to re-route things.
  314. */
  315. static void
  316. drm_crtc_prepare_encoders(struct drm_device *dev)
  317. {
  318. struct drm_encoder_helper_funcs *encoder_funcs;
  319. struct drm_encoder *encoder;
  320. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  321. encoder_funcs = encoder->helper_private;
  322. /* Disable unused encoders */
  323. if (encoder->crtc == NULL)
  324. drm_encoder_disable(encoder);
  325. /* Disable encoders whose CRTC is about to change */
  326. if (encoder_funcs->get_crtc &&
  327. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  328. drm_encoder_disable(encoder);
  329. }
  330. }
  331. /**
  332. * drm_crtc_helper_set_mode - internal helper to set a mode
  333. * @crtc: CRTC to program
  334. * @mode: mode to use
  335. * @x: horizontal offset into the surface
  336. * @y: vertical offset into the surface
  337. * @old_fb: old framebuffer, for cleanup
  338. *
  339. * LOCKING:
  340. * Caller must hold mode config lock.
  341. *
  342. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  343. * to fixup or reject the mode prior to trying to set it. This is an internal
  344. * helper that drivers could e.g. use to update properties that require the
  345. * entire output pipe to be disabled and re-enabled in a new configuration. For
  346. * example for changing whether audio is enabled on a hdmi link or for changing
  347. * panel fitter or dither attributes. It is also called by the
  348. * drm_crtc_helper_set_config() helper function to drive the mode setting
  349. * sequence.
  350. *
  351. * RETURNS:
  352. * True if the mode was set successfully, or false otherwise.
  353. */
  354. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  355. struct drm_display_mode *mode,
  356. int x, int y,
  357. struct drm_framebuffer *old_fb)
  358. {
  359. struct drm_device *dev = crtc->dev;
  360. struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
  361. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  362. struct drm_encoder_helper_funcs *encoder_funcs;
  363. int saved_x, saved_y;
  364. struct drm_encoder *encoder;
  365. bool ret = true;
  366. crtc->enabled = drm_helper_crtc_in_use(crtc);
  367. if (!crtc->enabled)
  368. return true;
  369. adjusted_mode = drm_mode_duplicate(dev, mode);
  370. if (!adjusted_mode)
  371. return false;
  372. saved_hwmode = crtc->hwmode;
  373. saved_mode = crtc->mode;
  374. saved_x = crtc->x;
  375. saved_y = crtc->y;
  376. /* Update crtc values up front so the driver can rely on them for mode
  377. * setting.
  378. */
  379. crtc->mode = *mode;
  380. crtc->x = x;
  381. crtc->y = y;
  382. /* Pass our mode to the connectors and the CRTC to give them a chance to
  383. * adjust it according to limitations or connector properties, and also
  384. * a chance to reject the mode entirely.
  385. */
  386. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  387. if (encoder->crtc != crtc)
  388. continue;
  389. if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
  390. ret = encoder->bridge->funcs->mode_fixup(
  391. encoder->bridge, mode, adjusted_mode);
  392. if (!ret) {
  393. DRM_DEBUG_KMS("Bridge fixup failed\n");
  394. goto done;
  395. }
  396. }
  397. encoder_funcs = encoder->helper_private;
  398. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  399. adjusted_mode))) {
  400. DRM_DEBUG_KMS("Encoder fixup failed\n");
  401. goto done;
  402. }
  403. }
  404. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  405. DRM_DEBUG_KMS("CRTC fixup failed\n");
  406. goto done;
  407. }
  408. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  409. /* Prepare the encoders and CRTCs before setting the mode. */
  410. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  411. if (encoder->crtc != crtc)
  412. continue;
  413. if (encoder->bridge)
  414. encoder->bridge->funcs->disable(encoder->bridge);
  415. encoder_funcs = encoder->helper_private;
  416. /* Disable the encoders as the first thing we do. */
  417. encoder_funcs->prepare(encoder);
  418. if (encoder->bridge)
  419. encoder->bridge->funcs->post_disable(encoder->bridge);
  420. }
  421. drm_crtc_prepare_encoders(dev);
  422. crtc_funcs->prepare(crtc);
  423. /* Set up the DPLL and any encoders state that needs to adjust or depend
  424. * on the DPLL.
  425. */
  426. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  427. if (!ret)
  428. goto done;
  429. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  430. if (encoder->crtc != crtc)
  431. continue;
  432. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  433. encoder->base.id, drm_get_encoder_name(encoder),
  434. mode->base.id, mode->name);
  435. encoder_funcs = encoder->helper_private;
  436. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  437. if (encoder->bridge && encoder->bridge->funcs->mode_set)
  438. encoder->bridge->funcs->mode_set(encoder->bridge, mode,
  439. adjusted_mode);
  440. }
  441. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  442. crtc_funcs->commit(crtc);
  443. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  444. if (encoder->crtc != crtc)
  445. continue;
  446. if (encoder->bridge)
  447. encoder->bridge->funcs->pre_enable(encoder->bridge);
  448. encoder_funcs = encoder->helper_private;
  449. encoder_funcs->commit(encoder);
  450. if (encoder->bridge)
  451. encoder->bridge->funcs->enable(encoder->bridge);
  452. }
  453. /* Store real post-adjustment hardware mode. */
  454. crtc->hwmode = *adjusted_mode;
  455. /* Calculate and store various constants which
  456. * are later needed by vblank and swap-completion
  457. * timestamping. They are derived from true hwmode.
  458. */
  459. drm_calc_timestamping_constants(crtc);
  460. /* FIXME: add subpixel order */
  461. done:
  462. drm_mode_destroy(dev, adjusted_mode);
  463. if (!ret) {
  464. crtc->hwmode = saved_hwmode;
  465. crtc->mode = saved_mode;
  466. crtc->x = saved_x;
  467. crtc->y = saved_y;
  468. }
  469. return ret;
  470. }
  471. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  472. static int
  473. drm_crtc_helper_disable(struct drm_crtc *crtc)
  474. {
  475. struct drm_device *dev = crtc->dev;
  476. struct drm_connector *connector;
  477. struct drm_encoder *encoder;
  478. /* Decouple all encoders and their attached connectors from this crtc */
  479. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  480. if (encoder->crtc != crtc)
  481. continue;
  482. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  483. if (connector->encoder != encoder)
  484. continue;
  485. connector->encoder = NULL;
  486. }
  487. }
  488. drm_helper_disable_unused_functions(dev);
  489. return 0;
  490. }
  491. /**
  492. * drm_crtc_helper_set_config - set a new config from userspace
  493. * @set: mode set configuration
  494. *
  495. * LOCKING:
  496. * Caller must hold mode config lock.
  497. *
  498. * Setup a new configuration, provided by the upper layers (either an ioctl call
  499. * from userspace or internally e.g. from the fbdev suppport code) in @set, and
  500. * enable it. This is the main helper functions for drivers that implement
  501. * kernel mode setting with the crtc helper functions and the assorted
  502. * ->prepare(), ->modeset() and ->commit() helper callbacks.
  503. *
  504. * RETURNS:
  505. * Returns 0 on success, -ERRNO on failure.
  506. */
  507. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  508. {
  509. struct drm_device *dev;
  510. struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  511. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  512. struct drm_framebuffer *old_fb = NULL;
  513. bool mode_changed = false; /* if true do a full mode set */
  514. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  515. struct drm_connector *save_connectors, *connector;
  516. int count = 0, ro, fail = 0;
  517. struct drm_crtc_helper_funcs *crtc_funcs;
  518. struct drm_mode_set save_set;
  519. int ret;
  520. int i;
  521. DRM_DEBUG_KMS("\n");
  522. BUG_ON(!set);
  523. BUG_ON(!set->crtc);
  524. BUG_ON(!set->crtc->helper_private);
  525. /* Enforce sane interface api - has been abused by the fb helper. */
  526. BUG_ON(!set->mode && set->fb);
  527. BUG_ON(set->fb && set->num_connectors == 0);
  528. crtc_funcs = set->crtc->helper_private;
  529. if (!set->mode)
  530. set->fb = NULL;
  531. if (set->fb) {
  532. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  533. set->crtc->base.id, set->fb->base.id,
  534. (int)set->num_connectors, set->x, set->y);
  535. } else {
  536. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  537. return drm_crtc_helper_disable(set->crtc);
  538. }
  539. dev = set->crtc->dev;
  540. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  541. * connector data. */
  542. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  543. sizeof(struct drm_crtc), GFP_KERNEL);
  544. if (!save_crtcs)
  545. return -ENOMEM;
  546. save_encoders = kzalloc(dev->mode_config.num_encoder *
  547. sizeof(struct drm_encoder), GFP_KERNEL);
  548. if (!save_encoders) {
  549. kfree(save_crtcs);
  550. return -ENOMEM;
  551. }
  552. save_connectors = kzalloc(dev->mode_config.num_connector *
  553. sizeof(struct drm_connector), GFP_KERNEL);
  554. if (!save_connectors) {
  555. kfree(save_crtcs);
  556. kfree(save_encoders);
  557. return -ENOMEM;
  558. }
  559. /* Copy data. Note that driver private data is not affected.
  560. * Should anything bad happen only the expected state is
  561. * restored, not the drivers personal bookkeeping.
  562. */
  563. count = 0;
  564. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  565. save_crtcs[count++] = *crtc;
  566. }
  567. count = 0;
  568. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  569. save_encoders[count++] = *encoder;
  570. }
  571. count = 0;
  572. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  573. save_connectors[count++] = *connector;
  574. }
  575. save_set.crtc = set->crtc;
  576. save_set.mode = &set->crtc->mode;
  577. save_set.x = set->crtc->x;
  578. save_set.y = set->crtc->y;
  579. save_set.fb = set->crtc->fb;
  580. /* We should be able to check here if the fb has the same properties
  581. * and then just flip_or_move it */
  582. if (set->crtc->fb != set->fb) {
  583. /* If we have no fb then treat it as a full mode set */
  584. if (set->crtc->fb == NULL) {
  585. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  586. mode_changed = true;
  587. } else if (set->fb == NULL) {
  588. mode_changed = true;
  589. } else if (set->fb->pixel_format !=
  590. set->crtc->fb->pixel_format) {
  591. mode_changed = true;
  592. } else
  593. fb_changed = true;
  594. }
  595. if (set->x != set->crtc->x || set->y != set->crtc->y)
  596. fb_changed = true;
  597. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  598. DRM_DEBUG_KMS("modes are different, full mode set\n");
  599. drm_mode_debug_printmodeline(&set->crtc->mode);
  600. drm_mode_debug_printmodeline(set->mode);
  601. mode_changed = true;
  602. }
  603. /* a) traverse passed in connector list and get encoders for them */
  604. count = 0;
  605. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  606. struct drm_connector_helper_funcs *connector_funcs =
  607. connector->helper_private;
  608. new_encoder = connector->encoder;
  609. for (ro = 0; ro < set->num_connectors; ro++) {
  610. if (set->connectors[ro] == connector) {
  611. new_encoder = connector_funcs->best_encoder(connector);
  612. /* if we can't get an encoder for a connector
  613. we are setting now - then fail */
  614. if (new_encoder == NULL)
  615. /* don't break so fail path works correct */
  616. fail = 1;
  617. break;
  618. if (connector->dpms != DRM_MODE_DPMS_ON) {
  619. DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
  620. mode_changed = true;
  621. }
  622. }
  623. }
  624. if (new_encoder != connector->encoder) {
  625. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  626. mode_changed = true;
  627. /* If the encoder is reused for another connector, then
  628. * the appropriate crtc will be set later.
  629. */
  630. if (connector->encoder)
  631. connector->encoder->crtc = NULL;
  632. connector->encoder = new_encoder;
  633. }
  634. }
  635. if (fail) {
  636. ret = -EINVAL;
  637. goto fail;
  638. }
  639. count = 0;
  640. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  641. if (!connector->encoder)
  642. continue;
  643. if (connector->encoder->crtc == set->crtc)
  644. new_crtc = NULL;
  645. else
  646. new_crtc = connector->encoder->crtc;
  647. for (ro = 0; ro < set->num_connectors; ro++) {
  648. if (set->connectors[ro] == connector)
  649. new_crtc = set->crtc;
  650. }
  651. /* Make sure the new CRTC will work with the encoder */
  652. if (new_crtc &&
  653. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  654. ret = -EINVAL;
  655. goto fail;
  656. }
  657. if (new_crtc != connector->encoder->crtc) {
  658. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  659. mode_changed = true;
  660. connector->encoder->crtc = new_crtc;
  661. }
  662. if (new_crtc) {
  663. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  664. connector->base.id, drm_get_connector_name(connector),
  665. new_crtc->base.id);
  666. } else {
  667. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  668. connector->base.id, drm_get_connector_name(connector));
  669. }
  670. }
  671. /* mode_set_base is not a required function */
  672. if (fb_changed && !crtc_funcs->mode_set_base)
  673. mode_changed = true;
  674. if (mode_changed) {
  675. set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
  676. if (set->crtc->enabled) {
  677. DRM_DEBUG_KMS("attempting to set mode from"
  678. " userspace\n");
  679. drm_mode_debug_printmodeline(set->mode);
  680. old_fb = set->crtc->fb;
  681. set->crtc->fb = set->fb;
  682. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  683. set->x, set->y,
  684. old_fb)) {
  685. DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  686. set->crtc->base.id);
  687. set->crtc->fb = old_fb;
  688. ret = -EINVAL;
  689. goto fail;
  690. }
  691. DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  692. for (i = 0; i < set->num_connectors; i++) {
  693. DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  694. drm_get_connector_name(set->connectors[i]));
  695. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  696. }
  697. }
  698. drm_helper_disable_unused_functions(dev);
  699. } else if (fb_changed) {
  700. set->crtc->x = set->x;
  701. set->crtc->y = set->y;
  702. old_fb = set->crtc->fb;
  703. if (set->crtc->fb != set->fb)
  704. set->crtc->fb = set->fb;
  705. ret = crtc_funcs->mode_set_base(set->crtc,
  706. set->x, set->y, old_fb);
  707. if (ret != 0) {
  708. set->crtc->fb = old_fb;
  709. goto fail;
  710. }
  711. }
  712. kfree(save_connectors);
  713. kfree(save_encoders);
  714. kfree(save_crtcs);
  715. return 0;
  716. fail:
  717. /* Restore all previous data. */
  718. count = 0;
  719. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  720. *crtc = save_crtcs[count++];
  721. }
  722. count = 0;
  723. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  724. *encoder = save_encoders[count++];
  725. }
  726. count = 0;
  727. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  728. *connector = save_connectors[count++];
  729. }
  730. /* Try to restore the config */
  731. if (mode_changed &&
  732. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  733. save_set.y, save_set.fb))
  734. DRM_ERROR("failed to restore config after modeset failure\n");
  735. kfree(save_connectors);
  736. kfree(save_encoders);
  737. kfree(save_crtcs);
  738. return ret;
  739. }
  740. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  741. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  742. {
  743. int dpms = DRM_MODE_DPMS_OFF;
  744. struct drm_connector *connector;
  745. struct drm_device *dev = encoder->dev;
  746. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  747. if (connector->encoder == encoder)
  748. if (connector->dpms < dpms)
  749. dpms = connector->dpms;
  750. return dpms;
  751. }
  752. /* Helper which handles bridge ordering around encoder dpms */
  753. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  754. {
  755. struct drm_bridge *bridge = encoder->bridge;
  756. struct drm_encoder_helper_funcs *encoder_funcs;
  757. if (bridge) {
  758. if (mode == DRM_MODE_DPMS_ON)
  759. bridge->funcs->pre_enable(bridge);
  760. else
  761. bridge->funcs->disable(bridge);
  762. }
  763. encoder_funcs = encoder->helper_private;
  764. if (encoder_funcs->dpms)
  765. encoder_funcs->dpms(encoder, mode);
  766. if (bridge) {
  767. if (mode == DRM_MODE_DPMS_ON)
  768. bridge->funcs->enable(bridge);
  769. else
  770. bridge->funcs->post_disable(bridge);
  771. }
  772. }
  773. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  774. {
  775. int dpms = DRM_MODE_DPMS_OFF;
  776. struct drm_connector *connector;
  777. struct drm_device *dev = crtc->dev;
  778. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  779. if (connector->encoder && connector->encoder->crtc == crtc)
  780. if (connector->dpms < dpms)
  781. dpms = connector->dpms;
  782. return dpms;
  783. }
  784. /**
  785. * drm_helper_connector_dpms() - connector dpms helper implementation
  786. * @connector: affected connector
  787. * @mode: DPMS mode
  788. *
  789. * This is the main helper function provided by the crtc helper framework for
  790. * implementing the DPMS connector attribute. It computes the new desired DPMS
  791. * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  792. * callback provided by the driver appropriately.
  793. */
  794. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  795. {
  796. struct drm_encoder *encoder = connector->encoder;
  797. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  798. int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  799. if (mode == connector->dpms)
  800. return;
  801. old_dpms = connector->dpms;
  802. connector->dpms = mode;
  803. if (encoder)
  804. encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  805. /* from off to on, do crtc then encoder */
  806. if (mode < old_dpms) {
  807. if (crtc) {
  808. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  809. if (crtc_funcs->dpms)
  810. (*crtc_funcs->dpms) (crtc,
  811. drm_helper_choose_crtc_dpms(crtc));
  812. }
  813. if (encoder)
  814. drm_helper_encoder_dpms(encoder, encoder_dpms);
  815. }
  816. /* from on to off, do encoder then crtc */
  817. if (mode > old_dpms) {
  818. if (encoder)
  819. drm_helper_encoder_dpms(encoder, encoder_dpms);
  820. if (crtc) {
  821. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  822. if (crtc_funcs->dpms)
  823. (*crtc_funcs->dpms) (crtc,
  824. drm_helper_choose_crtc_dpms(crtc));
  825. }
  826. }
  827. return;
  828. }
  829. EXPORT_SYMBOL(drm_helper_connector_dpms);
  830. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  831. struct drm_mode_fb_cmd2 *mode_cmd)
  832. {
  833. int i;
  834. fb->width = mode_cmd->width;
  835. fb->height = mode_cmd->height;
  836. for (i = 0; i < 4; i++) {
  837. fb->pitches[i] = mode_cmd->pitches[i];
  838. fb->offsets[i] = mode_cmd->offsets[i];
  839. }
  840. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  841. &fb->bits_per_pixel);
  842. fb->pixel_format = mode_cmd->pixel_format;
  843. return 0;
  844. }
  845. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  846. int drm_helper_resume_force_mode(struct drm_device *dev)
  847. {
  848. struct drm_crtc *crtc;
  849. struct drm_encoder *encoder;
  850. struct drm_crtc_helper_funcs *crtc_funcs;
  851. int ret, encoder_dpms;
  852. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  853. if (!crtc->enabled)
  854. continue;
  855. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  856. crtc->x, crtc->y, crtc->fb);
  857. if (ret == false)
  858. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  859. /* Turn off outputs that were already powered off */
  860. if (drm_helper_choose_crtc_dpms(crtc)) {
  861. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  862. if(encoder->crtc != crtc)
  863. continue;
  864. encoder_dpms = drm_helper_choose_encoder_dpms(
  865. encoder);
  866. drm_helper_encoder_dpms(encoder, encoder_dpms);
  867. }
  868. crtc_funcs = crtc->helper_private;
  869. if (crtc_funcs->dpms)
  870. (*crtc_funcs->dpms) (crtc,
  871. drm_helper_choose_crtc_dpms(crtc));
  872. }
  873. }
  874. /* disable the unused connectors while restoring the modesetting */
  875. drm_helper_disable_unused_functions(dev);
  876. return 0;
  877. }
  878. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  879. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  880. {
  881. /* send a uevent + call fbdev */
  882. drm_sysfs_hotplug_event(dev);
  883. if (dev->mode_config.funcs->output_poll_changed)
  884. dev->mode_config.funcs->output_poll_changed(dev);
  885. }
  886. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  887. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  888. static void output_poll_execute(struct work_struct *work)
  889. {
  890. struct delayed_work *delayed_work = to_delayed_work(work);
  891. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  892. struct drm_connector *connector;
  893. enum drm_connector_status old_status;
  894. bool repoll = false, changed = false;
  895. if (!drm_kms_helper_poll)
  896. return;
  897. mutex_lock(&dev->mode_config.mutex);
  898. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  899. /* Ignore forced connectors. */
  900. if (connector->force)
  901. continue;
  902. /* Ignore HPD capable connectors and connectors where we don't
  903. * want any hotplug detection at all for polling. */
  904. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  905. continue;
  906. repoll = true;
  907. old_status = connector->status;
  908. /* if we are connected and don't want to poll for disconnect
  909. skip it */
  910. if (old_status == connector_status_connected &&
  911. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  912. continue;
  913. connector->status = connector->funcs->detect(connector, false);
  914. if (old_status != connector->status) {
  915. const char *old, *new;
  916. old = drm_get_connector_status_name(old_status);
  917. new = drm_get_connector_status_name(connector->status);
  918. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  919. "status updated from %s to %s\n",
  920. connector->base.id,
  921. drm_get_connector_name(connector),
  922. old, new);
  923. changed = true;
  924. }
  925. }
  926. mutex_unlock(&dev->mode_config.mutex);
  927. if (changed)
  928. drm_kms_helper_hotplug_event(dev);
  929. if (repoll)
  930. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  931. }
  932. void drm_kms_helper_poll_disable(struct drm_device *dev)
  933. {
  934. if (!dev->mode_config.poll_enabled)
  935. return;
  936. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  937. }
  938. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  939. void drm_kms_helper_poll_enable(struct drm_device *dev)
  940. {
  941. bool poll = false;
  942. struct drm_connector *connector;
  943. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  944. return;
  945. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  946. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  947. DRM_CONNECTOR_POLL_DISCONNECT))
  948. poll = true;
  949. }
  950. if (poll)
  951. schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  952. }
  953. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  954. void drm_kms_helper_poll_init(struct drm_device *dev)
  955. {
  956. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  957. dev->mode_config.poll_enabled = true;
  958. drm_kms_helper_poll_enable(dev);
  959. }
  960. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  961. void drm_kms_helper_poll_fini(struct drm_device *dev)
  962. {
  963. drm_kms_helper_poll_disable(dev);
  964. }
  965. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  966. void drm_helper_hpd_irq_event(struct drm_device *dev)
  967. {
  968. struct drm_connector *connector;
  969. enum drm_connector_status old_status;
  970. bool changed = false;
  971. if (!dev->mode_config.poll_enabled)
  972. return;
  973. mutex_lock(&dev->mode_config.mutex);
  974. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  975. /* Only handle HPD capable connectors. */
  976. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  977. continue;
  978. old_status = connector->status;
  979. connector->status = connector->funcs->detect(connector, false);
  980. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  981. connector->base.id,
  982. drm_get_connector_name(connector),
  983. drm_get_connector_status_name(old_status),
  984. drm_get_connector_status_name(connector->status));
  985. if (old_status != connector->status)
  986. changed = true;
  987. }
  988. mutex_unlock(&dev->mode_config.mutex);
  989. if (changed)
  990. drm_kms_helper_hotplug_event(dev);
  991. }
  992. EXPORT_SYMBOL(drm_helper_hpd_irq_event);