drm_crtc_helper.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. bool depth_changed, bpp_changed;
  484. adjusted_mode = drm_mode_duplicate(dev, mode);
  485. crtc->enabled = drm_helper_crtc_in_use(crtc);
  486. if (!crtc->enabled)
  487. return true;
  488. if (old_fb && crtc->fb) {
  489. depth_changed = (old_fb->depth != crtc->fb->depth);
  490. bpp_changed = (old_fb->bits_per_pixel !=
  491. crtc->fb->bits_per_pixel);
  492. } else {
  493. depth_changed = true;
  494. bpp_changed = true;
  495. }
  496. saved_mode = crtc->mode;
  497. saved_x = crtc->x;
  498. saved_y = crtc->y;
  499. /* Update crtc values up front so the driver can rely on them for mode
  500. * setting.
  501. */
  502. crtc->mode = *mode;
  503. crtc->x = x;
  504. crtc->y = y;
  505. if (drm_mode_equal(&saved_mode, &crtc->mode)) {
  506. if (saved_x != crtc->x || saved_y != crtc->y ||
  507. depth_changed || bpp_changed) {
  508. ret = !crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y,
  509. old_fb);
  510. goto done;
  511. }
  512. }
  513. /* Pass our mode to the connectors and the CRTC to give them a chance to
  514. * adjust it according to limitations or connector properties, and also
  515. * a chance to reject the mode entirely.
  516. */
  517. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  518. if (encoder->crtc != crtc)
  519. continue;
  520. encoder_funcs = encoder->helper_private;
  521. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  522. adjusted_mode))) {
  523. goto done;
  524. }
  525. }
  526. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  527. goto done;
  528. }
  529. /* Prepare the encoders and CRTCs before setting the mode. */
  530. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  531. if (encoder->crtc != crtc)
  532. continue;
  533. encoder_funcs = encoder->helper_private;
  534. /* Disable the encoders as the first thing we do. */
  535. encoder_funcs->prepare(encoder);
  536. }
  537. drm_crtc_prepare_encoders(dev);
  538. crtc_funcs->prepare(crtc);
  539. /* Set up the DPLL and any encoders state that needs to adjust or depend
  540. * on the DPLL.
  541. */
  542. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  543. if (!ret)
  544. goto done;
  545. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  546. if (encoder->crtc != crtc)
  547. continue;
  548. DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
  549. mode->name, mode->base.id);
  550. encoder_funcs = encoder->helper_private;
  551. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  552. }
  553. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  554. crtc_funcs->commit(crtc);
  555. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  556. if (encoder->crtc != crtc)
  557. continue;
  558. encoder_funcs = encoder->helper_private;
  559. encoder_funcs->commit(encoder);
  560. }
  561. /* XXX free adjustedmode */
  562. drm_mode_destroy(dev, adjusted_mode);
  563. /* FIXME: add subpixel order */
  564. done:
  565. if (!ret) {
  566. crtc->mode = saved_mode;
  567. crtc->x = saved_x;
  568. crtc->y = saved_y;
  569. }
  570. return ret;
  571. }
  572. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  573. /**
  574. * drm_crtc_helper_set_config - set a new config from userspace
  575. * @crtc: CRTC to setup
  576. * @crtc_info: user provided configuration
  577. * @new_mode: new mode to set
  578. * @connector_set: set of connectors for the new config
  579. * @fb: new framebuffer
  580. *
  581. * LOCKING:
  582. * Caller must hold mode config lock.
  583. *
  584. * Setup a new configuration, provided by the user in @crtc_info, and enable
  585. * it.
  586. *
  587. * RETURNS:
  588. * Zero. (FIXME)
  589. */
  590. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  591. {
  592. struct drm_device *dev;
  593. struct drm_crtc **save_crtcs, *new_crtc;
  594. struct drm_encoder **save_encoders, *new_encoder;
  595. struct drm_framebuffer *old_fb = NULL;
  596. bool save_enabled;
  597. bool mode_changed = false;
  598. bool fb_changed = false;
  599. struct drm_connector *connector;
  600. int count = 0, ro, fail = 0;
  601. struct drm_crtc_helper_funcs *crtc_funcs;
  602. int ret = 0;
  603. DRM_DEBUG("\n");
  604. if (!set)
  605. return -EINVAL;
  606. if (!set->crtc)
  607. return -EINVAL;
  608. if (!set->crtc->helper_private)
  609. return -EINVAL;
  610. crtc_funcs = set->crtc->helper_private;
  611. DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
  612. set->crtc, set->crtc->base.id, set->fb, set->connectors,
  613. (int)set->num_connectors, set->x, set->y);
  614. dev = set->crtc->dev;
  615. /* save previous config */
  616. save_enabled = set->crtc->enabled;
  617. /*
  618. * We do mode_config.num_connectors here since we'll look at the
  619. * CRTC and encoder associated with each connector later.
  620. */
  621. save_crtcs = kzalloc(dev->mode_config.num_connector *
  622. sizeof(struct drm_crtc *), GFP_KERNEL);
  623. if (!save_crtcs)
  624. return -ENOMEM;
  625. save_encoders = kzalloc(dev->mode_config.num_connector *
  626. sizeof(struct drm_encoders *), GFP_KERNEL);
  627. if (!save_encoders) {
  628. kfree(save_crtcs);
  629. return -ENOMEM;
  630. }
  631. /* We should be able to check here if the fb has the same properties
  632. * and then just flip_or_move it */
  633. if (set->crtc->fb != set->fb) {
  634. /* If we have no fb then treat it as a full mode set */
  635. if (set->crtc->fb == NULL) {
  636. DRM_DEBUG("crtc has no fb, full mode set\n");
  637. mode_changed = true;
  638. } else if ((set->fb->bits_per_pixel !=
  639. set->crtc->fb->bits_per_pixel) ||
  640. set->fb->depth != set->crtc->fb->depth)
  641. fb_changed = true;
  642. else
  643. fb_changed = true;
  644. }
  645. if (set->x != set->crtc->x || set->y != set->crtc->y)
  646. fb_changed = true;
  647. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  648. DRM_DEBUG("modes are different, full mode set\n");
  649. drm_mode_debug_printmodeline(&set->crtc->mode);
  650. drm_mode_debug_printmodeline(set->mode);
  651. mode_changed = true;
  652. }
  653. /* a) traverse passed in connector list and get encoders for them */
  654. count = 0;
  655. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  656. struct drm_connector_helper_funcs *connector_funcs =
  657. connector->helper_private;
  658. save_encoders[count++] = connector->encoder;
  659. new_encoder = connector->encoder;
  660. for (ro = 0; ro < set->num_connectors; ro++) {
  661. if (set->connectors[ro] == connector) {
  662. new_encoder = connector_funcs->best_encoder(connector);
  663. /* if we can't get an encoder for a connector
  664. we are setting now - then fail */
  665. if (new_encoder == NULL)
  666. /* don't break so fail path works correct */
  667. fail = 1;
  668. break;
  669. }
  670. }
  671. if (new_encoder != connector->encoder) {
  672. DRM_DEBUG("encoder changed, full mode switch\n");
  673. mode_changed = true;
  674. connector->encoder = new_encoder;
  675. }
  676. }
  677. if (fail) {
  678. ret = -EINVAL;
  679. goto fail_no_encoder;
  680. }
  681. count = 0;
  682. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  683. if (!connector->encoder)
  684. continue;
  685. save_crtcs[count++] = connector->encoder->crtc;
  686. if (connector->encoder->crtc == set->crtc)
  687. new_crtc = NULL;
  688. else
  689. new_crtc = connector->encoder->crtc;
  690. for (ro = 0; ro < set->num_connectors; ro++) {
  691. if (set->connectors[ro] == connector)
  692. new_crtc = set->crtc;
  693. }
  694. /* Make sure the new CRTC will work with the encoder */
  695. if (new_crtc &&
  696. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  697. ret = -EINVAL;
  698. goto fail_set_mode;
  699. }
  700. if (new_crtc != connector->encoder->crtc) {
  701. DRM_DEBUG("crtc changed, full mode switch\n");
  702. mode_changed = true;
  703. connector->encoder->crtc = new_crtc;
  704. }
  705. DRM_DEBUG("setting connector %d crtc to %p\n",
  706. connector->base.id, new_crtc);
  707. }
  708. /* mode_set_base is not a required function */
  709. if (fb_changed && !crtc_funcs->mode_set_base)
  710. mode_changed = true;
  711. if (mode_changed) {
  712. old_fb = set->crtc->fb;
  713. set->crtc->fb = set->fb;
  714. set->crtc->enabled = (set->mode != NULL);
  715. if (set->mode != NULL) {
  716. DRM_DEBUG("attempting to set mode from userspace\n");
  717. drm_mode_debug_printmodeline(set->mode);
  718. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  719. set->x, set->y,
  720. old_fb)) {
  721. DRM_ERROR("failed to set mode on crtc %p\n",
  722. set->crtc);
  723. ret = -EINVAL;
  724. goto fail_set_mode;
  725. }
  726. /* TODO are these needed? */
  727. set->crtc->desired_x = set->x;
  728. set->crtc->desired_y = set->y;
  729. set->crtc->desired_mode = set->mode;
  730. }
  731. drm_helper_disable_unused_functions(dev);
  732. } else if (fb_changed) {
  733. old_fb = set->crtc->fb;
  734. if (set->crtc->fb != set->fb)
  735. set->crtc->fb = set->fb;
  736. ret = crtc_funcs->mode_set_base(set->crtc,
  737. set->x, set->y, old_fb);
  738. if (ret != 0)
  739. goto fail_set_mode;
  740. }
  741. kfree(save_encoders);
  742. kfree(save_crtcs);
  743. return 0;
  744. fail_set_mode:
  745. set->crtc->enabled = save_enabled;
  746. set->crtc->fb = old_fb;
  747. count = 0;
  748. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  749. if (!connector->encoder)
  750. continue;
  751. connector->encoder->crtc = save_crtcs[count++];
  752. }
  753. fail_no_encoder:
  754. kfree(save_crtcs);
  755. count = 0;
  756. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  757. connector->encoder = save_encoders[count++];
  758. }
  759. kfree(save_encoders);
  760. return ret;
  761. }
  762. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  763. bool drm_helper_plugged_event(struct drm_device *dev)
  764. {
  765. DRM_DEBUG("\n");
  766. drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
  767. dev->mode_config.max_height);
  768. drm_setup_crtcs(dev);
  769. /* alert the driver fb layer */
  770. dev->mode_config.funcs->fb_changed(dev);
  771. /* FIXME: send hotplug event */
  772. return true;
  773. }
  774. /**
  775. * drm_initial_config - setup a sane initial connector configuration
  776. * @dev: DRM device
  777. *
  778. * LOCKING:
  779. * Called at init time, must take mode config lock.
  780. *
  781. * Scan the CRTCs and connectors and try to put together an initial setup.
  782. * At the moment, this is a cloned configuration across all heads with
  783. * a new framebuffer object as the backing store.
  784. *
  785. * RETURNS:
  786. * Zero if everything went ok, nonzero otherwise.
  787. */
  788. bool drm_helper_initial_config(struct drm_device *dev)
  789. {
  790. struct drm_connector *connector;
  791. int count = 0;
  792. count = drm_helper_probe_connector_modes(dev,
  793. dev->mode_config.max_width,
  794. dev->mode_config.max_height);
  795. /*
  796. * None of the available connectors had any modes, so add some
  797. * and try to light them up anyway
  798. */
  799. if (!count) {
  800. DRM_ERROR("connectors have no modes, using standard modes\n");
  801. list_for_each_entry(connector,
  802. &dev->mode_config.connector_list,
  803. head)
  804. drm_helper_add_std_modes(dev, connector);
  805. }
  806. drm_setup_crtcs(dev);
  807. /* alert the driver fb layer */
  808. dev->mode_config.funcs->fb_changed(dev);
  809. return 0;
  810. }
  811. EXPORT_SYMBOL(drm_helper_initial_config);
  812. /**
  813. * drm_hotplug_stage_two
  814. * @dev DRM device
  815. * @connector hotpluged connector
  816. *
  817. * LOCKING.
  818. * Caller must hold mode config lock, function might grab struct lock.
  819. *
  820. * Stage two of a hotplug.
  821. *
  822. * RETURNS:
  823. * Zero on success, errno on failure.
  824. */
  825. int drm_helper_hotplug_stage_two(struct drm_device *dev)
  826. {
  827. drm_helper_plugged_event(dev);
  828. return 0;
  829. }
  830. EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
  831. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  832. struct drm_mode_fb_cmd *mode_cmd)
  833. {
  834. fb->width = mode_cmd->width;
  835. fb->height = mode_cmd->height;
  836. fb->pitch = mode_cmd->pitch;
  837. fb->bits_per_pixel = mode_cmd->bpp;
  838. fb->depth = mode_cmd->depth;
  839. return 0;
  840. }
  841. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  842. int drm_helper_resume_force_mode(struct drm_device *dev)
  843. {
  844. struct drm_crtc *crtc;
  845. int ret;
  846. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  847. if (!crtc->enabled)
  848. continue;
  849. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  850. crtc->x, crtc->y, crtc->fb);
  851. if (ret == false)
  852. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  853. }
  854. return 0;
  855. }
  856. EXPORT_SYMBOL(drm_helper_resume_force_mode);