drm_crtc_helper.c 29 KB

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