drm_crtc_helper.c 31 KB

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