drm_crtc_helper.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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;
  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. DRM_DEBUG_KMS("\n");
  413. if (!set)
  414. return -EINVAL;
  415. if (!set->crtc)
  416. return -EINVAL;
  417. if (!set->crtc->helper_private)
  418. return -EINVAL;
  419. crtc_funcs = set->crtc->helper_private;
  420. if (set->fb) {
  421. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  422. set->crtc->base.id, set->fb->base.id,
  423. (int)set->num_connectors, set->x, set->y);
  424. } else {
  425. DRM_DEBUG_KMS("[CRTC:%d] [NOFB] #connectors=%d (x y) (%i %i)\n",
  426. set->crtc->base.id, (int)set->num_connectors,
  427. set->x, set->y);
  428. }
  429. dev = set->crtc->dev;
  430. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  431. * connector data. */
  432. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  433. sizeof(struct drm_crtc), GFP_KERNEL);
  434. if (!save_crtcs)
  435. return -ENOMEM;
  436. save_encoders = kzalloc(dev->mode_config.num_encoder *
  437. sizeof(struct drm_encoder), GFP_KERNEL);
  438. if (!save_encoders) {
  439. kfree(save_crtcs);
  440. return -ENOMEM;
  441. }
  442. save_connectors = kzalloc(dev->mode_config.num_connector *
  443. sizeof(struct drm_connector), GFP_KERNEL);
  444. if (!save_connectors) {
  445. kfree(save_crtcs);
  446. kfree(save_encoders);
  447. return -ENOMEM;
  448. }
  449. /* Copy data. Note that driver private data is not affected.
  450. * Should anything bad happen only the expected state is
  451. * restored, not the drivers personal bookkeeping.
  452. */
  453. count = 0;
  454. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  455. save_crtcs[count++] = *crtc;
  456. }
  457. count = 0;
  458. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  459. save_encoders[count++] = *encoder;
  460. }
  461. count = 0;
  462. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  463. save_connectors[count++] = *connector;
  464. }
  465. /* We should be able to check here if the fb has the same properties
  466. * and then just flip_or_move it */
  467. if (set->crtc->fb != set->fb) {
  468. /* If we have no fb then treat it as a full mode set */
  469. if (set->crtc->fb == NULL) {
  470. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  471. mode_changed = true;
  472. } else if (set->fb == NULL) {
  473. mode_changed = true;
  474. } else
  475. fb_changed = true;
  476. }
  477. if (set->x != set->crtc->x || set->y != set->crtc->y)
  478. fb_changed = true;
  479. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  480. DRM_DEBUG_KMS("modes are different, full mode set\n");
  481. drm_mode_debug_printmodeline(&set->crtc->mode);
  482. drm_mode_debug_printmodeline(set->mode);
  483. mode_changed = true;
  484. }
  485. /* a) traverse passed in connector list and get encoders for them */
  486. count = 0;
  487. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  488. struct drm_connector_helper_funcs *connector_funcs =
  489. connector->helper_private;
  490. new_encoder = connector->encoder;
  491. for (ro = 0; ro < set->num_connectors; ro++) {
  492. if (set->connectors[ro] == connector) {
  493. new_encoder = connector_funcs->best_encoder(connector);
  494. /* if we can't get an encoder for a connector
  495. we are setting now - then fail */
  496. if (new_encoder == NULL)
  497. /* don't break so fail path works correct */
  498. fail = 1;
  499. break;
  500. }
  501. }
  502. if (new_encoder != connector->encoder) {
  503. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  504. mode_changed = true;
  505. /* If the encoder is reused for another connector, then
  506. * the appropriate crtc will be set later.
  507. */
  508. if (connector->encoder)
  509. connector->encoder->crtc = NULL;
  510. connector->encoder = new_encoder;
  511. }
  512. }
  513. if (fail) {
  514. ret = -EINVAL;
  515. goto fail;
  516. }
  517. count = 0;
  518. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  519. if (!connector->encoder)
  520. continue;
  521. if (connector->encoder->crtc == set->crtc)
  522. new_crtc = NULL;
  523. else
  524. new_crtc = connector->encoder->crtc;
  525. for (ro = 0; ro < set->num_connectors; ro++) {
  526. if (set->connectors[ro] == connector)
  527. new_crtc = set->crtc;
  528. }
  529. /* Make sure the new CRTC will work with the encoder */
  530. if (new_crtc &&
  531. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  532. ret = -EINVAL;
  533. goto fail;
  534. }
  535. if (new_crtc != connector->encoder->crtc) {
  536. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  537. mode_changed = true;
  538. connector->encoder->crtc = new_crtc;
  539. }
  540. if (new_crtc) {
  541. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  542. connector->base.id, drm_get_connector_name(connector),
  543. new_crtc->base.id);
  544. } else {
  545. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  546. connector->base.id, drm_get_connector_name(connector));
  547. }
  548. }
  549. /* mode_set_base is not a required function */
  550. if (fb_changed && !crtc_funcs->mode_set_base)
  551. mode_changed = true;
  552. if (mode_changed) {
  553. set->crtc->enabled = (set->mode != NULL);
  554. if (set->mode != NULL) {
  555. DRM_DEBUG_KMS("attempting to set mode from"
  556. " userspace\n");
  557. drm_mode_debug_printmodeline(set->mode);
  558. old_fb = set->crtc->fb;
  559. set->crtc->fb = set->fb;
  560. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  561. set->x, set->y,
  562. old_fb)) {
  563. DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  564. set->crtc->base.id);
  565. ret = -EINVAL;
  566. goto fail;
  567. }
  568. }
  569. drm_helper_disable_unused_functions(dev);
  570. } else if (fb_changed) {
  571. set->crtc->x = set->x;
  572. set->crtc->y = set->y;
  573. old_fb = set->crtc->fb;
  574. if (set->crtc->fb != set->fb)
  575. set->crtc->fb = set->fb;
  576. ret = crtc_funcs->mode_set_base(set->crtc,
  577. set->x, set->y, old_fb);
  578. if (ret != 0)
  579. goto fail;
  580. }
  581. kfree(save_connectors);
  582. kfree(save_encoders);
  583. kfree(save_crtcs);
  584. return 0;
  585. fail:
  586. /* Restore all previous data. */
  587. count = 0;
  588. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  589. *crtc = save_crtcs[count++];
  590. }
  591. count = 0;
  592. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  593. *encoder = save_encoders[count++];
  594. }
  595. count = 0;
  596. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  597. *connector = save_connectors[count++];
  598. }
  599. kfree(save_connectors);
  600. kfree(save_encoders);
  601. kfree(save_crtcs);
  602. return ret;
  603. }
  604. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  605. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  606. {
  607. int dpms = DRM_MODE_DPMS_OFF;
  608. struct drm_connector *connector;
  609. struct drm_device *dev = encoder->dev;
  610. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  611. if (connector->encoder == encoder)
  612. if (connector->dpms < dpms)
  613. dpms = connector->dpms;
  614. return dpms;
  615. }
  616. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  617. {
  618. int dpms = DRM_MODE_DPMS_OFF;
  619. struct drm_connector *connector;
  620. struct drm_device *dev = crtc->dev;
  621. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  622. if (connector->encoder && connector->encoder->crtc == crtc)
  623. if (connector->dpms < dpms)
  624. dpms = connector->dpms;
  625. return dpms;
  626. }
  627. /**
  628. * drm_helper_connector_dpms
  629. * @connector affected connector
  630. * @mode DPMS mode
  631. *
  632. * Calls the low-level connector DPMS function, then
  633. * calls appropriate encoder and crtc DPMS functions as well
  634. */
  635. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  636. {
  637. struct drm_encoder *encoder = connector->encoder;
  638. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  639. int old_dpms;
  640. if (mode == connector->dpms)
  641. return;
  642. old_dpms = connector->dpms;
  643. connector->dpms = mode;
  644. /* from off to on, do crtc then encoder */
  645. if (mode < old_dpms) {
  646. if (crtc) {
  647. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  648. if (crtc_funcs->dpms)
  649. (*crtc_funcs->dpms) (crtc,
  650. drm_helper_choose_crtc_dpms(crtc));
  651. }
  652. if (encoder) {
  653. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  654. if (encoder_funcs->dpms)
  655. (*encoder_funcs->dpms) (encoder,
  656. drm_helper_choose_encoder_dpms(encoder));
  657. }
  658. }
  659. /* from on to off, do encoder then crtc */
  660. if (mode > old_dpms) {
  661. if (encoder) {
  662. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  663. if (encoder_funcs->dpms)
  664. (*encoder_funcs->dpms) (encoder,
  665. drm_helper_choose_encoder_dpms(encoder));
  666. }
  667. if (crtc) {
  668. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  669. if (crtc_funcs->dpms)
  670. (*crtc_funcs->dpms) (crtc,
  671. drm_helper_choose_crtc_dpms(crtc));
  672. }
  673. }
  674. return;
  675. }
  676. EXPORT_SYMBOL(drm_helper_connector_dpms);
  677. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  678. struct drm_mode_fb_cmd *mode_cmd)
  679. {
  680. fb->width = mode_cmd->width;
  681. fb->height = mode_cmd->height;
  682. fb->pitch = mode_cmd->pitch;
  683. fb->bits_per_pixel = mode_cmd->bpp;
  684. fb->depth = mode_cmd->depth;
  685. return 0;
  686. }
  687. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  688. int drm_helper_resume_force_mode(struct drm_device *dev)
  689. {
  690. struct drm_crtc *crtc;
  691. struct drm_encoder *encoder;
  692. struct drm_encoder_helper_funcs *encoder_funcs;
  693. struct drm_crtc_helper_funcs *crtc_funcs;
  694. int ret;
  695. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  696. if (!crtc->enabled)
  697. continue;
  698. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  699. crtc->x, crtc->y, crtc->fb);
  700. if (ret == false)
  701. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  702. /* Turn off outputs that were already powered off */
  703. if (drm_helper_choose_crtc_dpms(crtc)) {
  704. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  705. if(encoder->crtc != crtc)
  706. continue;
  707. encoder_funcs = encoder->helper_private;
  708. if (encoder_funcs->dpms)
  709. (*encoder_funcs->dpms) (encoder,
  710. drm_helper_choose_encoder_dpms(encoder));
  711. }
  712. crtc_funcs = crtc->helper_private;
  713. if (crtc_funcs->dpms)
  714. (*crtc_funcs->dpms) (crtc,
  715. drm_helper_choose_crtc_dpms(crtc));
  716. }
  717. }
  718. /* disable the unused connectors while restoring the modesetting */
  719. drm_helper_disable_unused_functions(dev);
  720. return 0;
  721. }
  722. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  723. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  724. static void output_poll_execute(struct work_struct *work)
  725. {
  726. struct delayed_work *delayed_work = to_delayed_work(work);
  727. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  728. struct drm_connector *connector;
  729. enum drm_connector_status old_status, status;
  730. bool repoll = false, changed = false;
  731. if (!drm_kms_helper_poll)
  732. return;
  733. mutex_lock(&dev->mode_config.mutex);
  734. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  735. /* if this is HPD or polled don't check it -
  736. TV out for instance */
  737. if (!connector->polled)
  738. continue;
  739. else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
  740. repoll = true;
  741. old_status = connector->status;
  742. /* if we are connected and don't want to poll for disconnect
  743. skip it */
  744. if (old_status == connector_status_connected &&
  745. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
  746. !(connector->polled & DRM_CONNECTOR_POLL_HPD))
  747. continue;
  748. status = connector->funcs->detect(connector, false);
  749. if (old_status != status)
  750. changed = true;
  751. }
  752. mutex_unlock(&dev->mode_config.mutex);
  753. if (changed) {
  754. /* send a uevent + call fbdev */
  755. drm_sysfs_hotplug_event(dev);
  756. if (dev->mode_config.funcs->output_poll_changed)
  757. dev->mode_config.funcs->output_poll_changed(dev);
  758. }
  759. if (repoll)
  760. queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
  761. }
  762. void drm_kms_helper_poll_disable(struct drm_device *dev)
  763. {
  764. if (!dev->mode_config.poll_enabled)
  765. return;
  766. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  767. }
  768. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  769. void drm_kms_helper_poll_enable(struct drm_device *dev)
  770. {
  771. bool poll = false;
  772. struct drm_connector *connector;
  773. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  774. return;
  775. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  776. if (connector->polled)
  777. poll = true;
  778. }
  779. if (poll)
  780. queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  781. }
  782. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  783. void drm_kms_helper_poll_init(struct drm_device *dev)
  784. {
  785. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  786. dev->mode_config.poll_enabled = true;
  787. drm_kms_helper_poll_enable(dev);
  788. }
  789. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  790. void drm_kms_helper_poll_fini(struct drm_device *dev)
  791. {
  792. drm_kms_helper_poll_disable(dev);
  793. }
  794. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  795. void drm_helper_hpd_irq_event(struct drm_device *dev)
  796. {
  797. if (!dev->mode_config.poll_enabled)
  798. return;
  799. /* kill timer and schedule immediate execution, this doesn't block */
  800. cancel_delayed_work(&dev->mode_config.output_poll_work);
  801. if (drm_kms_helper_poll)
  802. queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, 0);
  803. }
  804. EXPORT_SYMBOL(drm_helper_hpd_irq_event);