drm_crtc_helper.c 29 KB

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