drm_crtc_helper.c 26 KB

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