drm_crtc_helper.c 27 KB

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