drm_crtc_helper.c 25 KB

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