drm_crtc_helper.c 31 KB

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