drm_crtc_helper.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. struct drm_framebuffer *old_fb)
  377. {
  378. struct drm_device *dev = crtc->dev;
  379. struct drm_display_mode *adjusted_mode, saved_mode;
  380. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  381. struct drm_encoder_helper_funcs *encoder_funcs;
  382. int saved_x, saved_y;
  383. struct drm_encoder *encoder;
  384. bool ret = true;
  385. adjusted_mode = drm_mode_duplicate(dev, mode);
  386. crtc->enabled = drm_helper_crtc_in_use(crtc);
  387. if (!crtc->enabled)
  388. return true;
  389. saved_mode = crtc->mode;
  390. saved_x = crtc->x;
  391. saved_y = crtc->y;
  392. /* Update crtc values up front so the driver can rely on them for mode
  393. * setting.
  394. */
  395. crtc->mode = *mode;
  396. crtc->x = x;
  397. crtc->y = y;
  398. if (drm_mode_equal(&saved_mode, &crtc->mode)) {
  399. if (saved_x != crtc->x || saved_y != crtc->y) {
  400. crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y,
  401. old_fb);
  402. goto done;
  403. }
  404. }
  405. /* Pass our mode to the connectors and the CRTC to give them a chance to
  406. * adjust it according to limitations or connector properties, and also
  407. * a chance to reject the mode entirely.
  408. */
  409. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  410. if (encoder->crtc != crtc)
  411. continue;
  412. encoder_funcs = encoder->helper_private;
  413. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  414. adjusted_mode))) {
  415. goto done;
  416. }
  417. }
  418. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  419. goto done;
  420. }
  421. /* Prepare the encoders and CRTCs before setting the mode. */
  422. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  423. if (encoder->crtc != crtc)
  424. continue;
  425. encoder_funcs = encoder->helper_private;
  426. /* Disable the encoders as the first thing we do. */
  427. encoder_funcs->prepare(encoder);
  428. }
  429. crtc_funcs->prepare(crtc);
  430. /* Set up the DPLL and any encoders state that needs to adjust or depend
  431. * on the DPLL.
  432. */
  433. crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  434. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  435. if (encoder->crtc != crtc)
  436. continue;
  437. DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
  438. mode->name, mode->base.id);
  439. encoder_funcs = encoder->helper_private;
  440. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  441. }
  442. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  443. crtc_funcs->commit(crtc);
  444. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  445. if (encoder->crtc != crtc)
  446. continue;
  447. encoder_funcs = encoder->helper_private;
  448. encoder_funcs->commit(encoder);
  449. }
  450. /* XXX free adjustedmode */
  451. drm_mode_destroy(dev, adjusted_mode);
  452. /* FIXME: add subpixel order */
  453. done:
  454. if (!ret) {
  455. crtc->mode = saved_mode;
  456. crtc->x = saved_x;
  457. crtc->y = saved_y;
  458. }
  459. return ret;
  460. }
  461. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  462. /**
  463. * drm_crtc_helper_set_config - set a new config from userspace
  464. * @crtc: CRTC to setup
  465. * @crtc_info: user provided configuration
  466. * @new_mode: new mode to set
  467. * @connector_set: set of connectors for the new config
  468. * @fb: new framebuffer
  469. *
  470. * LOCKING:
  471. * Caller must hold mode config lock.
  472. *
  473. * Setup a new configuration, provided by the user in @crtc_info, and enable
  474. * it.
  475. *
  476. * RETURNS:
  477. * Zero. (FIXME)
  478. */
  479. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  480. {
  481. struct drm_device *dev;
  482. struct drm_crtc **save_crtcs, *new_crtc;
  483. struct drm_encoder **save_encoders, *new_encoder;
  484. struct drm_framebuffer *old_fb;
  485. bool save_enabled;
  486. bool changed = false;
  487. bool flip_or_move = false;
  488. struct drm_connector *connector;
  489. int count = 0, ro, fail = 0;
  490. struct drm_crtc_helper_funcs *crtc_funcs;
  491. int ret = 0;
  492. DRM_DEBUG("\n");
  493. if (!set)
  494. return -EINVAL;
  495. if (!set->crtc)
  496. return -EINVAL;
  497. if (!set->crtc->helper_private)
  498. return -EINVAL;
  499. crtc_funcs = set->crtc->helper_private;
  500. DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
  501. set->crtc, set->crtc->base.id, set->fb, set->connectors,
  502. (int)set->num_connectors, set->x, set->y);
  503. dev = set->crtc->dev;
  504. /* save previous config */
  505. save_enabled = set->crtc->enabled;
  506. /* this is meant to be num_connector not num_crtc */
  507. save_crtcs = kzalloc(dev->mode_config.num_connector *
  508. sizeof(struct drm_crtc *), GFP_KERNEL);
  509. if (!save_crtcs)
  510. return -ENOMEM;
  511. save_encoders = kzalloc(dev->mode_config.num_connector *
  512. sizeof(struct drm_encoders *), GFP_KERNEL);
  513. if (!save_encoders) {
  514. kfree(save_crtcs);
  515. return -ENOMEM;
  516. }
  517. /* We should be able to check here if the fb has the same properties
  518. * and then just flip_or_move it */
  519. if (set->crtc->fb != set->fb) {
  520. /* if we have no fb then its a change not a flip */
  521. if (set->crtc->fb == NULL)
  522. changed = true;
  523. else
  524. flip_or_move = true;
  525. }
  526. if (set->x != set->crtc->x || set->y != set->crtc->y)
  527. flip_or_move = true;
  528. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  529. DRM_DEBUG("modes are different\n");
  530. drm_mode_debug_printmodeline(&set->crtc->mode);
  531. drm_mode_debug_printmodeline(set->mode);
  532. changed = true;
  533. }
  534. /* a) traverse passed in connector list and get encoders for them */
  535. count = 0;
  536. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  537. struct drm_connector_helper_funcs *connector_funcs =
  538. connector->helper_private;
  539. save_encoders[count++] = connector->encoder;
  540. new_encoder = connector->encoder;
  541. for (ro = 0; ro < set->num_connectors; ro++) {
  542. if (set->connectors[ro] == connector) {
  543. new_encoder = connector_funcs->best_encoder(connector);
  544. /* if we can't get an encoder for a connector
  545. we are setting now - then fail */
  546. if (new_encoder == NULL)
  547. /* don't break so fail path works correct */
  548. fail = 1;
  549. break;
  550. }
  551. }
  552. if (new_encoder != connector->encoder) {
  553. changed = true;
  554. connector->encoder = new_encoder;
  555. }
  556. }
  557. if (fail) {
  558. ret = -EINVAL;
  559. goto fail_no_encoder;
  560. }
  561. count = 0;
  562. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  563. if (!connector->encoder)
  564. continue;
  565. save_crtcs[count++] = connector->encoder->crtc;
  566. if (connector->encoder->crtc == set->crtc)
  567. new_crtc = NULL;
  568. else
  569. new_crtc = connector->encoder->crtc;
  570. for (ro = 0; ro < set->num_connectors; ro++) {
  571. if (set->connectors[ro] == connector)
  572. new_crtc = set->crtc;
  573. }
  574. if (new_crtc != connector->encoder->crtc) {
  575. changed = true;
  576. connector->encoder->crtc = new_crtc;
  577. }
  578. }
  579. /* mode_set_base is not a required function */
  580. if (flip_or_move && !crtc_funcs->mode_set_base)
  581. changed = true;
  582. if (changed) {
  583. old_fb = set->crtc->fb;
  584. set->crtc->fb = set->fb;
  585. set->crtc->enabled = (set->mode != NULL);
  586. if (set->mode != NULL) {
  587. DRM_DEBUG("attempting to set mode from userspace\n");
  588. drm_mode_debug_printmodeline(set->mode);
  589. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  590. set->x, set->y,
  591. old_fb)) {
  592. ret = -EINVAL;
  593. goto fail_set_mode;
  594. }
  595. /* TODO are these needed? */
  596. set->crtc->desired_x = set->x;
  597. set->crtc->desired_y = set->y;
  598. set->crtc->desired_mode = set->mode;
  599. }
  600. drm_helper_disable_unused_functions(dev);
  601. } else if (flip_or_move) {
  602. old_fb = set->crtc->fb;
  603. if (set->crtc->fb != set->fb)
  604. set->crtc->fb = set->fb;
  605. crtc_funcs->mode_set_base(set->crtc, set->x, set->y, old_fb);
  606. }
  607. kfree(save_encoders);
  608. kfree(save_crtcs);
  609. return 0;
  610. fail_set_mode:
  611. set->crtc->enabled = save_enabled;
  612. count = 0;
  613. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  614. connector->encoder->crtc = save_crtcs[count++];
  615. fail_no_encoder:
  616. kfree(save_crtcs);
  617. count = 0;
  618. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  619. connector->encoder = save_encoders[count++];
  620. }
  621. kfree(save_encoders);
  622. return ret;
  623. }
  624. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  625. bool drm_helper_plugged_event(struct drm_device *dev)
  626. {
  627. DRM_DEBUG("\n");
  628. drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
  629. dev->mode_config.max_height);
  630. drm_setup_crtcs(dev);
  631. /* alert the driver fb layer */
  632. dev->mode_config.funcs->fb_changed(dev);
  633. /* FIXME: send hotplug event */
  634. return true;
  635. }
  636. /**
  637. * drm_initial_config - setup a sane initial connector configuration
  638. * @dev: DRM device
  639. * @can_grow: this configuration is growable
  640. *
  641. * LOCKING:
  642. * Called at init time, must take mode config lock.
  643. *
  644. * Scan the CRTCs and connectors and try to put together an initial setup.
  645. * At the moment, this is a cloned configuration across all heads with
  646. * a new framebuffer object as the backing store.
  647. *
  648. * RETURNS:
  649. * Zero if everything went ok, nonzero otherwise.
  650. */
  651. bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
  652. {
  653. int ret = false;
  654. drm_helper_plugged_event(dev);
  655. return ret;
  656. }
  657. EXPORT_SYMBOL(drm_helper_initial_config);
  658. /**
  659. * drm_hotplug_stage_two
  660. * @dev DRM device
  661. * @connector hotpluged connector
  662. *
  663. * LOCKING.
  664. * Caller must hold mode config lock, function might grab struct lock.
  665. *
  666. * Stage two of a hotplug.
  667. *
  668. * RETURNS:
  669. * Zero on success, errno on failure.
  670. */
  671. int drm_helper_hotplug_stage_two(struct drm_device *dev)
  672. {
  673. drm_helper_plugged_event(dev);
  674. return 0;
  675. }
  676. EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
  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. int ret;
  692. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  693. if (!crtc->enabled)
  694. continue;
  695. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  696. crtc->x, crtc->y, crtc->fb);
  697. if (ret == false)
  698. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  699. }
  700. return 0;
  701. }
  702. EXPORT_SYMBOL(drm_helper_resume_force_mode);