drm_crtc_helper.c 25 KB

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