drm_crtc_helper.c 26 KB

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