drm_crtc_helper.c 26 KB

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