drm_crtc_helper.c 31 KB

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