drm_crtc_helper.c 32 KB

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