drm_crtc_helper.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. connector->encoder = new_encoder;
  702. }
  703. }
  704. if (fail) {
  705. ret = -EINVAL;
  706. goto fail;
  707. }
  708. count = 0;
  709. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  710. if (!connector->encoder)
  711. continue;
  712. if (connector->encoder->crtc == set->crtc)
  713. new_crtc = NULL;
  714. else
  715. new_crtc = connector->encoder->crtc;
  716. for (ro = 0; ro < set->num_connectors; ro++) {
  717. if (set->connectors[ro] == connector)
  718. new_crtc = set->crtc;
  719. }
  720. /* Make sure the new CRTC will work with the encoder */
  721. if (new_crtc &&
  722. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  723. ret = -EINVAL;
  724. goto fail;
  725. }
  726. if (new_crtc != connector->encoder->crtc) {
  727. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  728. mode_changed = true;
  729. connector->encoder->crtc = new_crtc;
  730. }
  731. DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
  732. connector->base.id, new_crtc);
  733. }
  734. /* mode_set_base is not a required function */
  735. if (fb_changed && !crtc_funcs->mode_set_base)
  736. mode_changed = true;
  737. if (mode_changed) {
  738. old_fb = set->crtc->fb;
  739. set->crtc->fb = set->fb;
  740. set->crtc->enabled = (set->mode != NULL);
  741. if (set->mode != NULL) {
  742. DRM_DEBUG_KMS("attempting to set mode from"
  743. " userspace\n");
  744. drm_mode_debug_printmodeline(set->mode);
  745. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  746. set->x, set->y,
  747. old_fb)) {
  748. DRM_ERROR("failed to set mode on crtc %p\n",
  749. set->crtc);
  750. ret = -EINVAL;
  751. goto fail;
  752. }
  753. /* TODO are these needed? */
  754. set->crtc->desired_x = set->x;
  755. set->crtc->desired_y = set->y;
  756. set->crtc->desired_mode = set->mode;
  757. }
  758. drm_helper_disable_unused_functions(dev);
  759. } else if (fb_changed) {
  760. old_fb = set->crtc->fb;
  761. if (set->crtc->fb != set->fb)
  762. set->crtc->fb = set->fb;
  763. ret = crtc_funcs->mode_set_base(set->crtc,
  764. set->x, set->y, old_fb);
  765. if (ret != 0)
  766. goto fail;
  767. }
  768. kfree(save_connectors);
  769. kfree(save_encoders);
  770. kfree(save_crtcs);
  771. return 0;
  772. fail:
  773. /* Restore all previous data. */
  774. count = 0;
  775. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  776. *crtc = save_crtcs[count++];
  777. }
  778. count = 0;
  779. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  780. *encoder = save_encoders[count++];
  781. }
  782. count = 0;
  783. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  784. *connector = save_connectors[count++];
  785. }
  786. kfree(save_connectors);
  787. kfree(save_encoders);
  788. kfree(save_crtcs);
  789. return ret;
  790. }
  791. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  792. bool drm_helper_plugged_event(struct drm_device *dev)
  793. {
  794. DRM_DEBUG_KMS("\n");
  795. drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
  796. dev->mode_config.max_height);
  797. drm_setup_crtcs(dev);
  798. /* alert the driver fb layer */
  799. dev->mode_config.funcs->fb_changed(dev);
  800. /* FIXME: send hotplug event */
  801. return true;
  802. }
  803. /**
  804. * drm_initial_config - setup a sane initial connector configuration
  805. * @dev: DRM device
  806. *
  807. * LOCKING:
  808. * Called at init time, must take mode config lock.
  809. *
  810. * Scan the CRTCs and connectors and try to put together an initial setup.
  811. * At the moment, this is a cloned configuration across all heads with
  812. * a new framebuffer object as the backing store.
  813. *
  814. * RETURNS:
  815. * Zero if everything went ok, nonzero otherwise.
  816. */
  817. bool drm_helper_initial_config(struct drm_device *dev)
  818. {
  819. struct drm_connector *connector;
  820. int count = 0;
  821. count = drm_helper_probe_connector_modes(dev,
  822. dev->mode_config.max_width,
  823. dev->mode_config.max_height);
  824. /*
  825. * None of the available connectors had any modes, so add some
  826. * and try to light them up anyway
  827. */
  828. if (!count) {
  829. DRM_ERROR("connectors have no modes, using standard modes\n");
  830. list_for_each_entry(connector,
  831. &dev->mode_config.connector_list,
  832. head)
  833. drm_helper_add_std_modes(dev, connector);
  834. }
  835. drm_setup_crtcs(dev);
  836. /* alert the driver fb layer */
  837. dev->mode_config.funcs->fb_changed(dev);
  838. return 0;
  839. }
  840. EXPORT_SYMBOL(drm_helper_initial_config);
  841. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  842. {
  843. int dpms = DRM_MODE_DPMS_OFF;
  844. struct drm_connector *connector;
  845. struct drm_device *dev = encoder->dev;
  846. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  847. if (connector->encoder == encoder)
  848. if (connector->dpms < dpms)
  849. dpms = connector->dpms;
  850. return dpms;
  851. }
  852. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  853. {
  854. int dpms = DRM_MODE_DPMS_OFF;
  855. struct drm_connector *connector;
  856. struct drm_device *dev = crtc->dev;
  857. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  858. if (connector->encoder && connector->encoder->crtc == crtc)
  859. if (connector->dpms < dpms)
  860. dpms = connector->dpms;
  861. return dpms;
  862. }
  863. /**
  864. * drm_helper_connector_dpms
  865. * @connector affected connector
  866. * @mode DPMS mode
  867. *
  868. * Calls the low-level connector DPMS function, then
  869. * calls appropriate encoder and crtc DPMS functions as well
  870. */
  871. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  872. {
  873. struct drm_encoder *encoder = connector->encoder;
  874. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  875. int old_dpms;
  876. if (mode == connector->dpms)
  877. return;
  878. old_dpms = connector->dpms;
  879. connector->dpms = mode;
  880. /* from off to on, do crtc then encoder */
  881. if (mode < old_dpms) {
  882. if (crtc) {
  883. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  884. if (crtc_funcs->dpms)
  885. (*crtc_funcs->dpms) (crtc,
  886. drm_helper_choose_crtc_dpms(crtc));
  887. }
  888. if (encoder) {
  889. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  890. if (encoder_funcs->dpms)
  891. (*encoder_funcs->dpms) (encoder,
  892. drm_helper_choose_encoder_dpms(encoder));
  893. }
  894. }
  895. /* from on to off, do encoder then crtc */
  896. if (mode > old_dpms) {
  897. if (encoder) {
  898. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  899. if (encoder_funcs->dpms)
  900. (*encoder_funcs->dpms) (encoder,
  901. drm_helper_choose_encoder_dpms(encoder));
  902. }
  903. if (crtc) {
  904. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  905. if (crtc_funcs->dpms)
  906. (*crtc_funcs->dpms) (crtc,
  907. drm_helper_choose_crtc_dpms(crtc));
  908. }
  909. }
  910. return;
  911. }
  912. EXPORT_SYMBOL(drm_helper_connector_dpms);
  913. /**
  914. * drm_hotplug_stage_two
  915. * @dev DRM device
  916. * @connector hotpluged connector
  917. *
  918. * LOCKING.
  919. * Caller must hold mode config lock, function might grab struct lock.
  920. *
  921. * Stage two of a hotplug.
  922. *
  923. * RETURNS:
  924. * Zero on success, errno on failure.
  925. */
  926. int drm_helper_hotplug_stage_two(struct drm_device *dev)
  927. {
  928. drm_helper_plugged_event(dev);
  929. return 0;
  930. }
  931. EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
  932. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  933. struct drm_mode_fb_cmd *mode_cmd)
  934. {
  935. fb->width = mode_cmd->width;
  936. fb->height = mode_cmd->height;
  937. fb->pitch = mode_cmd->pitch;
  938. fb->bits_per_pixel = mode_cmd->bpp;
  939. fb->depth = mode_cmd->depth;
  940. return 0;
  941. }
  942. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  943. int drm_helper_resume_force_mode(struct drm_device *dev)
  944. {
  945. struct drm_crtc *crtc;
  946. int ret;
  947. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  948. if (!crtc->enabled)
  949. continue;
  950. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  951. crtc->x, crtc->y, crtc->fb);
  952. if (ret == false)
  953. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  954. }
  955. /* disable the unused connectors while restoring the modesetting */
  956. drm_helper_disable_unused_functions(dev);
  957. return 0;
  958. }
  959. EXPORT_SYMBOL(drm_helper_resume_force_mode);