drm_crtc_helper.c 30 KB

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