drm_crtc_helper.c 25 KB

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