drm_crtc_helper.c 31 KB

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