drm_crtc_helper.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. /*
  35. * Detailed mode info for 800x600@60Hz
  36. */
  37. static struct drm_display_mode std_mode[] = {
  38. { DRM_MODE("800x600", DRM_MODE_TYPE_DEFAULT, 40000, 800, 840,
  39. 968, 1056, 0, 600, 601, 605, 628, 0,
  40. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  41. };
  42. /**
  43. * drm_helper_probe_connector_modes - get complete set of display modes
  44. * @dev: DRM device
  45. * @maxX: max width for modes
  46. * @maxY: max height for modes
  47. *
  48. * LOCKING:
  49. * Caller must hold mode config lock.
  50. *
  51. * Based on @dev's mode_config layout, scan all the connectors and try to detect
  52. * modes on them. Modes will first be added to the connector's probed_modes
  53. * list, then culled (based on validity and the @maxX, @maxY parameters) and
  54. * put into the normal modes list.
  55. *
  56. * Intended to be used either at bootup time or when major configuration
  57. * changes have occurred.
  58. *
  59. * FIXME: take into account monitor limits
  60. */
  61. void drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  62. uint32_t maxX, uint32_t maxY)
  63. {
  64. struct drm_device *dev = connector->dev;
  65. struct drm_display_mode *mode, *t;
  66. struct drm_connector_helper_funcs *connector_funcs =
  67. connector->helper_private;
  68. int ret;
  69. DRM_DEBUG("%s\n", drm_get_connector_name(connector));
  70. /* set all modes to the unverified state */
  71. list_for_each_entry_safe(mode, t, &connector->modes, head)
  72. mode->status = MODE_UNVERIFIED;
  73. connector->status = connector->funcs->detect(connector);
  74. if (connector->status == connector_status_disconnected) {
  75. DRM_DEBUG("%s is disconnected\n",
  76. drm_get_connector_name(connector));
  77. /* TODO set EDID to NULL */
  78. return;
  79. }
  80. ret = (*connector_funcs->get_modes)(connector);
  81. if (ret) {
  82. drm_mode_connector_list_update(connector);
  83. }
  84. if (maxX && maxY)
  85. drm_mode_validate_size(dev, &connector->modes, maxX,
  86. maxY, 0);
  87. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  88. if (mode->status == MODE_OK)
  89. mode->status = connector_funcs->mode_valid(connector,
  90. mode);
  91. }
  92. drm_mode_prune_invalid(dev, &connector->modes, true);
  93. if (list_empty(&connector->modes)) {
  94. struct drm_display_mode *stdmode;
  95. DRM_DEBUG("No valid modes on %s\n",
  96. drm_get_connector_name(connector));
  97. /* Should we do this here ???
  98. * When no valid EDID modes are available we end up
  99. * here and bailed in the past, now we add a standard
  100. * 640x480@60Hz mode and carry on.
  101. */
  102. stdmode = drm_mode_duplicate(dev, &std_mode[0]);
  103. drm_mode_probed_add(connector, stdmode);
  104. drm_mode_list_concat(&connector->probed_modes,
  105. &connector->modes);
  106. DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
  107. drm_get_connector_name(connector));
  108. }
  109. drm_mode_sort(&connector->modes);
  110. DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
  111. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  112. mode->vrefresh = drm_mode_vrefresh(mode);
  113. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  114. drm_mode_debug_printmodeline(mode);
  115. }
  116. }
  117. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  118. void drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
  119. uint32_t maxY)
  120. {
  121. struct drm_connector *connector;
  122. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  123. drm_helper_probe_single_connector_modes(connector, maxX, maxY);
  124. }
  125. }
  126. EXPORT_SYMBOL(drm_helper_probe_connector_modes);
  127. /**
  128. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  129. * @crtc: CRTC to check
  130. *
  131. * LOCKING:
  132. * Caller must hold mode config lock.
  133. *
  134. * Walk @crtc's DRM device's mode_config and see if it's in use.
  135. *
  136. * RETURNS:
  137. * True if @crtc is part of the mode_config, false otherwise.
  138. */
  139. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  140. {
  141. struct drm_encoder *encoder;
  142. struct drm_device *dev = crtc->dev;
  143. /* FIXME: Locking around list access? */
  144. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  145. if (encoder->crtc == crtc)
  146. return true;
  147. return false;
  148. }
  149. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  150. /**
  151. * drm_disable_unused_functions - disable unused objects
  152. * @dev: DRM device
  153. *
  154. * LOCKING:
  155. * Caller must hold mode config lock.
  156. *
  157. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  158. * by calling its dpms function, which should power it off.
  159. */
  160. void drm_helper_disable_unused_functions(struct drm_device *dev)
  161. {
  162. struct drm_encoder *encoder;
  163. struct drm_encoder_helper_funcs *encoder_funcs;
  164. struct drm_crtc *crtc;
  165. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  166. encoder_funcs = encoder->helper_private;
  167. if (!encoder->crtc)
  168. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  169. }
  170. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  171. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  172. crtc->enabled = drm_helper_crtc_in_use(crtc);
  173. if (!crtc->enabled) {
  174. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  175. crtc->fb = NULL;
  176. }
  177. }
  178. }
  179. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  180. static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
  181. {
  182. struct drm_display_mode *mode;
  183. list_for_each_entry(mode, &connector->modes, head) {
  184. if (drm_mode_width(mode) > width ||
  185. drm_mode_height(mode) > height)
  186. continue;
  187. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  188. return mode;
  189. }
  190. return NULL;
  191. }
  192. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  193. {
  194. bool enable;
  195. if (strict) {
  196. enable = connector->status == connector_status_connected;
  197. } else {
  198. enable = connector->status != connector_status_disconnected;
  199. }
  200. return enable;
  201. }
  202. static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
  203. {
  204. bool any_enabled = false;
  205. struct drm_connector *connector;
  206. int i = 0;
  207. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  208. enabled[i] = drm_connector_enabled(connector, true);
  209. any_enabled |= enabled[i];
  210. i++;
  211. }
  212. if (any_enabled)
  213. return;
  214. i = 0;
  215. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  216. enabled[i] = drm_connector_enabled(connector, false);
  217. i++;
  218. }
  219. }
  220. static bool drm_target_preferred(struct drm_device *dev,
  221. struct drm_display_mode **modes,
  222. bool *enabled, int width, int height)
  223. {
  224. struct drm_connector *connector;
  225. int i = 0;
  226. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  227. if (enabled[i] == false) {
  228. i++;
  229. continue;
  230. }
  231. modes[i] = drm_has_preferred_mode(connector, width, height);
  232. if (!modes[i]) {
  233. list_for_each_entry(modes[i], &connector->modes, head)
  234. break;
  235. }
  236. i++;
  237. }
  238. return true;
  239. }
  240. static int drm_pick_crtcs(struct drm_device *dev,
  241. struct drm_crtc **best_crtcs,
  242. struct drm_display_mode **modes,
  243. int n, int width, int height)
  244. {
  245. int c, o;
  246. struct drm_connector *connector;
  247. struct drm_connector_helper_funcs *connector_funcs;
  248. struct drm_encoder *encoder;
  249. struct drm_crtc *best_crtc;
  250. int my_score, best_score, score;
  251. struct drm_crtc **crtcs, *crtc;
  252. if (n == dev->mode_config.num_connector)
  253. return 0;
  254. c = 0;
  255. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  256. if (c == n)
  257. break;
  258. c++;
  259. }
  260. best_crtcs[n] = NULL;
  261. best_crtc = NULL;
  262. best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
  263. if (modes[n] == NULL)
  264. return best_score;
  265. crtcs = kmalloc(dev->mode_config.num_connector *
  266. sizeof(struct drm_crtc *), GFP_KERNEL);
  267. if (!crtcs)
  268. return best_score;
  269. my_score = 1;
  270. if (connector->status == connector_status_connected)
  271. my_score++;
  272. if (drm_has_preferred_mode(connector, width, height))
  273. my_score++;
  274. connector_funcs = connector->helper_private;
  275. encoder = connector_funcs->best_encoder(connector);
  276. if (!encoder)
  277. goto out;
  278. connector->encoder = encoder;
  279. /* select a crtc for this connector and then attempt to configure
  280. remaining connectors */
  281. c = 0;
  282. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  283. if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
  284. c++;
  285. continue;
  286. }
  287. for (o = 0; o < n; o++)
  288. if (best_crtcs[o] == crtc)
  289. break;
  290. if (o < n) {
  291. /* ignore cloning for now */
  292. c++;
  293. continue;
  294. }
  295. crtcs[n] = crtc;
  296. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
  297. score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
  298. width, height);
  299. if (score > best_score) {
  300. best_crtc = crtc;
  301. best_score = score;
  302. memcpy(best_crtcs, crtcs,
  303. dev->mode_config.num_connector *
  304. sizeof(struct drm_crtc *));
  305. }
  306. c++;
  307. }
  308. out:
  309. kfree(crtcs);
  310. return best_score;
  311. }
  312. static void drm_setup_crtcs(struct drm_device *dev)
  313. {
  314. struct drm_crtc **crtcs;
  315. struct drm_display_mode **modes;
  316. struct drm_encoder *encoder;
  317. struct drm_connector *connector;
  318. bool *enabled;
  319. int width, height;
  320. int i, ret;
  321. width = dev->mode_config.max_width;
  322. height = dev->mode_config.max_height;
  323. /* clean out all the encoder/crtc combos */
  324. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  325. encoder->crtc = NULL;
  326. }
  327. crtcs = kcalloc(dev->mode_config.num_connector,
  328. sizeof(struct drm_crtc *), GFP_KERNEL);
  329. modes = kcalloc(dev->mode_config.num_connector,
  330. sizeof(struct drm_display_mode *), GFP_KERNEL);
  331. enabled = kcalloc(dev->mode_config.num_connector,
  332. sizeof(bool), GFP_KERNEL);
  333. drm_enable_connectors(dev, enabled);
  334. ret = drm_target_preferred(dev, modes, enabled, width, height);
  335. if (!ret)
  336. DRM_ERROR("Unable to find initial modes\n");
  337. drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
  338. i = 0;
  339. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  340. struct drm_display_mode *mode = modes[i];
  341. struct drm_crtc *crtc = crtcs[i];
  342. if (connector->encoder == NULL) {
  343. i++;
  344. continue;
  345. }
  346. if (mode && crtc) {
  347. crtc->desired_mode = mode;
  348. connector->encoder->crtc = crtc;
  349. } else
  350. connector->encoder->crtc = NULL;
  351. i++;
  352. }
  353. kfree(crtcs);
  354. kfree(modes);
  355. kfree(enabled);
  356. }
  357. /**
  358. * drm_crtc_set_mode - set a mode
  359. * @crtc: CRTC to program
  360. * @mode: mode to use
  361. * @x: width of mode
  362. * @y: height of mode
  363. *
  364. * LOCKING:
  365. * Caller must hold mode config lock.
  366. *
  367. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  368. * to fixup or reject the mode prior to trying to set it.
  369. *
  370. * RETURNS:
  371. * True if the mode was set successfully, or false otherwise.
  372. */
  373. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  374. struct drm_display_mode *mode,
  375. int x, int y)
  376. {
  377. struct drm_device *dev = crtc->dev;
  378. struct drm_display_mode *adjusted_mode, saved_mode;
  379. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  380. struct drm_encoder_helper_funcs *encoder_funcs;
  381. int saved_x, saved_y;
  382. struct drm_encoder *encoder;
  383. bool ret = true;
  384. adjusted_mode = drm_mode_duplicate(dev, mode);
  385. crtc->enabled = drm_helper_crtc_in_use(crtc);
  386. if (!crtc->enabled)
  387. return true;
  388. saved_mode = crtc->mode;
  389. saved_x = crtc->x;
  390. saved_y = crtc->y;
  391. /* Update crtc values up front so the driver can rely on them for mode
  392. * setting.
  393. */
  394. crtc->mode = *mode;
  395. crtc->x = x;
  396. crtc->y = y;
  397. if (drm_mode_equal(&saved_mode, &crtc->mode)) {
  398. if (saved_x != crtc->x || saved_y != crtc->y) {
  399. crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y);
  400. goto done;
  401. }
  402. }
  403. /* Pass our mode to the connectors and the CRTC to give them a chance to
  404. * adjust it according to limitations or connector properties, and also
  405. * a chance to reject the mode entirely.
  406. */
  407. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  408. if (encoder->crtc != crtc)
  409. continue;
  410. encoder_funcs = encoder->helper_private;
  411. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  412. adjusted_mode))) {
  413. goto done;
  414. }
  415. }
  416. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  417. goto done;
  418. }
  419. /* Prepare the encoders and CRTCs before setting the mode. */
  420. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  421. if (encoder->crtc != crtc)
  422. continue;
  423. encoder_funcs = encoder->helper_private;
  424. /* Disable the encoders as the first thing we do. */
  425. encoder_funcs->prepare(encoder);
  426. }
  427. crtc_funcs->prepare(crtc);
  428. /* Set up the DPLL and any encoders state that needs to adjust or depend
  429. * on the DPLL.
  430. */
  431. crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y);
  432. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  433. if (encoder->crtc != crtc)
  434. continue;
  435. DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
  436. mode->name, mode->base.id);
  437. encoder_funcs = encoder->helper_private;
  438. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  439. }
  440. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  441. crtc_funcs->commit(crtc);
  442. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  443. if (encoder->crtc != crtc)
  444. continue;
  445. encoder_funcs = encoder->helper_private;
  446. encoder_funcs->commit(encoder);
  447. }
  448. /* XXX free adjustedmode */
  449. drm_mode_destroy(dev, adjusted_mode);
  450. /* FIXME: add subpixel order */
  451. done:
  452. if (!ret) {
  453. crtc->mode = saved_mode;
  454. crtc->x = saved_x;
  455. crtc->y = saved_y;
  456. }
  457. return ret;
  458. }
  459. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  460. /**
  461. * drm_crtc_helper_set_config - set a new config from userspace
  462. * @crtc: CRTC to setup
  463. * @crtc_info: user provided configuration
  464. * @new_mode: new mode to set
  465. * @connector_set: set of connectors for the new config
  466. * @fb: new framebuffer
  467. *
  468. * LOCKING:
  469. * Caller must hold mode config lock.
  470. *
  471. * Setup a new configuration, provided by the user in @crtc_info, and enable
  472. * it.
  473. *
  474. * RETURNS:
  475. * Zero. (FIXME)
  476. */
  477. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  478. {
  479. struct drm_device *dev;
  480. struct drm_crtc **save_crtcs, *new_crtc;
  481. struct drm_encoder **save_encoders, *new_encoder;
  482. bool save_enabled;
  483. bool changed = false;
  484. bool flip_or_move = false;
  485. struct drm_connector *connector;
  486. int count = 0, ro, fail = 0;
  487. struct drm_crtc_helper_funcs *crtc_funcs;
  488. int ret = 0;
  489. DRM_DEBUG("\n");
  490. if (!set)
  491. return -EINVAL;
  492. if (!set->crtc)
  493. return -EINVAL;
  494. if (!set->crtc->helper_private)
  495. return -EINVAL;
  496. crtc_funcs = set->crtc->helper_private;
  497. DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
  498. set->crtc, set->crtc->base.id, set->fb, set->connectors,
  499. (int)set->num_connectors, set->x, set->y);
  500. dev = set->crtc->dev;
  501. /* save previous config */
  502. save_enabled = set->crtc->enabled;
  503. /* this is meant to be num_connector not num_crtc */
  504. save_crtcs = kzalloc(dev->mode_config.num_connector *
  505. sizeof(struct drm_crtc *), GFP_KERNEL);
  506. if (!save_crtcs)
  507. return -ENOMEM;
  508. save_encoders = kzalloc(dev->mode_config.num_connector *
  509. sizeof(struct drm_encoders *), GFP_KERNEL);
  510. if (!save_encoders) {
  511. kfree(save_crtcs);
  512. return -ENOMEM;
  513. }
  514. /* We should be able to check here if the fb has the same properties
  515. * and then just flip_or_move it */
  516. if (set->crtc->fb != set->fb) {
  517. /* if we have no fb then its a change not a flip */
  518. if (set->crtc->fb == NULL)
  519. changed = true;
  520. else
  521. flip_or_move = true;
  522. }
  523. if (set->x != set->crtc->x || set->y != set->crtc->y)
  524. flip_or_move = true;
  525. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  526. DRM_DEBUG("modes are different\n");
  527. drm_mode_debug_printmodeline(&set->crtc->mode);
  528. drm_mode_debug_printmodeline(set->mode);
  529. changed = true;
  530. }
  531. /* a) traverse passed in connector list and get encoders for them */
  532. count = 0;
  533. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  534. struct drm_connector_helper_funcs *connector_funcs =
  535. connector->helper_private;
  536. save_encoders[count++] = connector->encoder;
  537. new_encoder = connector->encoder;
  538. for (ro = 0; ro < set->num_connectors; ro++) {
  539. if (set->connectors[ro] == connector) {
  540. new_encoder = connector_funcs->best_encoder(connector);
  541. /* if we can't get an encoder for a connector
  542. we are setting now - then fail */
  543. if (new_encoder == NULL)
  544. /* don't break so fail path works correct */
  545. fail = 1;
  546. break;
  547. }
  548. }
  549. if (new_encoder != connector->encoder) {
  550. changed = true;
  551. connector->encoder = new_encoder;
  552. }
  553. }
  554. if (fail) {
  555. ret = -EINVAL;
  556. goto fail_no_encoder;
  557. }
  558. count = 0;
  559. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  560. if (!connector->encoder)
  561. continue;
  562. save_crtcs[count++] = connector->encoder->crtc;
  563. if (connector->encoder->crtc == set->crtc)
  564. new_crtc = NULL;
  565. else
  566. new_crtc = connector->encoder->crtc;
  567. for (ro = 0; ro < set->num_connectors; ro++) {
  568. if (set->connectors[ro] == connector)
  569. new_crtc = set->crtc;
  570. }
  571. if (new_crtc != connector->encoder->crtc) {
  572. changed = true;
  573. connector->encoder->crtc = new_crtc;
  574. }
  575. }
  576. /* mode_set_base is not a required function */
  577. if (flip_or_move && !crtc_funcs->mode_set_base)
  578. changed = true;
  579. if (changed) {
  580. set->crtc->fb = set->fb;
  581. set->crtc->enabled = (set->mode != NULL);
  582. if (set->mode != NULL) {
  583. DRM_DEBUG("attempting to set mode from userspace\n");
  584. drm_mode_debug_printmodeline(set->mode);
  585. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  586. set->x, set->y)) {
  587. ret = -EINVAL;
  588. goto fail_set_mode;
  589. }
  590. /* TODO are these needed? */
  591. set->crtc->desired_x = set->x;
  592. set->crtc->desired_y = set->y;
  593. set->crtc->desired_mode = set->mode;
  594. }
  595. drm_helper_disable_unused_functions(dev);
  596. } else if (flip_or_move) {
  597. if (set->crtc->fb != set->fb)
  598. set->crtc->fb = set->fb;
  599. crtc_funcs->mode_set_base(set->crtc, set->x, set->y);
  600. }
  601. kfree(save_encoders);
  602. kfree(save_crtcs);
  603. return 0;
  604. fail_set_mode:
  605. set->crtc->enabled = save_enabled;
  606. count = 0;
  607. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  608. connector->encoder->crtc = save_crtcs[count++];
  609. fail_no_encoder:
  610. kfree(save_crtcs);
  611. count = 0;
  612. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  613. connector->encoder = save_encoders[count++];
  614. }
  615. kfree(save_encoders);
  616. return ret;
  617. }
  618. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  619. bool drm_helper_plugged_event(struct drm_device *dev)
  620. {
  621. DRM_DEBUG("\n");
  622. drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
  623. dev->mode_config.max_height);
  624. drm_setup_crtcs(dev);
  625. /* alert the driver fb layer */
  626. dev->mode_config.funcs->fb_changed(dev);
  627. /* FIXME: send hotplug event */
  628. return true;
  629. }
  630. /**
  631. * drm_initial_config - setup a sane initial connector configuration
  632. * @dev: DRM device
  633. * @can_grow: this configuration is growable
  634. *
  635. * LOCKING:
  636. * Called at init time, must take mode config lock.
  637. *
  638. * Scan the CRTCs and connectors and try to put together an initial setup.
  639. * At the moment, this is a cloned configuration across all heads with
  640. * a new framebuffer object as the backing store.
  641. *
  642. * RETURNS:
  643. * Zero if everything went ok, nonzero otherwise.
  644. */
  645. bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
  646. {
  647. int ret = false;
  648. drm_helper_plugged_event(dev);
  649. return ret;
  650. }
  651. EXPORT_SYMBOL(drm_helper_initial_config);
  652. /**
  653. * drm_hotplug_stage_two
  654. * @dev DRM device
  655. * @connector hotpluged connector
  656. *
  657. * LOCKING.
  658. * Caller must hold mode config lock, function might grab struct lock.
  659. *
  660. * Stage two of a hotplug.
  661. *
  662. * RETURNS:
  663. * Zero on success, errno on failure.
  664. */
  665. int drm_helper_hotplug_stage_two(struct drm_device *dev)
  666. {
  667. drm_helper_plugged_event(dev);
  668. return 0;
  669. }
  670. EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
  671. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  672. struct drm_mode_fb_cmd *mode_cmd)
  673. {
  674. fb->width = mode_cmd->width;
  675. fb->height = mode_cmd->height;
  676. fb->pitch = mode_cmd->pitch;
  677. fb->bits_per_pixel = mode_cmd->bpp;
  678. fb->depth = mode_cmd->depth;
  679. return 0;
  680. }
  681. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  682. int drm_helper_resume_force_mode(struct drm_device *dev)
  683. {
  684. struct drm_crtc *crtc;
  685. int ret;
  686. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  687. if (!crtc->enabled)
  688. continue;
  689. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
  690. crtc->y);
  691. if (ret == false)
  692. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  693. }
  694. return 0;
  695. }
  696. EXPORT_SYMBOL(drm_helper_resume_force_mode);