drm_crtc_helper.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. *
  5. * DRM core CRTC related functions
  6. *
  7. * Permission to use, copy, modify, distribute, and sell this software and its
  8. * documentation for any purpose is hereby granted without fee, provided that
  9. * the above copyright notice appear in all copies and that both that copyright
  10. * notice and this permission notice appear in supporting documentation, and
  11. * that the name of the copyright holders not be used in advertising or
  12. * publicity pertaining to distribution of the software without specific,
  13. * written prior permission. The copyright holders make no representations
  14. * about the suitability of this software for any purpose. It is provided "as
  15. * is" without express or implied warranty.
  16. *
  17. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  19. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  21. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  22. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. *
  25. * Authors:
  26. * Keith Packard
  27. * Eric Anholt <eric@anholt.net>
  28. * Dave Airlie <airlied@linux.ie>
  29. * Jesse Barnes <jesse.barnes@intel.com>
  30. */
  31. #include "drmP.h"
  32. #include "drm_crtc.h"
  33. #include "drm_crtc_helper.h"
  34. /*
  35. * Detailed mode info for 800x600@60Hz
  36. */
  37. static struct drm_display_mode std_modes[] = {
  38. { DRM_MODE("800x600", DRM_MODE_TYPE_DEFAULT, 40000, 800, 840,
  39. 968, 1056, 0, 600, 601, 605, 628, 0,
  40. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  41. };
  42. static void drm_mode_validate_flag(struct drm_connector *connector,
  43. int flags)
  44. {
  45. struct drm_display_mode *mode, *t;
  46. if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
  47. return;
  48. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  49. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  50. !(flags & DRM_MODE_FLAG_INTERLACE))
  51. mode->status = MODE_NO_INTERLACE;
  52. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  53. !(flags & DRM_MODE_FLAG_DBLSCAN))
  54. mode->status = MODE_NO_DBLESCAN;
  55. }
  56. return;
  57. }
  58. /**
  59. * drm_helper_probe_connector_modes - get complete set of display modes
  60. * @dev: DRM device
  61. * @maxX: max width for modes
  62. * @maxY: max height for modes
  63. *
  64. * LOCKING:
  65. * Caller must hold mode config lock.
  66. *
  67. * Based on @dev's mode_config layout, scan all the connectors and try to detect
  68. * modes on them. Modes will first be added to the connector's probed_modes
  69. * list, then culled (based on validity and the @maxX, @maxY parameters) and
  70. * put into the normal modes list.
  71. *
  72. * Intended to be used either at bootup time or when major configuration
  73. * changes have occurred.
  74. *
  75. * FIXME: take into account monitor limits
  76. *
  77. * RETURNS:
  78. * Number of modes found on @connector.
  79. */
  80. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  81. uint32_t maxX, uint32_t maxY)
  82. {
  83. struct drm_device *dev = connector->dev;
  84. struct drm_display_mode *mode, *t;
  85. struct drm_connector_helper_funcs *connector_funcs =
  86. connector->helper_private;
  87. int count = 0;
  88. int mode_flags = 0;
  89. DRM_DEBUG("%s\n", drm_get_connector_name(connector));
  90. /* set all modes to the unverified state */
  91. list_for_each_entry_safe(mode, t, &connector->modes, head)
  92. mode->status = MODE_UNVERIFIED;
  93. connector->status = connector->funcs->detect(connector);
  94. if (connector->status == connector_status_disconnected) {
  95. DRM_DEBUG("%s is disconnected\n",
  96. drm_get_connector_name(connector));
  97. /* TODO set EDID to NULL */
  98. return 0;
  99. }
  100. count = (*connector_funcs->get_modes)(connector);
  101. if (!count)
  102. return 0;
  103. drm_mode_connector_list_update(connector);
  104. if (maxX && maxY)
  105. drm_mode_validate_size(dev, &connector->modes, maxX,
  106. maxY, 0);
  107. if (connector->interlace_allowed)
  108. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  109. if (connector->doublescan_allowed)
  110. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  111. drm_mode_validate_flag(connector, mode_flags);
  112. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  113. if (mode->status == MODE_OK)
  114. mode->status = connector_funcs->mode_valid(connector,
  115. mode);
  116. }
  117. drm_mode_prune_invalid(dev, &connector->modes, true);
  118. if (list_empty(&connector->modes))
  119. return 0;
  120. drm_mode_sort(&connector->modes);
  121. DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
  122. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  123. mode->vrefresh = drm_mode_vrefresh(mode);
  124. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  125. drm_mode_debug_printmodeline(mode);
  126. }
  127. return count;
  128. }
  129. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  130. int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
  131. uint32_t maxY)
  132. {
  133. struct drm_connector *connector;
  134. int count = 0;
  135. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  136. count += drm_helper_probe_single_connector_modes(connector,
  137. maxX, maxY);
  138. }
  139. return count;
  140. }
  141. EXPORT_SYMBOL(drm_helper_probe_connector_modes);
  142. static void drm_helper_add_std_modes(struct drm_device *dev,
  143. struct drm_connector *connector)
  144. {
  145. struct drm_display_mode *mode, *t;
  146. int i;
  147. for (i = 0; i < ARRAY_SIZE(std_modes); i++) {
  148. struct drm_display_mode *stdmode;
  149. /*
  150. * When no valid EDID modes are available we end up
  151. * here and bailed in the past, now we add some standard
  152. * modes and move on.
  153. */
  154. stdmode = drm_mode_duplicate(dev, &std_modes[i]);
  155. drm_mode_probed_add(connector, stdmode);
  156. drm_mode_list_concat(&connector->probed_modes,
  157. &connector->modes);
  158. DRM_DEBUG("Adding mode %s to %s\n", stdmode->name,
  159. drm_get_connector_name(connector));
  160. }
  161. drm_mode_sort(&connector->modes);
  162. DRM_DEBUG("Added std modes on %s\n", drm_get_connector_name(connector));
  163. list_for_each_entry_safe(mode, t, &connector->modes, head) {
  164. mode->vrefresh = drm_mode_vrefresh(mode);
  165. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  166. drm_mode_debug_printmodeline(mode);
  167. }
  168. }
  169. /**
  170. * drm_helper_encoder_in_use - check if a given encoder is in use
  171. * @encoder: encoder to check
  172. *
  173. * LOCKING:
  174. * Caller must hold mode config lock.
  175. *
  176. * Walk @encoders's DRM device's mode_config and see if it's in use.
  177. *
  178. * RETURNS:
  179. * True if @encoder is part of the mode_config, false otherwise.
  180. */
  181. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  182. {
  183. struct drm_connector *connector;
  184. struct drm_device *dev = encoder->dev;
  185. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  186. if (connector->encoder == encoder)
  187. return true;
  188. return false;
  189. }
  190. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  191. /**
  192. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  193. * @crtc: CRTC to check
  194. *
  195. * LOCKING:
  196. * Caller must hold mode config lock.
  197. *
  198. * Walk @crtc's DRM device's mode_config and see if it's in use.
  199. *
  200. * RETURNS:
  201. * True if @crtc is part of the mode_config, false otherwise.
  202. */
  203. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  204. {
  205. struct drm_encoder *encoder;
  206. struct drm_device *dev = crtc->dev;
  207. /* FIXME: Locking around list access? */
  208. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  209. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  210. return true;
  211. return false;
  212. }
  213. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  214. /**
  215. * drm_disable_unused_functions - disable unused objects
  216. * @dev: DRM device
  217. *
  218. * LOCKING:
  219. * Caller must hold mode config lock.
  220. *
  221. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  222. * by calling its dpms function, which should power it off.
  223. */
  224. void drm_helper_disable_unused_functions(struct drm_device *dev)
  225. {
  226. struct drm_encoder *encoder;
  227. struct drm_encoder_helper_funcs *encoder_funcs;
  228. struct drm_crtc *crtc;
  229. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  230. encoder_funcs = encoder->helper_private;
  231. if (!drm_helper_encoder_in_use(encoder))
  232. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  233. }
  234. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  235. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  236. crtc->enabled = drm_helper_crtc_in_use(crtc);
  237. if (!crtc->enabled) {
  238. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  239. crtc->fb = NULL;
  240. }
  241. }
  242. }
  243. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  244. static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
  245. {
  246. struct drm_display_mode *mode;
  247. list_for_each_entry(mode, &connector->modes, head) {
  248. if (drm_mode_width(mode) > width ||
  249. drm_mode_height(mode) > height)
  250. continue;
  251. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  252. return mode;
  253. }
  254. return NULL;
  255. }
  256. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  257. {
  258. bool enable;
  259. if (strict) {
  260. enable = connector->status == connector_status_connected;
  261. } else {
  262. enable = connector->status != connector_status_disconnected;
  263. }
  264. return enable;
  265. }
  266. static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
  267. {
  268. bool any_enabled = false;
  269. struct drm_connector *connector;
  270. int i = 0;
  271. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  272. enabled[i] = drm_connector_enabled(connector, true);
  273. DRM_DEBUG("connector %d enabled? %s\n", connector->base.id,
  274. enabled[i] ? "yes" : "no");
  275. any_enabled |= enabled[i];
  276. i++;
  277. }
  278. if (any_enabled)
  279. return;
  280. i = 0;
  281. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  282. enabled[i] = drm_connector_enabled(connector, false);
  283. i++;
  284. }
  285. }
  286. static bool drm_target_preferred(struct drm_device *dev,
  287. struct drm_display_mode **modes,
  288. bool *enabled, int width, int height)
  289. {
  290. struct drm_connector *connector;
  291. int i = 0;
  292. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  293. if (enabled[i] == false) {
  294. i++;
  295. continue;
  296. }
  297. DRM_DEBUG("looking for preferred mode on connector %d\n",
  298. connector->base.id);
  299. modes[i] = drm_has_preferred_mode(connector, width, height);
  300. /* No preferred modes, pick one off the list */
  301. if (!modes[i] && !list_empty(&connector->modes)) {
  302. list_for_each_entry(modes[i], &connector->modes, head)
  303. break;
  304. }
  305. DRM_DEBUG("found mode %s\n", modes[i] ? modes[i]->name :
  306. "none");
  307. i++;
  308. }
  309. return true;
  310. }
  311. static int drm_pick_crtcs(struct drm_device *dev,
  312. struct drm_crtc **best_crtcs,
  313. struct drm_display_mode **modes,
  314. int n, int width, int height)
  315. {
  316. int c, o;
  317. struct drm_connector *connector;
  318. struct drm_connector_helper_funcs *connector_funcs;
  319. struct drm_encoder *encoder;
  320. struct drm_crtc *best_crtc;
  321. int my_score, best_score, score;
  322. struct drm_crtc **crtcs, *crtc;
  323. if (n == dev->mode_config.num_connector)
  324. return 0;
  325. c = 0;
  326. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  327. if (c == n)
  328. break;
  329. c++;
  330. }
  331. best_crtcs[n] = NULL;
  332. best_crtc = NULL;
  333. best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
  334. if (modes[n] == NULL)
  335. return best_score;
  336. crtcs = kmalloc(dev->mode_config.num_connector *
  337. sizeof(struct drm_crtc *), GFP_KERNEL);
  338. if (!crtcs)
  339. return best_score;
  340. my_score = 1;
  341. if (connector->status == connector_status_connected)
  342. my_score++;
  343. if (drm_has_preferred_mode(connector, width, height))
  344. my_score++;
  345. connector_funcs = connector->helper_private;
  346. encoder = connector_funcs->best_encoder(connector);
  347. if (!encoder)
  348. goto out;
  349. connector->encoder = encoder;
  350. /* select a crtc for this connector and then attempt to configure
  351. remaining connectors */
  352. c = 0;
  353. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  354. if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
  355. c++;
  356. continue;
  357. }
  358. for (o = 0; o < n; o++)
  359. if (best_crtcs[o] == crtc)
  360. break;
  361. if (o < n) {
  362. /* ignore cloning for now */
  363. c++;
  364. continue;
  365. }
  366. crtcs[n] = crtc;
  367. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
  368. score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
  369. width, height);
  370. if (score > best_score) {
  371. best_crtc = crtc;
  372. best_score = score;
  373. memcpy(best_crtcs, crtcs,
  374. dev->mode_config.num_connector *
  375. sizeof(struct drm_crtc *));
  376. }
  377. c++;
  378. }
  379. out:
  380. kfree(crtcs);
  381. return best_score;
  382. }
  383. static void drm_setup_crtcs(struct drm_device *dev)
  384. {
  385. struct drm_crtc **crtcs;
  386. struct drm_display_mode **modes;
  387. struct drm_encoder *encoder;
  388. struct drm_connector *connector;
  389. bool *enabled;
  390. int width, height;
  391. int i, ret;
  392. DRM_DEBUG("\n");
  393. width = dev->mode_config.max_width;
  394. height = dev->mode_config.max_height;
  395. /* clean out all the encoder/crtc combos */
  396. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  397. encoder->crtc = NULL;
  398. }
  399. crtcs = kcalloc(dev->mode_config.num_connector,
  400. sizeof(struct drm_crtc *), GFP_KERNEL);
  401. modes = kcalloc(dev->mode_config.num_connector,
  402. sizeof(struct drm_display_mode *), GFP_KERNEL);
  403. enabled = kcalloc(dev->mode_config.num_connector,
  404. sizeof(bool), GFP_KERNEL);
  405. drm_enable_connectors(dev, enabled);
  406. ret = drm_target_preferred(dev, modes, enabled, width, height);
  407. if (!ret)
  408. DRM_ERROR("Unable to find initial modes\n");
  409. DRM_DEBUG("picking CRTCs for %dx%d config\n", width, height);
  410. drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
  411. i = 0;
  412. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  413. struct drm_display_mode *mode = modes[i];
  414. struct drm_crtc *crtc = crtcs[i];
  415. if (connector->encoder == NULL) {
  416. i++;
  417. continue;
  418. }
  419. if (mode && crtc) {
  420. DRM_DEBUG("desired mode %s set on crtc %d\n",
  421. mode->name, crtc->base.id);
  422. crtc->desired_mode = mode;
  423. connector->encoder->crtc = crtc;
  424. } else
  425. connector->encoder->crtc = NULL;
  426. i++;
  427. }
  428. kfree(crtcs);
  429. kfree(modes);
  430. kfree(enabled);
  431. }
  432. /**
  433. * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  434. * @encoder: encoder to test
  435. * @crtc: crtc to test
  436. *
  437. * Return false if @encoder can't be driven by @crtc, true otherwise.
  438. */
  439. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  440. struct drm_crtc *crtc)
  441. {
  442. struct drm_device *dev;
  443. struct drm_crtc *tmp;
  444. int crtc_mask = 1;
  445. WARN(!crtc, "checking null crtc?");
  446. dev = crtc->dev;
  447. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  448. if (tmp == crtc)
  449. break;
  450. crtc_mask <<= 1;
  451. }
  452. if (encoder->possible_crtcs & crtc_mask)
  453. return true;
  454. return false;
  455. }
  456. /*
  457. * Check the CRTC we're going to map each output to vs. its current
  458. * CRTC. If they don't match, we have to disable the output and the CRTC
  459. * since the driver will have to re-route things.
  460. */
  461. static void
  462. drm_crtc_prepare_encoders(struct drm_device *dev)
  463. {
  464. struct drm_encoder_helper_funcs *encoder_funcs;
  465. struct drm_encoder *encoder;
  466. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  467. encoder_funcs = encoder->helper_private;
  468. /* Disable unused encoders */
  469. if (encoder->crtc == NULL)
  470. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  471. /* Disable encoders whose CRTC is about to change */
  472. if (encoder_funcs->get_crtc &&
  473. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  474. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  475. }
  476. }
  477. /**
  478. * drm_crtc_set_mode - set a mode
  479. * @crtc: CRTC to program
  480. * @mode: mode to use
  481. * @x: width of mode
  482. * @y: height of mode
  483. *
  484. * LOCKING:
  485. * Caller must hold mode config lock.
  486. *
  487. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  488. * to fixup or reject the mode prior to trying to set it.
  489. *
  490. * RETURNS:
  491. * True if the mode was set successfully, or false otherwise.
  492. */
  493. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  494. struct drm_display_mode *mode,
  495. int x, int y,
  496. struct drm_framebuffer *old_fb)
  497. {
  498. struct drm_device *dev = crtc->dev;
  499. struct drm_display_mode *adjusted_mode, saved_mode;
  500. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  501. struct drm_encoder_helper_funcs *encoder_funcs;
  502. int saved_x, saved_y;
  503. struct drm_encoder *encoder;
  504. bool ret = true;
  505. adjusted_mode = drm_mode_duplicate(dev, mode);
  506. crtc->enabled = drm_helper_crtc_in_use(crtc);
  507. if (!crtc->enabled)
  508. return true;
  509. saved_mode = crtc->mode;
  510. saved_x = crtc->x;
  511. saved_y = crtc->y;
  512. /* Update crtc values up front so the driver can rely on them for mode
  513. * setting.
  514. */
  515. crtc->mode = *mode;
  516. crtc->x = x;
  517. crtc->y = y;
  518. /* Pass our mode to the connectors and the CRTC to give them a chance to
  519. * adjust it according to limitations or connector properties, and also
  520. * a chance to reject the mode entirely.
  521. */
  522. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  523. if (encoder->crtc != crtc)
  524. continue;
  525. encoder_funcs = encoder->helper_private;
  526. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  527. adjusted_mode))) {
  528. goto done;
  529. }
  530. }
  531. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  532. goto done;
  533. }
  534. /* Prepare the encoders and CRTCs before setting the mode. */
  535. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  536. if (encoder->crtc != crtc)
  537. continue;
  538. encoder_funcs = encoder->helper_private;
  539. /* Disable the encoders as the first thing we do. */
  540. encoder_funcs->prepare(encoder);
  541. }
  542. drm_crtc_prepare_encoders(dev);
  543. crtc_funcs->prepare(crtc);
  544. /* Set up the DPLL and any encoders state that needs to adjust or depend
  545. * on the DPLL.
  546. */
  547. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  548. if (!ret)
  549. goto done;
  550. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  551. if (encoder->crtc != crtc)
  552. continue;
  553. DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
  554. mode->name, mode->base.id);
  555. encoder_funcs = encoder->helper_private;
  556. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  557. }
  558. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  559. crtc_funcs->commit(crtc);
  560. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  561. if (encoder->crtc != crtc)
  562. continue;
  563. encoder_funcs = encoder->helper_private;
  564. encoder_funcs->commit(encoder);
  565. }
  566. /* XXX free adjustedmode */
  567. drm_mode_destroy(dev, adjusted_mode);
  568. /* FIXME: add subpixel order */
  569. done:
  570. if (!ret) {
  571. crtc->mode = saved_mode;
  572. crtc->x = saved_x;
  573. crtc->y = saved_y;
  574. }
  575. return ret;
  576. }
  577. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  578. /**
  579. * drm_crtc_helper_set_config - set a new config from userspace
  580. * @crtc: CRTC to setup
  581. * @crtc_info: user provided configuration
  582. * @new_mode: new mode to set
  583. * @connector_set: set of connectors for the new config
  584. * @fb: new framebuffer
  585. *
  586. * LOCKING:
  587. * Caller must hold mode config lock.
  588. *
  589. * Setup a new configuration, provided by the user in @crtc_info, and enable
  590. * it.
  591. *
  592. * RETURNS:
  593. * Zero. (FIXME)
  594. */
  595. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  596. {
  597. struct drm_device *dev;
  598. struct drm_crtc **save_crtcs, *new_crtc;
  599. struct drm_encoder **save_encoders, *new_encoder;
  600. struct drm_framebuffer *old_fb = NULL;
  601. bool save_enabled;
  602. bool mode_changed = false;
  603. bool fb_changed = false;
  604. struct drm_connector *connector;
  605. int count = 0, ro, fail = 0;
  606. struct drm_crtc_helper_funcs *crtc_funcs;
  607. int ret = 0;
  608. DRM_DEBUG("\n");
  609. if (!set)
  610. return -EINVAL;
  611. if (!set->crtc)
  612. return -EINVAL;
  613. if (!set->crtc->helper_private)
  614. return -EINVAL;
  615. crtc_funcs = set->crtc->helper_private;
  616. DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
  617. set->crtc, set->crtc->base.id, set->fb, set->connectors,
  618. (int)set->num_connectors, set->x, set->y);
  619. dev = set->crtc->dev;
  620. /* save previous config */
  621. save_enabled = set->crtc->enabled;
  622. /*
  623. * We do mode_config.num_connectors here since we'll look at the
  624. * CRTC and encoder associated with each connector later.
  625. */
  626. save_crtcs = kzalloc(dev->mode_config.num_connector *
  627. sizeof(struct drm_crtc *), GFP_KERNEL);
  628. if (!save_crtcs)
  629. return -ENOMEM;
  630. save_encoders = kzalloc(dev->mode_config.num_connector *
  631. sizeof(struct drm_encoders *), GFP_KERNEL);
  632. if (!save_encoders) {
  633. kfree(save_crtcs);
  634. return -ENOMEM;
  635. }
  636. /* We should be able to check here if the fb has the same properties
  637. * and then just flip_or_move it */
  638. if (set->crtc->fb != set->fb) {
  639. /* If we have no fb then treat it as a full mode set */
  640. if (set->crtc->fb == NULL) {
  641. DRM_DEBUG("crtc has no fb, full mode set\n");
  642. mode_changed = true;
  643. } else if ((set->fb->bits_per_pixel !=
  644. set->crtc->fb->bits_per_pixel) ||
  645. set->fb->depth != set->crtc->fb->depth)
  646. fb_changed = true;
  647. else
  648. fb_changed = true;
  649. }
  650. if (set->x != set->crtc->x || set->y != set->crtc->y)
  651. fb_changed = true;
  652. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  653. DRM_DEBUG("modes are different, full mode set\n");
  654. drm_mode_debug_printmodeline(&set->crtc->mode);
  655. drm_mode_debug_printmodeline(set->mode);
  656. mode_changed = true;
  657. }
  658. /* a) traverse passed in connector list and get encoders for them */
  659. count = 0;
  660. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  661. struct drm_connector_helper_funcs *connector_funcs =
  662. connector->helper_private;
  663. save_encoders[count++] = connector->encoder;
  664. new_encoder = connector->encoder;
  665. for (ro = 0; ro < set->num_connectors; ro++) {
  666. if (set->connectors[ro] == connector) {
  667. new_encoder = connector_funcs->best_encoder(connector);
  668. /* if we can't get an encoder for a connector
  669. we are setting now - then fail */
  670. if (new_encoder == NULL)
  671. /* don't break so fail path works correct */
  672. fail = 1;
  673. break;
  674. }
  675. }
  676. if (new_encoder != connector->encoder) {
  677. DRM_DEBUG("encoder changed, full mode switch\n");
  678. mode_changed = true;
  679. connector->encoder = new_encoder;
  680. }
  681. }
  682. if (fail) {
  683. ret = -EINVAL;
  684. goto fail_no_encoder;
  685. }
  686. count = 0;
  687. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  688. if (!connector->encoder)
  689. continue;
  690. save_crtcs[count++] = connector->encoder->crtc;
  691. if (connector->encoder->crtc == set->crtc)
  692. new_crtc = NULL;
  693. else
  694. new_crtc = connector->encoder->crtc;
  695. for (ro = 0; ro < set->num_connectors; ro++) {
  696. if (set->connectors[ro] == connector)
  697. new_crtc = set->crtc;
  698. }
  699. /* Make sure the new CRTC will work with the encoder */
  700. if (new_crtc &&
  701. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  702. ret = -EINVAL;
  703. goto fail_set_mode;
  704. }
  705. if (new_crtc != connector->encoder->crtc) {
  706. DRM_DEBUG("crtc changed, full mode switch\n");
  707. mode_changed = true;
  708. connector->encoder->crtc = new_crtc;
  709. }
  710. DRM_DEBUG("setting connector %d crtc to %p\n",
  711. connector->base.id, new_crtc);
  712. }
  713. /* mode_set_base is not a required function */
  714. if (fb_changed && !crtc_funcs->mode_set_base)
  715. mode_changed = true;
  716. if (mode_changed) {
  717. old_fb = set->crtc->fb;
  718. set->crtc->fb = set->fb;
  719. set->crtc->enabled = (set->mode != NULL);
  720. if (set->mode != NULL) {
  721. DRM_DEBUG("attempting to set mode from userspace\n");
  722. drm_mode_debug_printmodeline(set->mode);
  723. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  724. set->x, set->y,
  725. old_fb)) {
  726. DRM_ERROR("failed to set mode on crtc %p\n",
  727. set->crtc);
  728. ret = -EINVAL;
  729. goto fail_set_mode;
  730. }
  731. /* TODO are these needed? */
  732. set->crtc->desired_x = set->x;
  733. set->crtc->desired_y = set->y;
  734. set->crtc->desired_mode = set->mode;
  735. }
  736. drm_helper_disable_unused_functions(dev);
  737. } else if (fb_changed) {
  738. old_fb = set->crtc->fb;
  739. if (set->crtc->fb != set->fb)
  740. set->crtc->fb = set->fb;
  741. ret = crtc_funcs->mode_set_base(set->crtc,
  742. set->x, set->y, old_fb);
  743. if (ret != 0)
  744. goto fail_set_mode;
  745. }
  746. kfree(save_encoders);
  747. kfree(save_crtcs);
  748. return 0;
  749. fail_set_mode:
  750. set->crtc->enabled = save_enabled;
  751. set->crtc->fb = old_fb;
  752. count = 0;
  753. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  754. if (!connector->encoder)
  755. continue;
  756. connector->encoder->crtc = save_crtcs[count++];
  757. }
  758. fail_no_encoder:
  759. kfree(save_crtcs);
  760. count = 0;
  761. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  762. connector->encoder = save_encoders[count++];
  763. }
  764. kfree(save_encoders);
  765. return ret;
  766. }
  767. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  768. bool drm_helper_plugged_event(struct drm_device *dev)
  769. {
  770. DRM_DEBUG("\n");
  771. drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
  772. dev->mode_config.max_height);
  773. drm_setup_crtcs(dev);
  774. /* alert the driver fb layer */
  775. dev->mode_config.funcs->fb_changed(dev);
  776. /* FIXME: send hotplug event */
  777. return true;
  778. }
  779. /**
  780. * drm_initial_config - setup a sane initial connector configuration
  781. * @dev: DRM device
  782. *
  783. * LOCKING:
  784. * Called at init time, must take mode config lock.
  785. *
  786. * Scan the CRTCs and connectors and try to put together an initial setup.
  787. * At the moment, this is a cloned configuration across all heads with
  788. * a new framebuffer object as the backing store.
  789. *
  790. * RETURNS:
  791. * Zero if everything went ok, nonzero otherwise.
  792. */
  793. bool drm_helper_initial_config(struct drm_device *dev)
  794. {
  795. struct drm_connector *connector;
  796. int count = 0;
  797. count = drm_helper_probe_connector_modes(dev,
  798. dev->mode_config.max_width,
  799. dev->mode_config.max_height);
  800. /*
  801. * None of the available connectors had any modes, so add some
  802. * and try to light them up anyway
  803. */
  804. if (!count) {
  805. DRM_ERROR("connectors have no modes, using standard modes\n");
  806. list_for_each_entry(connector,
  807. &dev->mode_config.connector_list,
  808. head)
  809. drm_helper_add_std_modes(dev, connector);
  810. }
  811. drm_setup_crtcs(dev);
  812. /* alert the driver fb layer */
  813. dev->mode_config.funcs->fb_changed(dev);
  814. return 0;
  815. }
  816. EXPORT_SYMBOL(drm_helper_initial_config);
  817. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  818. {
  819. int dpms = DRM_MODE_DPMS_OFF;
  820. struct drm_connector *connector;
  821. struct drm_device *dev = encoder->dev;
  822. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  823. if (connector->encoder == encoder)
  824. if (connector->dpms < dpms)
  825. dpms = connector->dpms;
  826. return dpms;
  827. }
  828. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  829. {
  830. int dpms = DRM_MODE_DPMS_OFF;
  831. struct drm_connector *connector;
  832. struct drm_device *dev = crtc->dev;
  833. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  834. if (connector->encoder && connector->encoder->crtc == crtc)
  835. if (connector->dpms < dpms)
  836. dpms = connector->dpms;
  837. return dpms;
  838. }
  839. /**
  840. * drm_helper_connector_dpms
  841. * @connector affected connector
  842. * @mode DPMS mode
  843. *
  844. * Calls the low-level connector DPMS function, then
  845. * calls appropriate encoder and crtc DPMS functions as well
  846. */
  847. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  848. {
  849. struct drm_encoder *encoder = connector->encoder;
  850. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  851. int old_dpms;
  852. if (mode == connector->dpms)
  853. return;
  854. old_dpms = connector->dpms;
  855. connector->dpms = mode;
  856. /* from off to on, do crtc then encoder */
  857. if (mode < old_dpms) {
  858. if (crtc) {
  859. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  860. if (crtc_funcs->dpms)
  861. (*crtc_funcs->dpms) (crtc,
  862. drm_helper_choose_crtc_dpms(crtc));
  863. }
  864. if (encoder) {
  865. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  866. if (encoder_funcs->dpms)
  867. (*encoder_funcs->dpms) (encoder,
  868. drm_helper_choose_encoder_dpms(encoder));
  869. }
  870. }
  871. /* from on to off, do encoder then crtc */
  872. if (mode > old_dpms) {
  873. if (encoder) {
  874. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  875. if (encoder_funcs->dpms)
  876. (*encoder_funcs->dpms) (encoder,
  877. drm_helper_choose_encoder_dpms(encoder));
  878. }
  879. if (crtc) {
  880. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  881. if (crtc_funcs->dpms)
  882. (*crtc_funcs->dpms) (crtc,
  883. drm_helper_choose_crtc_dpms(crtc));
  884. }
  885. }
  886. return;
  887. }
  888. EXPORT_SYMBOL(drm_helper_connector_dpms);
  889. /**
  890. * drm_hotplug_stage_two
  891. * @dev DRM device
  892. * @connector hotpluged connector
  893. *
  894. * LOCKING.
  895. * Caller must hold mode config lock, function might grab struct lock.
  896. *
  897. * Stage two of a hotplug.
  898. *
  899. * RETURNS:
  900. * Zero on success, errno on failure.
  901. */
  902. int drm_helper_hotplug_stage_two(struct drm_device *dev)
  903. {
  904. drm_helper_plugged_event(dev);
  905. return 0;
  906. }
  907. EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
  908. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  909. struct drm_mode_fb_cmd *mode_cmd)
  910. {
  911. fb->width = mode_cmd->width;
  912. fb->height = mode_cmd->height;
  913. fb->pitch = mode_cmd->pitch;
  914. fb->bits_per_pixel = mode_cmd->bpp;
  915. fb->depth = mode_cmd->depth;
  916. return 0;
  917. }
  918. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  919. int drm_helper_resume_force_mode(struct drm_device *dev)
  920. {
  921. struct drm_crtc *crtc;
  922. int ret;
  923. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  924. if (!crtc->enabled)
  925. continue;
  926. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  927. crtc->x, crtc->y, crtc->fb);
  928. if (ret == false)
  929. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  930. }
  931. /* disable the unused connectors while restoring the modesetting */
  932. drm_helper_disable_unused_functions(dev);
  933. return 0;
  934. }
  935. EXPORT_SYMBOL(drm_helper_resume_force_mode);