drm_crtc_helper.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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;
  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. bool saved_enabled;
  365. struct drm_encoder *encoder;
  366. bool ret = true;
  367. saved_enabled = crtc->enabled;
  368. crtc->enabled = drm_helper_crtc_in_use(crtc);
  369. if (!crtc->enabled)
  370. return true;
  371. adjusted_mode = drm_mode_duplicate(dev, mode);
  372. if (!adjusted_mode) {
  373. crtc->enabled = saved_enabled;
  374. return false;
  375. }
  376. saved_mode = crtc->mode;
  377. saved_x = crtc->x;
  378. saved_y = crtc->y;
  379. /* Update crtc values up front so the driver can rely on them for mode
  380. * setting.
  381. */
  382. crtc->mode = *mode;
  383. crtc->x = x;
  384. crtc->y = y;
  385. /* Pass our mode to the connectors and the CRTC to give them a chance to
  386. * adjust it according to limitations or connector properties, and also
  387. * a chance to reject the mode entirely.
  388. */
  389. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  390. if (encoder->crtc != crtc)
  391. continue;
  392. if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
  393. ret = encoder->bridge->funcs->mode_fixup(
  394. encoder->bridge, mode, adjusted_mode);
  395. if (!ret) {
  396. DRM_DEBUG_KMS("Bridge fixup failed\n");
  397. goto done;
  398. }
  399. }
  400. encoder_funcs = encoder->helper_private;
  401. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  402. adjusted_mode))) {
  403. DRM_DEBUG_KMS("Encoder fixup failed\n");
  404. goto done;
  405. }
  406. }
  407. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  408. DRM_DEBUG_KMS("CRTC fixup failed\n");
  409. goto done;
  410. }
  411. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  412. /* Prepare the encoders and CRTCs before setting the mode. */
  413. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  414. if (encoder->crtc != crtc)
  415. continue;
  416. if (encoder->bridge)
  417. encoder->bridge->funcs->disable(encoder->bridge);
  418. encoder_funcs = encoder->helper_private;
  419. /* Disable the encoders as the first thing we do. */
  420. encoder_funcs->prepare(encoder);
  421. if (encoder->bridge)
  422. encoder->bridge->funcs->post_disable(encoder->bridge);
  423. }
  424. drm_crtc_prepare_encoders(dev);
  425. crtc_funcs->prepare(crtc);
  426. /* Set up the DPLL and any encoders state that needs to adjust or depend
  427. * on the DPLL.
  428. */
  429. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  430. if (!ret)
  431. goto done;
  432. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  433. if (encoder->crtc != crtc)
  434. continue;
  435. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  436. encoder->base.id, drm_get_encoder_name(encoder),
  437. mode->base.id, mode->name);
  438. encoder_funcs = encoder->helper_private;
  439. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  440. if (encoder->bridge && encoder->bridge->funcs->mode_set)
  441. encoder->bridge->funcs->mode_set(encoder->bridge, mode,
  442. adjusted_mode);
  443. }
  444. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  445. crtc_funcs->commit(crtc);
  446. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  447. if (encoder->crtc != crtc)
  448. continue;
  449. if (encoder->bridge)
  450. encoder->bridge->funcs->pre_enable(encoder->bridge);
  451. encoder_funcs = encoder->helper_private;
  452. encoder_funcs->commit(encoder);
  453. if (encoder->bridge)
  454. encoder->bridge->funcs->enable(encoder->bridge);
  455. }
  456. /* Store real post-adjustment hardware mode. */
  457. crtc->hwmode = *adjusted_mode;
  458. /* Calculate and store various constants which
  459. * are later needed by vblank and swap-completion
  460. * timestamping. They are derived from true hwmode.
  461. */
  462. drm_calc_timestamping_constants(crtc);
  463. /* FIXME: add subpixel order */
  464. done:
  465. drm_mode_destroy(dev, adjusted_mode);
  466. if (!ret) {
  467. crtc->enabled = saved_enabled;
  468. crtc->mode = saved_mode;
  469. crtc->x = saved_x;
  470. crtc->y = saved_y;
  471. }
  472. return ret;
  473. }
  474. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  475. static int
  476. drm_crtc_helper_disable(struct drm_crtc *crtc)
  477. {
  478. struct drm_device *dev = crtc->dev;
  479. struct drm_connector *connector;
  480. struct drm_encoder *encoder;
  481. /* Decouple all encoders and their attached connectors from this crtc */
  482. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  483. if (encoder->crtc != crtc)
  484. continue;
  485. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  486. if (connector->encoder != encoder)
  487. continue;
  488. connector->encoder = NULL;
  489. /*
  490. * drm_helper_disable_unused_functions() ought to be
  491. * doing this, but since we've decoupled the encoder
  492. * from the connector above, the required connection
  493. * between them is henceforth no longer available.
  494. */
  495. connector->dpms = DRM_MODE_DPMS_OFF;
  496. }
  497. }
  498. drm_helper_disable_unused_functions(dev);
  499. return 0;
  500. }
  501. /**
  502. * drm_crtc_helper_set_config - set a new config from userspace
  503. * @set: mode set configuration
  504. *
  505. * LOCKING:
  506. * Caller must hold mode config lock.
  507. *
  508. * Setup a new configuration, provided by the upper layers (either an ioctl call
  509. * from userspace or internally e.g. from the fbdev suppport code) in @set, and
  510. * enable it. This is the main helper functions for drivers that implement
  511. * kernel mode setting with the crtc helper functions and the assorted
  512. * ->prepare(), ->modeset() and ->commit() helper callbacks.
  513. *
  514. * RETURNS:
  515. * Returns 0 on success, -ERRNO on failure.
  516. */
  517. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  518. {
  519. struct drm_device *dev;
  520. struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  521. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  522. bool mode_changed = false; /* if true do a full mode set */
  523. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  524. struct drm_connector *save_connectors, *connector;
  525. int count = 0, ro, fail = 0;
  526. struct drm_crtc_helper_funcs *crtc_funcs;
  527. struct drm_mode_set save_set;
  528. int ret;
  529. int i;
  530. DRM_DEBUG_KMS("\n");
  531. BUG_ON(!set);
  532. BUG_ON(!set->crtc);
  533. BUG_ON(!set->crtc->helper_private);
  534. /* Enforce sane interface api - has been abused by the fb helper. */
  535. BUG_ON(!set->mode && set->fb);
  536. BUG_ON(set->fb && set->num_connectors == 0);
  537. crtc_funcs = set->crtc->helper_private;
  538. if (!set->mode)
  539. set->fb = NULL;
  540. if (set->fb) {
  541. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  542. set->crtc->base.id, set->fb->base.id,
  543. (int)set->num_connectors, set->x, set->y);
  544. } else {
  545. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  546. return drm_crtc_helper_disable(set->crtc);
  547. }
  548. dev = set->crtc->dev;
  549. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  550. * connector data. */
  551. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  552. sizeof(struct drm_crtc), GFP_KERNEL);
  553. if (!save_crtcs)
  554. return -ENOMEM;
  555. save_encoders = kzalloc(dev->mode_config.num_encoder *
  556. sizeof(struct drm_encoder), GFP_KERNEL);
  557. if (!save_encoders) {
  558. kfree(save_crtcs);
  559. return -ENOMEM;
  560. }
  561. save_connectors = kzalloc(dev->mode_config.num_connector *
  562. sizeof(struct drm_connector), GFP_KERNEL);
  563. if (!save_connectors) {
  564. kfree(save_crtcs);
  565. kfree(save_encoders);
  566. return -ENOMEM;
  567. }
  568. /* Copy data. Note that driver private data is not affected.
  569. * Should anything bad happen only the expected state is
  570. * restored, not the drivers personal bookkeeping.
  571. */
  572. count = 0;
  573. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  574. save_crtcs[count++] = *crtc;
  575. }
  576. count = 0;
  577. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  578. save_encoders[count++] = *encoder;
  579. }
  580. count = 0;
  581. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  582. save_connectors[count++] = *connector;
  583. }
  584. save_set.crtc = set->crtc;
  585. save_set.mode = &set->crtc->mode;
  586. save_set.x = set->crtc->x;
  587. save_set.y = set->crtc->y;
  588. save_set.fb = set->crtc->fb;
  589. /* We should be able to check here if the fb has the same properties
  590. * and then just flip_or_move it */
  591. if (set->crtc->fb != set->fb) {
  592. /* If we have no fb then treat it as a full mode set */
  593. if (set->crtc->fb == NULL) {
  594. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  595. mode_changed = true;
  596. } else if (set->fb == NULL) {
  597. mode_changed = true;
  598. } else if (set->fb->pixel_format !=
  599. set->crtc->fb->pixel_format) {
  600. mode_changed = true;
  601. } else
  602. fb_changed = true;
  603. }
  604. if (set->x != set->crtc->x || set->y != set->crtc->y)
  605. fb_changed = true;
  606. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  607. DRM_DEBUG_KMS("modes are different, full mode set\n");
  608. drm_mode_debug_printmodeline(&set->crtc->mode);
  609. drm_mode_debug_printmodeline(set->mode);
  610. mode_changed = true;
  611. }
  612. /* a) traverse passed in connector list and get encoders for them */
  613. count = 0;
  614. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  615. struct drm_connector_helper_funcs *connector_funcs =
  616. connector->helper_private;
  617. new_encoder = connector->encoder;
  618. for (ro = 0; ro < set->num_connectors; ro++) {
  619. if (set->connectors[ro] == connector) {
  620. new_encoder = connector_funcs->best_encoder(connector);
  621. /* if we can't get an encoder for a connector
  622. we are setting now - then fail */
  623. if (new_encoder == NULL)
  624. /* don't break so fail path works correct */
  625. fail = 1;
  626. break;
  627. if (connector->dpms != DRM_MODE_DPMS_ON) {
  628. DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
  629. mode_changed = true;
  630. }
  631. }
  632. }
  633. if (new_encoder != connector->encoder) {
  634. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  635. mode_changed = true;
  636. /* If the encoder is reused for another connector, then
  637. * the appropriate crtc will be set later.
  638. */
  639. if (connector->encoder)
  640. connector->encoder->crtc = NULL;
  641. connector->encoder = new_encoder;
  642. }
  643. }
  644. if (fail) {
  645. ret = -EINVAL;
  646. goto fail;
  647. }
  648. count = 0;
  649. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  650. if (!connector->encoder)
  651. continue;
  652. if (connector->encoder->crtc == set->crtc)
  653. new_crtc = NULL;
  654. else
  655. new_crtc = connector->encoder->crtc;
  656. for (ro = 0; ro < set->num_connectors; ro++) {
  657. if (set->connectors[ro] == connector)
  658. new_crtc = set->crtc;
  659. }
  660. /* Make sure the new CRTC will work with the encoder */
  661. if (new_crtc &&
  662. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  663. ret = -EINVAL;
  664. goto fail;
  665. }
  666. if (new_crtc != connector->encoder->crtc) {
  667. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  668. mode_changed = true;
  669. connector->encoder->crtc = new_crtc;
  670. }
  671. if (new_crtc) {
  672. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  673. connector->base.id, drm_get_connector_name(connector),
  674. new_crtc->base.id);
  675. } else {
  676. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  677. connector->base.id, drm_get_connector_name(connector));
  678. }
  679. }
  680. /* mode_set_base is not a required function */
  681. if (fb_changed && !crtc_funcs->mode_set_base)
  682. mode_changed = true;
  683. if (mode_changed) {
  684. if (drm_helper_crtc_in_use(set->crtc)) {
  685. DRM_DEBUG_KMS("attempting to set mode from"
  686. " userspace\n");
  687. drm_mode_debug_printmodeline(set->mode);
  688. set->crtc->fb = set->fb;
  689. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  690. set->x, set->y,
  691. save_set.fb)) {
  692. DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  693. set->crtc->base.id);
  694. set->crtc->fb = save_set.fb;
  695. ret = -EINVAL;
  696. goto fail;
  697. }
  698. DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  699. for (i = 0; i < set->num_connectors; i++) {
  700. DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  701. drm_get_connector_name(set->connectors[i]));
  702. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  703. }
  704. }
  705. drm_helper_disable_unused_functions(dev);
  706. } else if (fb_changed) {
  707. set->crtc->x = set->x;
  708. set->crtc->y = set->y;
  709. set->crtc->fb = set->fb;
  710. ret = crtc_funcs->mode_set_base(set->crtc,
  711. set->x, set->y, save_set.fb);
  712. if (ret != 0) {
  713. set->crtc->x = save_set.x;
  714. set->crtc->y = save_set.y;
  715. set->crtc->fb = save_set.fb;
  716. goto fail;
  717. }
  718. }
  719. kfree(save_connectors);
  720. kfree(save_encoders);
  721. kfree(save_crtcs);
  722. return 0;
  723. fail:
  724. /* Restore all previous data. */
  725. count = 0;
  726. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  727. *crtc = save_crtcs[count++];
  728. }
  729. count = 0;
  730. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  731. *encoder = save_encoders[count++];
  732. }
  733. count = 0;
  734. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  735. *connector = save_connectors[count++];
  736. }
  737. /* Try to restore the config */
  738. if (mode_changed &&
  739. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  740. save_set.y, save_set.fb))
  741. DRM_ERROR("failed to restore config after modeset failure\n");
  742. kfree(save_connectors);
  743. kfree(save_encoders);
  744. kfree(save_crtcs);
  745. return ret;
  746. }
  747. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  748. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  749. {
  750. int dpms = DRM_MODE_DPMS_OFF;
  751. struct drm_connector *connector;
  752. struct drm_device *dev = encoder->dev;
  753. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  754. if (connector->encoder == encoder)
  755. if (connector->dpms < dpms)
  756. dpms = connector->dpms;
  757. return dpms;
  758. }
  759. /* Helper which handles bridge ordering around encoder dpms */
  760. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  761. {
  762. struct drm_bridge *bridge = encoder->bridge;
  763. struct drm_encoder_helper_funcs *encoder_funcs;
  764. if (bridge) {
  765. if (mode == DRM_MODE_DPMS_ON)
  766. bridge->funcs->pre_enable(bridge);
  767. else
  768. bridge->funcs->disable(bridge);
  769. }
  770. encoder_funcs = encoder->helper_private;
  771. if (encoder_funcs->dpms)
  772. encoder_funcs->dpms(encoder, mode);
  773. if (bridge) {
  774. if (mode == DRM_MODE_DPMS_ON)
  775. bridge->funcs->enable(bridge);
  776. else
  777. bridge->funcs->post_disable(bridge);
  778. }
  779. }
  780. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  781. {
  782. int dpms = DRM_MODE_DPMS_OFF;
  783. struct drm_connector *connector;
  784. struct drm_device *dev = crtc->dev;
  785. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  786. if (connector->encoder && connector->encoder->crtc == crtc)
  787. if (connector->dpms < dpms)
  788. dpms = connector->dpms;
  789. return dpms;
  790. }
  791. /**
  792. * drm_helper_connector_dpms() - connector dpms helper implementation
  793. * @connector: affected connector
  794. * @mode: DPMS mode
  795. *
  796. * This is the main helper function provided by the crtc helper framework for
  797. * implementing the DPMS connector attribute. It computes the new desired DPMS
  798. * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  799. * callback provided by the driver appropriately.
  800. */
  801. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  802. {
  803. struct drm_encoder *encoder = connector->encoder;
  804. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  805. int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  806. if (mode == connector->dpms)
  807. return;
  808. old_dpms = connector->dpms;
  809. connector->dpms = mode;
  810. if (encoder)
  811. encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  812. /* from off to on, do crtc then encoder */
  813. if (mode < old_dpms) {
  814. if (crtc) {
  815. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  816. if (crtc_funcs->dpms)
  817. (*crtc_funcs->dpms) (crtc,
  818. drm_helper_choose_crtc_dpms(crtc));
  819. }
  820. if (encoder)
  821. drm_helper_encoder_dpms(encoder, encoder_dpms);
  822. }
  823. /* from on to off, do encoder then crtc */
  824. if (mode > old_dpms) {
  825. if (encoder)
  826. drm_helper_encoder_dpms(encoder, encoder_dpms);
  827. if (crtc) {
  828. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  829. if (crtc_funcs->dpms)
  830. (*crtc_funcs->dpms) (crtc,
  831. drm_helper_choose_crtc_dpms(crtc));
  832. }
  833. }
  834. return;
  835. }
  836. EXPORT_SYMBOL(drm_helper_connector_dpms);
  837. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  838. struct drm_mode_fb_cmd2 *mode_cmd)
  839. {
  840. int i;
  841. fb->width = mode_cmd->width;
  842. fb->height = mode_cmd->height;
  843. for (i = 0; i < 4; i++) {
  844. fb->pitches[i] = mode_cmd->pitches[i];
  845. fb->offsets[i] = mode_cmd->offsets[i];
  846. }
  847. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  848. &fb->bits_per_pixel);
  849. fb->pixel_format = mode_cmd->pixel_format;
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  853. int drm_helper_resume_force_mode(struct drm_device *dev)
  854. {
  855. struct drm_crtc *crtc;
  856. struct drm_encoder *encoder;
  857. struct drm_crtc_helper_funcs *crtc_funcs;
  858. int ret, encoder_dpms;
  859. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  860. if (!crtc->enabled)
  861. continue;
  862. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  863. crtc->x, crtc->y, crtc->fb);
  864. if (ret == false)
  865. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  866. /* Turn off outputs that were already powered off */
  867. if (drm_helper_choose_crtc_dpms(crtc)) {
  868. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  869. if(encoder->crtc != crtc)
  870. continue;
  871. encoder_dpms = drm_helper_choose_encoder_dpms(
  872. encoder);
  873. drm_helper_encoder_dpms(encoder, encoder_dpms);
  874. }
  875. crtc_funcs = crtc->helper_private;
  876. if (crtc_funcs->dpms)
  877. (*crtc_funcs->dpms) (crtc,
  878. drm_helper_choose_crtc_dpms(crtc));
  879. }
  880. }
  881. /* disable the unused connectors while restoring the modesetting */
  882. drm_helper_disable_unused_functions(dev);
  883. return 0;
  884. }
  885. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  886. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  887. {
  888. /* send a uevent + call fbdev */
  889. drm_sysfs_hotplug_event(dev);
  890. if (dev->mode_config.funcs->output_poll_changed)
  891. dev->mode_config.funcs->output_poll_changed(dev);
  892. }
  893. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  894. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  895. static void output_poll_execute(struct work_struct *work)
  896. {
  897. struct delayed_work *delayed_work = to_delayed_work(work);
  898. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  899. struct drm_connector *connector;
  900. enum drm_connector_status old_status;
  901. bool repoll = false, changed = false;
  902. if (!drm_kms_helper_poll)
  903. return;
  904. mutex_lock(&dev->mode_config.mutex);
  905. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  906. /* Ignore forced connectors. */
  907. if (connector->force)
  908. continue;
  909. /* Ignore HPD capable connectors and connectors where we don't
  910. * want any hotplug detection at all for polling. */
  911. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  912. continue;
  913. repoll = true;
  914. old_status = connector->status;
  915. /* if we are connected and don't want to poll for disconnect
  916. skip it */
  917. if (old_status == connector_status_connected &&
  918. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  919. continue;
  920. connector->status = connector->funcs->detect(connector, false);
  921. if (old_status != connector->status) {
  922. const char *old, *new;
  923. old = drm_get_connector_status_name(old_status);
  924. new = drm_get_connector_status_name(connector->status);
  925. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  926. "status updated from %s to %s\n",
  927. connector->base.id,
  928. drm_get_connector_name(connector),
  929. old, new);
  930. changed = true;
  931. }
  932. }
  933. mutex_unlock(&dev->mode_config.mutex);
  934. if (changed)
  935. drm_kms_helper_hotplug_event(dev);
  936. if (repoll)
  937. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  938. }
  939. void drm_kms_helper_poll_disable(struct drm_device *dev)
  940. {
  941. if (!dev->mode_config.poll_enabled)
  942. return;
  943. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  944. }
  945. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  946. void drm_kms_helper_poll_enable(struct drm_device *dev)
  947. {
  948. bool poll = false;
  949. struct drm_connector *connector;
  950. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  951. return;
  952. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  953. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  954. DRM_CONNECTOR_POLL_DISCONNECT))
  955. poll = true;
  956. }
  957. if (poll)
  958. schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  959. }
  960. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  961. void drm_kms_helper_poll_init(struct drm_device *dev)
  962. {
  963. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  964. dev->mode_config.poll_enabled = true;
  965. drm_kms_helper_poll_enable(dev);
  966. }
  967. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  968. void drm_kms_helper_poll_fini(struct drm_device *dev)
  969. {
  970. drm_kms_helper_poll_disable(dev);
  971. }
  972. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  973. bool drm_helper_hpd_irq_event(struct drm_device *dev)
  974. {
  975. struct drm_connector *connector;
  976. enum drm_connector_status old_status;
  977. bool changed = false;
  978. if (!dev->mode_config.poll_enabled)
  979. return false;
  980. mutex_lock(&dev->mode_config.mutex);
  981. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  982. /* Only handle HPD capable connectors. */
  983. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  984. continue;
  985. old_status = connector->status;
  986. connector->status = connector->funcs->detect(connector, false);
  987. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  988. connector->base.id,
  989. drm_get_connector_name(connector),
  990. drm_get_connector_status_name(old_status),
  991. drm_get_connector_status_name(connector->status));
  992. if (old_status != connector->status)
  993. changed = true;
  994. }
  995. mutex_unlock(&dev->mode_config.mutex);
  996. if (changed)
  997. drm_kms_helper_hotplug_event(dev);
  998. return changed;
  999. }
  1000. EXPORT_SYMBOL(drm_helper_hpd_irq_event);