drm_crtc_helper.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. if (crtc_funcs->disable)
  219. (*crtc_funcs->disable)(crtc);
  220. else
  221. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  222. crtc->fb = NULL;
  223. }
  224. }
  225. }
  226. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  227. /**
  228. * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  229. * @encoder: encoder to test
  230. * @crtc: crtc to test
  231. *
  232. * Return false if @encoder can't be driven by @crtc, true otherwise.
  233. */
  234. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  235. struct drm_crtc *crtc)
  236. {
  237. struct drm_device *dev;
  238. struct drm_crtc *tmp;
  239. int crtc_mask = 1;
  240. WARN(!crtc, "checking null crtc?");
  241. dev = crtc->dev;
  242. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  243. if (tmp == crtc)
  244. break;
  245. crtc_mask <<= 1;
  246. }
  247. if (encoder->possible_crtcs & crtc_mask)
  248. return true;
  249. return false;
  250. }
  251. /*
  252. * Check the CRTC we're going to map each output to vs. its current
  253. * CRTC. If they don't match, we have to disable the output and the CRTC
  254. * since the driver will have to re-route things.
  255. */
  256. static void
  257. drm_crtc_prepare_encoders(struct drm_device *dev)
  258. {
  259. struct drm_encoder_helper_funcs *encoder_funcs;
  260. struct drm_encoder *encoder;
  261. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  262. encoder_funcs = encoder->helper_private;
  263. /* Disable unused encoders */
  264. if (encoder->crtc == NULL)
  265. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  266. /* Disable encoders whose CRTC is about to change */
  267. if (encoder_funcs->get_crtc &&
  268. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  269. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  270. }
  271. }
  272. /**
  273. * drm_crtc_set_mode - set a mode
  274. * @crtc: CRTC to program
  275. * @mode: mode to use
  276. * @x: width of mode
  277. * @y: height of mode
  278. *
  279. * LOCKING:
  280. * Caller must hold mode config lock.
  281. *
  282. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  283. * to fixup or reject the mode prior to trying to set it.
  284. *
  285. * RETURNS:
  286. * True if the mode was set successfully, or false otherwise.
  287. */
  288. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  289. struct drm_display_mode *mode,
  290. int x, int y,
  291. struct drm_framebuffer *old_fb)
  292. {
  293. struct drm_device *dev = crtc->dev;
  294. struct drm_display_mode *adjusted_mode, saved_mode;
  295. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  296. struct drm_encoder_helper_funcs *encoder_funcs;
  297. int saved_x, saved_y;
  298. struct drm_encoder *encoder;
  299. bool ret = true;
  300. adjusted_mode = drm_mode_duplicate(dev, mode);
  301. crtc->enabled = drm_helper_crtc_in_use(crtc);
  302. if (!crtc->enabled)
  303. return true;
  304. saved_mode = crtc->mode;
  305. saved_x = crtc->x;
  306. saved_y = crtc->y;
  307. /* Update crtc values up front so the driver can rely on them for mode
  308. * setting.
  309. */
  310. crtc->mode = *mode;
  311. crtc->x = x;
  312. crtc->y = y;
  313. /* Pass our mode to the connectors and the CRTC to give them a chance to
  314. * adjust it according to limitations or connector properties, and also
  315. * a chance to reject the mode entirely.
  316. */
  317. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  318. if (encoder->crtc != crtc)
  319. continue;
  320. encoder_funcs = encoder->helper_private;
  321. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  322. adjusted_mode))) {
  323. goto done;
  324. }
  325. }
  326. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  327. goto done;
  328. }
  329. /* Prepare the encoders and CRTCs before setting the mode. */
  330. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  331. if (encoder->crtc != crtc)
  332. continue;
  333. encoder_funcs = encoder->helper_private;
  334. /* Disable the encoders as the first thing we do. */
  335. encoder_funcs->prepare(encoder);
  336. }
  337. drm_crtc_prepare_encoders(dev);
  338. crtc_funcs->prepare(crtc);
  339. /* Set up the DPLL and any encoders state that needs to adjust or depend
  340. * on the DPLL.
  341. */
  342. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  343. if (!ret)
  344. goto done;
  345. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  346. if (encoder->crtc != crtc)
  347. continue;
  348. DRM_DEBUG("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
  349. mode->name, mode->base.id);
  350. encoder_funcs = encoder->helper_private;
  351. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  352. }
  353. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  354. crtc_funcs->commit(crtc);
  355. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  356. if (encoder->crtc != crtc)
  357. continue;
  358. encoder_funcs = encoder->helper_private;
  359. encoder_funcs->commit(encoder);
  360. }
  361. /* XXX free adjustedmode */
  362. drm_mode_destroy(dev, adjusted_mode);
  363. /* FIXME: add subpixel order */
  364. done:
  365. if (!ret) {
  366. crtc->mode = saved_mode;
  367. crtc->x = saved_x;
  368. crtc->y = saved_y;
  369. }
  370. return ret;
  371. }
  372. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  373. /**
  374. * drm_crtc_helper_set_config - set a new config from userspace
  375. * @crtc: CRTC to setup
  376. * @crtc_info: user provided configuration
  377. * @new_mode: new mode to set
  378. * @connector_set: set of connectors for the new config
  379. * @fb: new framebuffer
  380. *
  381. * LOCKING:
  382. * Caller must hold mode config lock.
  383. *
  384. * Setup a new configuration, provided by the user in @crtc_info, and enable
  385. * it.
  386. *
  387. * RETURNS:
  388. * Zero. (FIXME)
  389. */
  390. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  391. {
  392. struct drm_device *dev;
  393. struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  394. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  395. struct drm_framebuffer *old_fb = NULL;
  396. bool mode_changed = false; /* if true do a full mode set */
  397. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  398. struct drm_connector *save_connectors, *connector;
  399. int count = 0, ro, fail = 0;
  400. struct drm_crtc_helper_funcs *crtc_funcs;
  401. int ret = 0;
  402. DRM_DEBUG_KMS("\n");
  403. if (!set)
  404. return -EINVAL;
  405. if (!set->crtc)
  406. return -EINVAL;
  407. if (!set->crtc->helper_private)
  408. return -EINVAL;
  409. crtc_funcs = set->crtc->helper_private;
  410. DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
  411. " %d (x, y) (%i, %i)\n",
  412. set->crtc, set->crtc->base.id, set->fb, set->connectors,
  413. (int)set->num_connectors, set->x, set->y);
  414. dev = set->crtc->dev;
  415. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  416. * connector data. */
  417. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  418. sizeof(struct drm_crtc), GFP_KERNEL);
  419. if (!save_crtcs)
  420. return -ENOMEM;
  421. save_encoders = kzalloc(dev->mode_config.num_encoder *
  422. sizeof(struct drm_encoder), GFP_KERNEL);
  423. if (!save_encoders) {
  424. kfree(save_crtcs);
  425. return -ENOMEM;
  426. }
  427. save_connectors = kzalloc(dev->mode_config.num_connector *
  428. sizeof(struct drm_connector), GFP_KERNEL);
  429. if (!save_connectors) {
  430. kfree(save_crtcs);
  431. kfree(save_encoders);
  432. return -ENOMEM;
  433. }
  434. /* Copy data. Note that driver private data is not affected.
  435. * Should anything bad happen only the expected state is
  436. * restored, not the drivers personal bookkeeping.
  437. */
  438. count = 0;
  439. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  440. save_crtcs[count++] = *crtc;
  441. }
  442. count = 0;
  443. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  444. save_encoders[count++] = *encoder;
  445. }
  446. count = 0;
  447. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  448. save_connectors[count++] = *connector;
  449. }
  450. /* We should be able to check here if the fb has the same properties
  451. * and then just flip_or_move it */
  452. if (set->crtc->fb != set->fb) {
  453. /* If we have no fb then treat it as a full mode set */
  454. if (set->crtc->fb == NULL) {
  455. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  456. mode_changed = true;
  457. } else if (set->fb == NULL) {
  458. mode_changed = true;
  459. } else
  460. fb_changed = true;
  461. }
  462. if (set->x != set->crtc->x || set->y != set->crtc->y)
  463. fb_changed = true;
  464. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  465. DRM_DEBUG_KMS("modes are different, full mode set\n");
  466. drm_mode_debug_printmodeline(&set->crtc->mode);
  467. drm_mode_debug_printmodeline(set->mode);
  468. mode_changed = true;
  469. }
  470. /* a) traverse passed in connector list and get encoders for them */
  471. count = 0;
  472. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  473. struct drm_connector_helper_funcs *connector_funcs =
  474. connector->helper_private;
  475. new_encoder = connector->encoder;
  476. for (ro = 0; ro < set->num_connectors; ro++) {
  477. if (set->connectors[ro] == connector) {
  478. new_encoder = connector_funcs->best_encoder(connector);
  479. /* if we can't get an encoder for a connector
  480. we are setting now - then fail */
  481. if (new_encoder == NULL)
  482. /* don't break so fail path works correct */
  483. fail = 1;
  484. break;
  485. }
  486. }
  487. if (new_encoder != connector->encoder) {
  488. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  489. mode_changed = true;
  490. /* If the encoder is reused for another connector, then
  491. * the appropriate crtc will be set later.
  492. */
  493. if (connector->encoder)
  494. connector->encoder->crtc = NULL;
  495. connector->encoder = new_encoder;
  496. }
  497. }
  498. if (fail) {
  499. ret = -EINVAL;
  500. goto fail;
  501. }
  502. count = 0;
  503. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  504. if (!connector->encoder)
  505. continue;
  506. if (connector->encoder->crtc == set->crtc)
  507. new_crtc = NULL;
  508. else
  509. new_crtc = connector->encoder->crtc;
  510. for (ro = 0; ro < set->num_connectors; ro++) {
  511. if (set->connectors[ro] == connector)
  512. new_crtc = set->crtc;
  513. }
  514. /* Make sure the new CRTC will work with the encoder */
  515. if (new_crtc &&
  516. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  517. ret = -EINVAL;
  518. goto fail;
  519. }
  520. if (new_crtc != connector->encoder->crtc) {
  521. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  522. mode_changed = true;
  523. connector->encoder->crtc = new_crtc;
  524. }
  525. DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
  526. connector->base.id, new_crtc);
  527. }
  528. /* mode_set_base is not a required function */
  529. if (fb_changed && !crtc_funcs->mode_set_base)
  530. mode_changed = true;
  531. if (mode_changed) {
  532. old_fb = set->crtc->fb;
  533. set->crtc->fb = set->fb;
  534. set->crtc->enabled = (set->mode != NULL);
  535. if (set->mode != NULL) {
  536. DRM_DEBUG_KMS("attempting to set mode from"
  537. " userspace\n");
  538. drm_mode_debug_printmodeline(set->mode);
  539. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  540. set->x, set->y,
  541. old_fb)) {
  542. DRM_ERROR("failed to set mode on crtc %p\n",
  543. set->crtc);
  544. ret = -EINVAL;
  545. goto fail;
  546. }
  547. }
  548. drm_helper_disable_unused_functions(dev);
  549. } else if (fb_changed) {
  550. set->crtc->x = set->x;
  551. set->crtc->y = set->y;
  552. old_fb = set->crtc->fb;
  553. if (set->crtc->fb != set->fb)
  554. set->crtc->fb = set->fb;
  555. ret = crtc_funcs->mode_set_base(set->crtc,
  556. set->x, set->y, old_fb);
  557. if (ret != 0)
  558. goto fail;
  559. }
  560. kfree(save_connectors);
  561. kfree(save_encoders);
  562. kfree(save_crtcs);
  563. return 0;
  564. fail:
  565. /* Restore all previous data. */
  566. count = 0;
  567. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  568. *crtc = save_crtcs[count++];
  569. }
  570. count = 0;
  571. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  572. *encoder = save_encoders[count++];
  573. }
  574. count = 0;
  575. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  576. *connector = save_connectors[count++];
  577. }
  578. kfree(save_connectors);
  579. kfree(save_encoders);
  580. kfree(save_crtcs);
  581. return ret;
  582. }
  583. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  584. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  585. {
  586. int dpms = DRM_MODE_DPMS_OFF;
  587. struct drm_connector *connector;
  588. struct drm_device *dev = encoder->dev;
  589. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  590. if (connector->encoder == encoder)
  591. if (connector->dpms < dpms)
  592. dpms = connector->dpms;
  593. return dpms;
  594. }
  595. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  596. {
  597. int dpms = DRM_MODE_DPMS_OFF;
  598. struct drm_connector *connector;
  599. struct drm_device *dev = crtc->dev;
  600. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  601. if (connector->encoder && connector->encoder->crtc == crtc)
  602. if (connector->dpms < dpms)
  603. dpms = connector->dpms;
  604. return dpms;
  605. }
  606. /**
  607. * drm_helper_connector_dpms
  608. * @connector affected connector
  609. * @mode DPMS mode
  610. *
  611. * Calls the low-level connector DPMS function, then
  612. * calls appropriate encoder and crtc DPMS functions as well
  613. */
  614. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  615. {
  616. struct drm_encoder *encoder = connector->encoder;
  617. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  618. int old_dpms;
  619. if (mode == connector->dpms)
  620. return;
  621. old_dpms = connector->dpms;
  622. connector->dpms = mode;
  623. /* from off to on, do crtc then encoder */
  624. if (mode < old_dpms) {
  625. if (crtc) {
  626. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  627. if (crtc_funcs->dpms)
  628. (*crtc_funcs->dpms) (crtc,
  629. drm_helper_choose_crtc_dpms(crtc));
  630. }
  631. if (encoder) {
  632. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  633. if (encoder_funcs->dpms)
  634. (*encoder_funcs->dpms) (encoder,
  635. drm_helper_choose_encoder_dpms(encoder));
  636. }
  637. }
  638. /* from on to off, do encoder then crtc */
  639. if (mode > old_dpms) {
  640. if (encoder) {
  641. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  642. if (encoder_funcs->dpms)
  643. (*encoder_funcs->dpms) (encoder,
  644. drm_helper_choose_encoder_dpms(encoder));
  645. }
  646. if (crtc) {
  647. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  648. if (crtc_funcs->dpms)
  649. (*crtc_funcs->dpms) (crtc,
  650. drm_helper_choose_crtc_dpms(crtc));
  651. }
  652. }
  653. return;
  654. }
  655. EXPORT_SYMBOL(drm_helper_connector_dpms);
  656. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  657. struct drm_mode_fb_cmd *mode_cmd)
  658. {
  659. fb->width = mode_cmd->width;
  660. fb->height = mode_cmd->height;
  661. fb->pitch = mode_cmd->pitch;
  662. fb->bits_per_pixel = mode_cmd->bpp;
  663. fb->depth = mode_cmd->depth;
  664. return 0;
  665. }
  666. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  667. int drm_helper_resume_force_mode(struct drm_device *dev)
  668. {
  669. struct drm_crtc *crtc;
  670. struct drm_encoder *encoder;
  671. struct drm_encoder_helper_funcs *encoder_funcs;
  672. struct drm_crtc_helper_funcs *crtc_funcs;
  673. int ret;
  674. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  675. if (!crtc->enabled)
  676. continue;
  677. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  678. crtc->x, crtc->y, crtc->fb);
  679. if (ret == false)
  680. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  681. /* Turn off outputs that were already powered off */
  682. if (drm_helper_choose_crtc_dpms(crtc)) {
  683. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  684. if(encoder->crtc != crtc)
  685. continue;
  686. encoder_funcs = encoder->helper_private;
  687. if (encoder_funcs->dpms)
  688. (*encoder_funcs->dpms) (encoder,
  689. drm_helper_choose_encoder_dpms(encoder));
  690. crtc_funcs = crtc->helper_private;
  691. if (crtc_funcs->dpms)
  692. (*crtc_funcs->dpms) (crtc,
  693. drm_helper_choose_crtc_dpms(crtc));
  694. }
  695. }
  696. }
  697. /* disable the unused connectors while restoring the modesetting */
  698. drm_helper_disable_unused_functions(dev);
  699. return 0;
  700. }
  701. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  702. static struct slow_work_ops output_poll_ops;
  703. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  704. static void output_poll_execute(struct slow_work *work)
  705. {
  706. struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
  707. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_slow_work);
  708. struct drm_connector *connector;
  709. enum drm_connector_status old_status, status;
  710. bool repoll = false, changed = false;
  711. int ret;
  712. mutex_lock(&dev->mode_config.mutex);
  713. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  714. /* if this is HPD or polled don't check it -
  715. TV out for instance */
  716. if (!connector->polled)
  717. continue;
  718. else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
  719. repoll = true;
  720. old_status = connector->status;
  721. /* if we are connected and don't want to poll for disconnect
  722. skip it */
  723. if (old_status == connector_status_connected &&
  724. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
  725. !(connector->polled & DRM_CONNECTOR_POLL_HPD))
  726. continue;
  727. status = connector->funcs->detect(connector);
  728. if (old_status != status)
  729. changed = true;
  730. }
  731. mutex_unlock(&dev->mode_config.mutex);
  732. if (changed) {
  733. /* send a uevent + call fbdev */
  734. drm_sysfs_hotplug_event(dev);
  735. if (dev->mode_config.funcs->output_poll_changed)
  736. dev->mode_config.funcs->output_poll_changed(dev);
  737. }
  738. if (repoll) {
  739. ret = delayed_slow_work_enqueue(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  740. if (ret)
  741. DRM_ERROR("delayed enqueue failed %d\n", ret);
  742. }
  743. }
  744. void drm_kms_helper_poll_disable(struct drm_device *dev)
  745. {
  746. if (!dev->mode_config.poll_enabled)
  747. return;
  748. delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work);
  749. }
  750. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  751. void drm_kms_helper_poll_enable(struct drm_device *dev)
  752. {
  753. bool poll = false;
  754. struct drm_connector *connector;
  755. int ret;
  756. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  757. if (connector->polled)
  758. poll = true;
  759. }
  760. if (poll) {
  761. ret = delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, DRM_OUTPUT_POLL_PERIOD);
  762. if (ret)
  763. DRM_ERROR("delayed enqueue failed %d\n", ret);
  764. }
  765. }
  766. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  767. void drm_kms_helper_poll_init(struct drm_device *dev)
  768. {
  769. slow_work_register_user(THIS_MODULE);
  770. delayed_slow_work_init(&dev->mode_config.output_poll_slow_work,
  771. &output_poll_ops);
  772. dev->mode_config.poll_enabled = true;
  773. drm_kms_helper_poll_enable(dev);
  774. }
  775. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  776. void drm_kms_helper_poll_fini(struct drm_device *dev)
  777. {
  778. drm_kms_helper_poll_disable(dev);
  779. slow_work_unregister_user(THIS_MODULE);
  780. }
  781. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  782. void drm_helper_hpd_irq_event(struct drm_device *dev)
  783. {
  784. if (!dev->mode_config.poll_enabled)
  785. return;
  786. delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work);
  787. /* schedule a slow work asap */
  788. delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, 0);
  789. }
  790. EXPORT_SYMBOL(drm_helper_hpd_irq_event);
  791. static struct slow_work_ops output_poll_ops = {
  792. .execute = output_poll_execute,
  793. };