drm_crtc_helper.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. /**
  43. * drm_helper_probe_connector_modes - get complete set of display modes
  44. * @dev: DRM device
  45. * @maxX: max width for modes
  46. * @maxY: max height for modes
  47. *
  48. * LOCKING:
  49. * Caller must hold mode config lock.
  50. *
  51. * Based on @dev's mode_config layout, scan all the connectors and try to detect
  52. * modes on them. Modes will first be added to the connector's probed_modes
  53. * list, then culled (based on validity and the @maxX, @maxY parameters) and
  54. * put into the normal modes list.
  55. *
  56. * Intended to be used either at bootup time or when major configuration
  57. * changes have occurred.
  58. *
  59. * FIXME: take into account monitor limits
  60. *
  61. * RETURNS:
  62. * Number of modes found on @connector.
  63. */
  64. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  65. uint32_t maxX, uint32_t maxY)
  66. {
  67. struct drm_device *dev = connector->dev;
  68. struct drm_display_mode *mode, *t;
  69. struct drm_connector_helper_funcs *connector_funcs =
  70. connector->helper_private;
  71. int count = 0;
  72. DRM_DEBUG("%s\n", drm_get_connector_name(connector));
  73. /* set all modes to the unverified state */
  74. list_for_each_entry_safe(mode, t, &connector->modes, head)
  75. mode->status = MODE_UNVERIFIED;
  76. connector->status = connector->funcs->detect(connector);
  77. if (connector->status == connector_status_disconnected) {
  78. DRM_DEBUG("%s is disconnected\n",
  79. drm_get_connector_name(connector));
  80. /* TODO set EDID to NULL */
  81. return 0;
  82. }
  83. count = (*connector_funcs->get_modes)(connector);
  84. if (!count)
  85. return 0;
  86. drm_mode_connector_list_update(connector);
  87. if (maxX && maxY)
  88. drm_mode_validate_size(dev, &connector->modes, maxX,
  89. maxY, 0);
  90. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  91. if (mode->status == MODE_OK)
  92. mode->status = connector_funcs->mode_valid(connector,
  93. mode);
  94. }
  95. drm_mode_prune_invalid(dev, &connector->modes, true);
  96. if (list_empty(&connector->modes))
  97. return 0;
  98. drm_mode_sort(&connector->modes);
  99. DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
  100. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  101. mode->vrefresh = drm_mode_vrefresh(mode);
  102. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  103. drm_mode_debug_printmodeline(mode);
  104. }
  105. return count;
  106. }
  107. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  108. int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
  109. uint32_t maxY)
  110. {
  111. struct drm_connector *connector;
  112. int count = 0;
  113. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  114. count += drm_helper_probe_single_connector_modes(connector,
  115. maxX, maxY);
  116. }
  117. return count;
  118. }
  119. EXPORT_SYMBOL(drm_helper_probe_connector_modes);
  120. static void drm_helper_add_std_modes(struct drm_device *dev,
  121. struct drm_connector *connector)
  122. {
  123. struct drm_display_mode *mode, *t;
  124. int i;
  125. for (i = 0; i < ARRAY_SIZE(std_modes); i++) {
  126. struct drm_display_mode *stdmode;
  127. /*
  128. * When no valid EDID modes are available we end up
  129. * here and bailed in the past, now we add some standard
  130. * modes and move on.
  131. */
  132. stdmode = drm_mode_duplicate(dev, &std_modes[i]);
  133. drm_mode_probed_add(connector, stdmode);
  134. drm_mode_list_concat(&connector->probed_modes,
  135. &connector->modes);
  136. DRM_DEBUG("Adding mode %s to %s\n", stdmode->name,
  137. drm_get_connector_name(connector));
  138. }
  139. drm_mode_sort(&connector->modes);
  140. DRM_DEBUG("Added std modes on %s\n", drm_get_connector_name(connector));
  141. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  142. mode->vrefresh = drm_mode_vrefresh(mode);
  143. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  144. drm_mode_debug_printmodeline(mode);
  145. }
  146. }
  147. /**
  148. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  149. * @crtc: CRTC to check
  150. *
  151. * LOCKING:
  152. * Caller must hold mode config lock.
  153. *
  154. * Walk @crtc's DRM device's mode_config and see if it's in use.
  155. *
  156. * RETURNS:
  157. * True if @crtc is part of the mode_config, false otherwise.
  158. */
  159. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  160. {
  161. struct drm_encoder *encoder;
  162. struct drm_device *dev = crtc->dev;
  163. /* FIXME: Locking around list access? */
  164. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  165. if (encoder->crtc == crtc)
  166. return true;
  167. return false;
  168. }
  169. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  170. /**
  171. * drm_disable_unused_functions - disable unused objects
  172. * @dev: DRM device
  173. *
  174. * LOCKING:
  175. * Caller must hold mode config lock.
  176. *
  177. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  178. * by calling its dpms function, which should power it off.
  179. */
  180. void drm_helper_disable_unused_functions(struct drm_device *dev)
  181. {
  182. struct drm_encoder *encoder;
  183. struct drm_encoder_helper_funcs *encoder_funcs;
  184. struct drm_crtc *crtc;
  185. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  186. encoder_funcs = encoder->helper_private;
  187. if (!encoder->crtc)
  188. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  189. }
  190. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  191. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  192. crtc->enabled = drm_helper_crtc_in_use(crtc);
  193. if (!crtc->enabled) {
  194. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  195. crtc->fb = NULL;
  196. }
  197. }
  198. }
  199. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  200. static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
  201. {
  202. struct drm_display_mode *mode;
  203. list_for_each_entry(mode, &connector->modes, head) {
  204. if (drm_mode_width(mode) > width ||
  205. drm_mode_height(mode) > height)
  206. continue;
  207. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  208. return mode;
  209. }
  210. return NULL;
  211. }
  212. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  213. {
  214. bool enable;
  215. if (strict) {
  216. enable = connector->status == connector_status_connected;
  217. } else {
  218. enable = connector->status != connector_status_disconnected;
  219. }
  220. return enable;
  221. }
  222. static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
  223. {
  224. bool any_enabled = false;
  225. struct drm_connector *connector;
  226. int i = 0;
  227. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  228. enabled[i] = drm_connector_enabled(connector, true);
  229. DRM_DEBUG("connector %d enabled? %s\n", connector->base.id,
  230. enabled[i] ? "yes" : "no");
  231. any_enabled |= enabled[i];
  232. i++;
  233. }
  234. if (any_enabled)
  235. return;
  236. i = 0;
  237. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  238. enabled[i] = drm_connector_enabled(connector, false);
  239. i++;
  240. }
  241. }
  242. static bool drm_target_preferred(struct drm_device *dev,
  243. struct drm_display_mode **modes,
  244. bool *enabled, int width, int height)
  245. {
  246. struct drm_connector *connector;
  247. int i = 0;
  248. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  249. if (enabled[i] == false) {
  250. i++;
  251. continue;
  252. }
  253. DRM_DEBUG("looking for preferred mode on connector %d\n",
  254. connector->base.id);
  255. modes[i] = drm_has_preferred_mode(connector, width, height);
  256. /* No preferred modes, pick one off the list */
  257. if (!modes[i] && !list_empty(&connector->modes)) {
  258. list_for_each_entry(modes[i], &connector->modes, head)
  259. break;
  260. }
  261. DRM_DEBUG("found mode %s\n", modes[i] ? modes[i]->name :
  262. "none");
  263. i++;
  264. }
  265. return true;
  266. }
  267. static int drm_pick_crtcs(struct drm_device *dev,
  268. struct drm_crtc **best_crtcs,
  269. struct drm_display_mode **modes,
  270. int n, int width, int height)
  271. {
  272. int c, o;
  273. struct drm_connector *connector;
  274. struct drm_connector_helper_funcs *connector_funcs;
  275. struct drm_encoder *encoder;
  276. struct drm_crtc *best_crtc;
  277. int my_score, best_score, score;
  278. struct drm_crtc **crtcs, *crtc;
  279. if (n == dev->mode_config.num_connector)
  280. return 0;
  281. c = 0;
  282. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  283. if (c == n)
  284. break;
  285. c++;
  286. }
  287. best_crtcs[n] = NULL;
  288. best_crtc = NULL;
  289. best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
  290. if (modes[n] == NULL)
  291. return best_score;
  292. crtcs = kmalloc(dev->mode_config.num_connector *
  293. sizeof(struct drm_crtc *), GFP_KERNEL);
  294. if (!crtcs)
  295. return best_score;
  296. my_score = 1;
  297. if (connector->status == connector_status_connected)
  298. my_score++;
  299. if (drm_has_preferred_mode(connector, width, height))
  300. my_score++;
  301. connector_funcs = connector->helper_private;
  302. encoder = connector_funcs->best_encoder(connector);
  303. if (!encoder)
  304. goto out;
  305. connector->encoder = encoder;
  306. /* select a crtc for this connector and then attempt to configure
  307. remaining connectors */
  308. c = 0;
  309. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  310. if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
  311. c++;
  312. continue;
  313. }
  314. for (o = 0; o < n; o++)
  315. if (best_crtcs[o] == crtc)
  316. break;
  317. if (o < n) {
  318. /* ignore cloning for now */
  319. c++;
  320. continue;
  321. }
  322. crtcs[n] = crtc;
  323. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
  324. score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
  325. width, height);
  326. if (score > best_score) {
  327. best_crtc = crtc;
  328. best_score = score;
  329. memcpy(best_crtcs, crtcs,
  330. dev->mode_config.num_connector *
  331. sizeof(struct drm_crtc *));
  332. }
  333. c++;
  334. }
  335. out:
  336. kfree(crtcs);
  337. return best_score;
  338. }
  339. static void drm_setup_crtcs(struct drm_device *dev)
  340. {
  341. struct drm_crtc **crtcs;
  342. struct drm_display_mode **modes;
  343. struct drm_encoder *encoder;
  344. struct drm_connector *connector;
  345. bool *enabled;
  346. int width, height;
  347. int i, ret;
  348. DRM_DEBUG("\n");
  349. width = dev->mode_config.max_width;
  350. height = dev->mode_config.max_height;
  351. /* clean out all the encoder/crtc combos */
  352. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  353. encoder->crtc = NULL;
  354. }
  355. crtcs = kcalloc(dev->mode_config.num_connector,
  356. sizeof(struct drm_crtc *), GFP_KERNEL);
  357. modes = kcalloc(dev->mode_config.num_connector,
  358. sizeof(struct drm_display_mode *), GFP_KERNEL);
  359. enabled = kcalloc(dev->mode_config.num_connector,
  360. sizeof(bool), GFP_KERNEL);
  361. drm_enable_connectors(dev, enabled);
  362. ret = drm_target_preferred(dev, modes, enabled, width, height);
  363. if (!ret)
  364. DRM_ERROR("Unable to find initial modes\n");
  365. DRM_DEBUG("picking CRTCs for %dx%d config\n", width, height);
  366. drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
  367. i = 0;
  368. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  369. struct drm_display_mode *mode = modes[i];
  370. struct drm_crtc *crtc = crtcs[i];
  371. if (connector->encoder == NULL) {
  372. i++;
  373. continue;
  374. }
  375. if (mode && crtc) {
  376. DRM_DEBUG("desired mode %s set on crtc %d\n",
  377. mode->name, crtc->base.id);
  378. crtc->desired_mode = mode;
  379. connector->encoder->crtc = crtc;
  380. } else
  381. connector->encoder->crtc = NULL;
  382. i++;
  383. }
  384. kfree(crtcs);
  385. kfree(modes);
  386. kfree(enabled);
  387. }
  388. /**
  389. * drm_crtc_set_mode - set a mode
  390. * @crtc: CRTC to program
  391. * @mode: mode to use
  392. * @x: width of mode
  393. * @y: height of mode
  394. *
  395. * LOCKING:
  396. * Caller must hold mode config lock.
  397. *
  398. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  399. * to fixup or reject the mode prior to trying to set it.
  400. *
  401. * RETURNS:
  402. * True if the mode was set successfully, or false otherwise.
  403. */
  404. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  405. struct drm_display_mode *mode,
  406. int x, int y,
  407. struct drm_framebuffer *old_fb)
  408. {
  409. struct drm_device *dev = crtc->dev;
  410. struct drm_display_mode *adjusted_mode, saved_mode;
  411. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  412. struct drm_encoder_helper_funcs *encoder_funcs;
  413. int saved_x, saved_y;
  414. struct drm_encoder *encoder;
  415. bool ret = true;
  416. bool depth_changed, bpp_changed;
  417. adjusted_mode = drm_mode_duplicate(dev, mode);
  418. crtc->enabled = drm_helper_crtc_in_use(crtc);
  419. if (!crtc->enabled)
  420. return true;
  421. if (old_fb && crtc->fb) {
  422. depth_changed = (old_fb->depth != crtc->fb->depth);
  423. bpp_changed = (old_fb->bits_per_pixel !=
  424. crtc->fb->bits_per_pixel);
  425. } else {
  426. depth_changed = true;
  427. bpp_changed = true;
  428. }
  429. saved_mode = crtc->mode;
  430. saved_x = crtc->x;
  431. saved_y = crtc->y;
  432. /* Update crtc values up front so the driver can rely on them for mode
  433. * setting.
  434. */
  435. crtc->mode = *mode;
  436. crtc->x = x;
  437. crtc->y = y;
  438. if (drm_mode_equal(&saved_mode, &crtc->mode)) {
  439. if (saved_x != crtc->x || saved_y != crtc->y ||
  440. depth_changed || bpp_changed) {
  441. crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y,
  442. old_fb);
  443. goto done;
  444. }
  445. }
  446. /* Pass our mode to the connectors and the CRTC to give them a chance to
  447. * adjust it according to limitations or connector properties, and also
  448. * a chance to reject the mode entirely.
  449. */
  450. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  451. if (encoder->crtc != crtc)
  452. continue;
  453. encoder_funcs = encoder->helper_private;
  454. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  455. adjusted_mode))) {
  456. goto done;
  457. }
  458. }
  459. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  460. goto done;
  461. }
  462. /* Prepare the encoders and CRTCs before setting the mode. */
  463. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  464. if (encoder->crtc != crtc)
  465. continue;
  466. encoder_funcs = encoder->helper_private;
  467. /* Disable the encoders as the first thing we do. */
  468. encoder_funcs->prepare(encoder);
  469. }
  470. crtc_funcs->prepare(crtc);
  471. /* Set up the DPLL and any encoders state that needs to adjust or depend
  472. * on the DPLL.
  473. */
  474. crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  475. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  476. if (encoder->crtc != crtc)
  477. continue;
  478. DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
  479. mode->name, mode->base.id);
  480. encoder_funcs = encoder->helper_private;
  481. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  482. }
  483. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  484. crtc_funcs->commit(crtc);
  485. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  486. if (encoder->crtc != crtc)
  487. continue;
  488. encoder_funcs = encoder->helper_private;
  489. encoder_funcs->commit(encoder);
  490. }
  491. /* XXX free adjustedmode */
  492. drm_mode_destroy(dev, adjusted_mode);
  493. /* FIXME: add subpixel order */
  494. done:
  495. if (!ret) {
  496. crtc->mode = saved_mode;
  497. crtc->x = saved_x;
  498. crtc->y = saved_y;
  499. }
  500. return ret;
  501. }
  502. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  503. /**
  504. * drm_crtc_helper_set_config - set a new config from userspace
  505. * @crtc: CRTC to setup
  506. * @crtc_info: user provided configuration
  507. * @new_mode: new mode to set
  508. * @connector_set: set of connectors for the new config
  509. * @fb: new framebuffer
  510. *
  511. * LOCKING:
  512. * Caller must hold mode config lock.
  513. *
  514. * Setup a new configuration, provided by the user in @crtc_info, and enable
  515. * it.
  516. *
  517. * RETURNS:
  518. * Zero. (FIXME)
  519. */
  520. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  521. {
  522. struct drm_device *dev;
  523. struct drm_crtc **save_crtcs, *new_crtc;
  524. struct drm_encoder **save_encoders, *new_encoder;
  525. struct drm_framebuffer *old_fb;
  526. bool save_enabled;
  527. bool mode_changed = false;
  528. bool fb_changed = false;
  529. struct drm_connector *connector;
  530. int count = 0, ro, fail = 0;
  531. struct drm_crtc_helper_funcs *crtc_funcs;
  532. int ret = 0;
  533. DRM_DEBUG("\n");
  534. if (!set)
  535. return -EINVAL;
  536. if (!set->crtc)
  537. return -EINVAL;
  538. if (!set->crtc->helper_private)
  539. return -EINVAL;
  540. crtc_funcs = set->crtc->helper_private;
  541. DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
  542. set->crtc, set->crtc->base.id, set->fb, set->connectors,
  543. (int)set->num_connectors, set->x, set->y);
  544. dev = set->crtc->dev;
  545. /* save previous config */
  546. save_enabled = set->crtc->enabled;
  547. /*
  548. * We do mode_config.num_connectors here since we'll look at the
  549. * CRTC and encoder associated with each connector later.
  550. */
  551. save_crtcs = kzalloc(dev->mode_config.num_connector *
  552. sizeof(struct drm_crtc *), GFP_KERNEL);
  553. if (!save_crtcs)
  554. return -ENOMEM;
  555. save_encoders = kzalloc(dev->mode_config.num_connector *
  556. sizeof(struct drm_encoders *), GFP_KERNEL);
  557. if (!save_encoders) {
  558. kfree(save_crtcs);
  559. return -ENOMEM;
  560. }
  561. /* We should be able to check here if the fb has the same properties
  562. * and then just flip_or_move it */
  563. if (set->crtc->fb != set->fb) {
  564. /* If we have no fb then treat it as a full mode set */
  565. if (set->crtc->fb == NULL)
  566. mode_changed = true;
  567. else if ((set->fb->bits_per_pixel !=
  568. set->crtc->fb->bits_per_pixel) ||
  569. set->fb->depth != set->crtc->fb->depth)
  570. fb_changed = true;
  571. else
  572. fb_changed = true;
  573. }
  574. if (set->x != set->crtc->x || set->y != set->crtc->y)
  575. fb_changed = true;
  576. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  577. DRM_DEBUG("modes are different\n");
  578. drm_mode_debug_printmodeline(&set->crtc->mode);
  579. drm_mode_debug_printmodeline(set->mode);
  580. mode_changed = true;
  581. }
  582. /* a) traverse passed in connector list and get encoders for them */
  583. count = 0;
  584. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  585. struct drm_connector_helper_funcs *connector_funcs =
  586. connector->helper_private;
  587. save_encoders[count++] = connector->encoder;
  588. new_encoder = connector->encoder;
  589. for (ro = 0; ro < set->num_connectors; ro++) {
  590. if (set->connectors[ro] == connector) {
  591. new_encoder = connector_funcs->best_encoder(connector);
  592. /* if we can't get an encoder for a connector
  593. we are setting now - then fail */
  594. if (new_encoder == NULL)
  595. /* don't break so fail path works correct */
  596. fail = 1;
  597. break;
  598. }
  599. }
  600. if (new_encoder != connector->encoder) {
  601. mode_changed = true;
  602. connector->encoder = new_encoder;
  603. }
  604. }
  605. if (fail) {
  606. ret = -EINVAL;
  607. goto fail_no_encoder;
  608. }
  609. count = 0;
  610. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  611. if (!connector->encoder)
  612. continue;
  613. save_crtcs[count++] = connector->encoder->crtc;
  614. if (connector->encoder->crtc == set->crtc)
  615. new_crtc = NULL;
  616. else
  617. new_crtc = connector->encoder->crtc;
  618. for (ro = 0; ro < set->num_connectors; ro++) {
  619. if (set->connectors[ro] == connector)
  620. new_crtc = set->crtc;
  621. }
  622. if (new_crtc != connector->encoder->crtc) {
  623. mode_changed = true;
  624. connector->encoder->crtc = new_crtc;
  625. }
  626. }
  627. /* mode_set_base is not a required function */
  628. if (fb_changed && !crtc_funcs->mode_set_base)
  629. mode_changed = true;
  630. if (mode_changed) {
  631. old_fb = set->crtc->fb;
  632. set->crtc->fb = set->fb;
  633. set->crtc->enabled = (set->mode != NULL);
  634. if (set->mode != NULL) {
  635. DRM_DEBUG("attempting to set mode from userspace\n");
  636. drm_mode_debug_printmodeline(set->mode);
  637. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  638. set->x, set->y,
  639. old_fb)) {
  640. ret = -EINVAL;
  641. goto fail_set_mode;
  642. }
  643. /* TODO are these needed? */
  644. set->crtc->desired_x = set->x;
  645. set->crtc->desired_y = set->y;
  646. set->crtc->desired_mode = set->mode;
  647. }
  648. drm_helper_disable_unused_functions(dev);
  649. } else if (fb_changed) {
  650. old_fb = set->crtc->fb;
  651. if (set->crtc->fb != set->fb)
  652. set->crtc->fb = set->fb;
  653. crtc_funcs->mode_set_base(set->crtc, set->x, set->y, old_fb);
  654. }
  655. kfree(save_encoders);
  656. kfree(save_crtcs);
  657. return 0;
  658. fail_set_mode:
  659. set->crtc->enabled = save_enabled;
  660. count = 0;
  661. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  662. connector->encoder->crtc = save_crtcs[count++];
  663. fail_no_encoder:
  664. kfree(save_crtcs);
  665. count = 0;
  666. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  667. connector->encoder = save_encoders[count++];
  668. }
  669. kfree(save_encoders);
  670. return ret;
  671. }
  672. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  673. bool drm_helper_plugged_event(struct drm_device *dev)
  674. {
  675. DRM_DEBUG("\n");
  676. drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
  677. dev->mode_config.max_height);
  678. drm_setup_crtcs(dev);
  679. /* alert the driver fb layer */
  680. dev->mode_config.funcs->fb_changed(dev);
  681. /* FIXME: send hotplug event */
  682. return true;
  683. }
  684. /**
  685. * drm_initial_config - setup a sane initial connector configuration
  686. * @dev: DRM device
  687. * @can_grow: this configuration is growable
  688. *
  689. * LOCKING:
  690. * Called at init time, must take mode config lock.
  691. *
  692. * Scan the CRTCs and connectors and try to put together an initial setup.
  693. * At the moment, this is a cloned configuration across all heads with
  694. * a new framebuffer object as the backing store.
  695. *
  696. * RETURNS:
  697. * Zero if everything went ok, nonzero otherwise.
  698. */
  699. bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
  700. {
  701. struct drm_connector *connector;
  702. int count = 0;
  703. count = drm_helper_probe_connector_modes(dev,
  704. dev->mode_config.max_width,
  705. dev->mode_config.max_height);
  706. /*
  707. * None of the available connectors had any modes, so add some
  708. * and try to light them up anyway
  709. */
  710. if (!count) {
  711. DRM_ERROR("connectors have no modes, using standard modes\n");
  712. list_for_each_entry(connector,
  713. &dev->mode_config.connector_list,
  714. head)
  715. drm_helper_add_std_modes(dev, connector);
  716. }
  717. drm_setup_crtcs(dev);
  718. /* alert the driver fb layer */
  719. dev->mode_config.funcs->fb_changed(dev);
  720. return 0;
  721. }
  722. EXPORT_SYMBOL(drm_helper_initial_config);
  723. /**
  724. * drm_hotplug_stage_two
  725. * @dev DRM device
  726. * @connector hotpluged connector
  727. *
  728. * LOCKING.
  729. * Caller must hold mode config lock, function might grab struct lock.
  730. *
  731. * Stage two of a hotplug.
  732. *
  733. * RETURNS:
  734. * Zero on success, errno on failure.
  735. */
  736. int drm_helper_hotplug_stage_two(struct drm_device *dev)
  737. {
  738. drm_helper_plugged_event(dev);
  739. return 0;
  740. }
  741. EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
  742. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  743. struct drm_mode_fb_cmd *mode_cmd)
  744. {
  745. fb->width = mode_cmd->width;
  746. fb->height = mode_cmd->height;
  747. fb->pitch = mode_cmd->pitch;
  748. fb->bits_per_pixel = mode_cmd->bpp;
  749. fb->depth = mode_cmd->depth;
  750. return 0;
  751. }
  752. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  753. int drm_helper_resume_force_mode(struct drm_device *dev)
  754. {
  755. struct drm_crtc *crtc;
  756. int ret;
  757. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  758. if (!crtc->enabled)
  759. continue;
  760. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  761. crtc->x, crtc->y, crtc->fb);
  762. if (ret == false)
  763. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  764. }
  765. return 0;
  766. }
  767. EXPORT_SYMBOL(drm_helper_resume_force_mode);