drm_crtc_helper.c 27 KB

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