drm_crtc_helper.c 25 KB

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