drm_crtc_helper.c 29 KB

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