drm_crtc_helper.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 if (set->fb->pixel_format !=
  560. set->crtc->fb->pixel_format) {
  561. mode_changed = true;
  562. } else
  563. fb_changed = true;
  564. }
  565. if (set->x != set->crtc->x || set->y != set->crtc->y)
  566. fb_changed = true;
  567. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  568. DRM_DEBUG_KMS("modes are different, full mode set\n");
  569. drm_mode_debug_printmodeline(&set->crtc->mode);
  570. drm_mode_debug_printmodeline(set->mode);
  571. mode_changed = true;
  572. }
  573. /* a) traverse passed in connector list and get encoders for them */
  574. count = 0;
  575. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  576. struct drm_connector_helper_funcs *connector_funcs =
  577. connector->helper_private;
  578. new_encoder = connector->encoder;
  579. for (ro = 0; ro < set->num_connectors; ro++) {
  580. if (set->connectors[ro] == connector) {
  581. new_encoder = connector_funcs->best_encoder(connector);
  582. /* if we can't get an encoder for a connector
  583. we are setting now - then fail */
  584. if (new_encoder == NULL)
  585. /* don't break so fail path works correct */
  586. fail = 1;
  587. break;
  588. }
  589. }
  590. if (new_encoder != connector->encoder) {
  591. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  592. mode_changed = true;
  593. /* If the encoder is reused for another connector, then
  594. * the appropriate crtc will be set later.
  595. */
  596. if (connector->encoder)
  597. connector->encoder->crtc = NULL;
  598. connector->encoder = new_encoder;
  599. }
  600. }
  601. if (fail) {
  602. ret = -EINVAL;
  603. goto fail;
  604. }
  605. count = 0;
  606. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  607. if (!connector->encoder)
  608. continue;
  609. if (connector->encoder->crtc == set->crtc)
  610. new_crtc = NULL;
  611. else
  612. new_crtc = connector->encoder->crtc;
  613. for (ro = 0; ro < set->num_connectors; ro++) {
  614. if (set->connectors[ro] == connector)
  615. new_crtc = set->crtc;
  616. }
  617. /* Make sure the new CRTC will work with the encoder */
  618. if (new_crtc &&
  619. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  620. ret = -EINVAL;
  621. goto fail;
  622. }
  623. if (new_crtc != connector->encoder->crtc) {
  624. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  625. mode_changed = true;
  626. connector->encoder->crtc = new_crtc;
  627. }
  628. if (new_crtc) {
  629. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  630. connector->base.id, drm_get_connector_name(connector),
  631. new_crtc->base.id);
  632. } else {
  633. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  634. connector->base.id, drm_get_connector_name(connector));
  635. }
  636. }
  637. /* mode_set_base is not a required function */
  638. if (fb_changed && !crtc_funcs->mode_set_base)
  639. mode_changed = true;
  640. if (mode_changed) {
  641. set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
  642. if (set->crtc->enabled) {
  643. DRM_DEBUG_KMS("attempting to set mode from"
  644. " userspace\n");
  645. drm_mode_debug_printmodeline(set->mode);
  646. old_fb = set->crtc->fb;
  647. set->crtc->fb = set->fb;
  648. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  649. set->x, set->y,
  650. old_fb)) {
  651. DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  652. set->crtc->base.id);
  653. set->crtc->fb = old_fb;
  654. ret = -EINVAL;
  655. goto fail;
  656. }
  657. DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  658. for (i = 0; i < set->num_connectors; i++) {
  659. DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  660. drm_get_connector_name(set->connectors[i]));
  661. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  662. }
  663. }
  664. drm_helper_disable_unused_functions(dev);
  665. } else if (fb_changed) {
  666. set->crtc->x = set->x;
  667. set->crtc->y = set->y;
  668. old_fb = set->crtc->fb;
  669. if (set->crtc->fb != set->fb)
  670. set->crtc->fb = set->fb;
  671. ret = crtc_funcs->mode_set_base(set->crtc,
  672. set->x, set->y, old_fb);
  673. if (ret != 0) {
  674. set->crtc->fb = old_fb;
  675. goto fail;
  676. }
  677. }
  678. kfree(save_connectors);
  679. kfree(save_encoders);
  680. kfree(save_crtcs);
  681. return 0;
  682. fail:
  683. /* Restore all previous data. */
  684. count = 0;
  685. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  686. *crtc = save_crtcs[count++];
  687. }
  688. count = 0;
  689. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  690. *encoder = save_encoders[count++];
  691. }
  692. count = 0;
  693. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  694. *connector = save_connectors[count++];
  695. }
  696. /* Try to restore the config */
  697. if (mode_changed &&
  698. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  699. save_set.y, save_set.fb))
  700. DRM_ERROR("failed to restore config after modeset failure\n");
  701. kfree(save_connectors);
  702. kfree(save_encoders);
  703. kfree(save_crtcs);
  704. return ret;
  705. }
  706. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  707. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  708. {
  709. int dpms = DRM_MODE_DPMS_OFF;
  710. struct drm_connector *connector;
  711. struct drm_device *dev = encoder->dev;
  712. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  713. if (connector->encoder == encoder)
  714. if (connector->dpms < dpms)
  715. dpms = connector->dpms;
  716. return dpms;
  717. }
  718. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  719. {
  720. int dpms = DRM_MODE_DPMS_OFF;
  721. struct drm_connector *connector;
  722. struct drm_device *dev = crtc->dev;
  723. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  724. if (connector->encoder && connector->encoder->crtc == crtc)
  725. if (connector->dpms < dpms)
  726. dpms = connector->dpms;
  727. return dpms;
  728. }
  729. /**
  730. * drm_helper_connector_dpms() - connector dpms helper implementation
  731. * @connector: affected connector
  732. * @mode: DPMS mode
  733. *
  734. * This is the main helper function provided by the crtc helper framework for
  735. * implementing the DPMS connector attribute. It computes the new desired DPMS
  736. * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  737. * callback provided by the driver appropriately.
  738. */
  739. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  740. {
  741. struct drm_encoder *encoder = connector->encoder;
  742. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  743. int old_dpms;
  744. if (mode == connector->dpms)
  745. return;
  746. old_dpms = connector->dpms;
  747. connector->dpms = mode;
  748. /* from off to on, do crtc then encoder */
  749. if (mode < old_dpms) {
  750. if (crtc) {
  751. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  752. if (crtc_funcs->dpms)
  753. (*crtc_funcs->dpms) (crtc,
  754. drm_helper_choose_crtc_dpms(crtc));
  755. }
  756. if (encoder) {
  757. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  758. if (encoder_funcs->dpms)
  759. (*encoder_funcs->dpms) (encoder,
  760. drm_helper_choose_encoder_dpms(encoder));
  761. }
  762. }
  763. /* from on to off, do encoder then crtc */
  764. if (mode > old_dpms) {
  765. if (encoder) {
  766. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  767. if (encoder_funcs->dpms)
  768. (*encoder_funcs->dpms) (encoder,
  769. drm_helper_choose_encoder_dpms(encoder));
  770. }
  771. if (crtc) {
  772. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  773. if (crtc_funcs->dpms)
  774. (*crtc_funcs->dpms) (crtc,
  775. drm_helper_choose_crtc_dpms(crtc));
  776. }
  777. }
  778. return;
  779. }
  780. EXPORT_SYMBOL(drm_helper_connector_dpms);
  781. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  782. struct drm_mode_fb_cmd2 *mode_cmd)
  783. {
  784. int i;
  785. fb->width = mode_cmd->width;
  786. fb->height = mode_cmd->height;
  787. for (i = 0; i < 4; i++) {
  788. fb->pitches[i] = mode_cmd->pitches[i];
  789. fb->offsets[i] = mode_cmd->offsets[i];
  790. }
  791. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  792. &fb->bits_per_pixel);
  793. fb->pixel_format = mode_cmd->pixel_format;
  794. return 0;
  795. }
  796. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  797. int drm_helper_resume_force_mode(struct drm_device *dev)
  798. {
  799. struct drm_crtc *crtc;
  800. struct drm_encoder *encoder;
  801. struct drm_encoder_helper_funcs *encoder_funcs;
  802. struct drm_crtc_helper_funcs *crtc_funcs;
  803. int ret;
  804. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  805. if (!crtc->enabled)
  806. continue;
  807. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  808. crtc->x, crtc->y, crtc->fb);
  809. if (ret == false)
  810. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  811. /* Turn off outputs that were already powered off */
  812. if (drm_helper_choose_crtc_dpms(crtc)) {
  813. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  814. if(encoder->crtc != crtc)
  815. continue;
  816. encoder_funcs = encoder->helper_private;
  817. if (encoder_funcs->dpms)
  818. (*encoder_funcs->dpms) (encoder,
  819. drm_helper_choose_encoder_dpms(encoder));
  820. }
  821. crtc_funcs = crtc->helper_private;
  822. if (crtc_funcs->dpms)
  823. (*crtc_funcs->dpms) (crtc,
  824. drm_helper_choose_crtc_dpms(crtc));
  825. }
  826. }
  827. /* disable the unused connectors while restoring the modesetting */
  828. drm_helper_disable_unused_functions(dev);
  829. return 0;
  830. }
  831. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  832. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  833. {
  834. /* send a uevent + call fbdev */
  835. drm_sysfs_hotplug_event(dev);
  836. if (dev->mode_config.funcs->output_poll_changed)
  837. dev->mode_config.funcs->output_poll_changed(dev);
  838. }
  839. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  840. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  841. static void output_poll_execute(struct work_struct *work)
  842. {
  843. struct delayed_work *delayed_work = to_delayed_work(work);
  844. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  845. struct drm_connector *connector;
  846. enum drm_connector_status old_status;
  847. bool repoll = false, changed = false;
  848. if (!drm_kms_helper_poll)
  849. return;
  850. mutex_lock(&dev->mode_config.mutex);
  851. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  852. /* Ignore forced connectors. */
  853. if (connector->force)
  854. continue;
  855. /* Ignore HPD capable connectors and connectors where we don't
  856. * want any hotplug detection at all for polling. */
  857. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  858. continue;
  859. repoll = true;
  860. old_status = connector->status;
  861. /* if we are connected and don't want to poll for disconnect
  862. skip it */
  863. if (old_status == connector_status_connected &&
  864. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  865. continue;
  866. connector->status = connector->funcs->detect(connector, false);
  867. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
  868. connector->base.id,
  869. drm_get_connector_name(connector),
  870. old_status, connector->status);
  871. if (old_status != connector->status)
  872. changed = true;
  873. }
  874. mutex_unlock(&dev->mode_config.mutex);
  875. if (changed)
  876. drm_kms_helper_hotplug_event(dev);
  877. if (repoll)
  878. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  879. }
  880. void drm_kms_helper_poll_disable(struct drm_device *dev)
  881. {
  882. if (!dev->mode_config.poll_enabled)
  883. return;
  884. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  885. }
  886. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  887. void drm_kms_helper_poll_enable(struct drm_device *dev)
  888. {
  889. bool poll = false;
  890. struct drm_connector *connector;
  891. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  892. return;
  893. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  894. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  895. DRM_CONNECTOR_POLL_DISCONNECT))
  896. poll = true;
  897. }
  898. if (poll)
  899. schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  900. }
  901. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  902. void drm_kms_helper_poll_init(struct drm_device *dev)
  903. {
  904. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  905. dev->mode_config.poll_enabled = true;
  906. drm_kms_helper_poll_enable(dev);
  907. }
  908. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  909. void drm_kms_helper_poll_fini(struct drm_device *dev)
  910. {
  911. drm_kms_helper_poll_disable(dev);
  912. }
  913. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  914. void drm_helper_hpd_irq_event(struct drm_device *dev)
  915. {
  916. struct drm_connector *connector;
  917. enum drm_connector_status old_status;
  918. bool changed = false;
  919. if (!dev->mode_config.poll_enabled)
  920. return;
  921. mutex_lock(&dev->mode_config.mutex);
  922. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  923. /* Only handle HPD capable connectors. */
  924. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  925. continue;
  926. old_status = connector->status;
  927. connector->status = connector->funcs->detect(connector, false);
  928. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
  929. connector->base.id,
  930. drm_get_connector_name(connector),
  931. old_status, connector->status);
  932. if (old_status != connector->status)
  933. changed = true;
  934. }
  935. mutex_unlock(&dev->mode_config.mutex);
  936. if (changed)
  937. drm_kms_helper_hotplug_event(dev);
  938. }
  939. EXPORT_SYMBOL(drm_helper_hpd_irq_event);