drm_crtc_helper.c 30 KB

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