drm_crtc_helper.c 24 KB

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