drm_crtc_helper.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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, 1024, 768);
  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_helper_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;
  251. if (!fb_help_conn)
  252. return false;
  253. cmdline_mode = &fb_help_conn->cmdline_mode;
  254. return cmdline_mode->specified;
  255. }
  256. static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
  257. {
  258. struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
  259. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  260. struct drm_display_mode *mode = NULL;
  261. if (!fb_help_conn)
  262. return mode;
  263. cmdline_mode = &fb_help_conn->cmdline_mode;
  264. if (cmdline_mode->specified == false)
  265. return mode;
  266. /* attempt to find a matching mode in the list of modes
  267. * we have gotten so far, if not add a CVT mode that conforms
  268. */
  269. if (cmdline_mode->rb || cmdline_mode->margins)
  270. goto create_mode;
  271. list_for_each_entry(mode, &connector->modes, head) {
  272. /* check width/height */
  273. if (mode->hdisplay != cmdline_mode->xres ||
  274. mode->vdisplay != cmdline_mode->yres)
  275. continue;
  276. if (cmdline_mode->refresh_specified) {
  277. if (mode->vrefresh != cmdline_mode->refresh)
  278. continue;
  279. }
  280. if (cmdline_mode->interlace) {
  281. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  282. continue;
  283. }
  284. return mode;
  285. }
  286. create_mode:
  287. mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
  288. cmdline_mode->yres,
  289. cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
  290. cmdline_mode->rb, cmdline_mode->interlace,
  291. cmdline_mode->margins);
  292. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  293. list_add(&mode->head, &connector->modes);
  294. return mode;
  295. }
  296. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  297. {
  298. bool enable;
  299. if (strict) {
  300. enable = connector->status == connector_status_connected;
  301. } else {
  302. enable = connector->status != connector_status_disconnected;
  303. }
  304. return enable;
  305. }
  306. static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
  307. {
  308. bool any_enabled = false;
  309. struct drm_connector *connector;
  310. int i = 0;
  311. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  312. enabled[i] = drm_connector_enabled(connector, true);
  313. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  314. enabled[i] ? "yes" : "no");
  315. any_enabled |= enabled[i];
  316. i++;
  317. }
  318. if (any_enabled)
  319. return;
  320. i = 0;
  321. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  322. enabled[i] = drm_connector_enabled(connector, false);
  323. i++;
  324. }
  325. }
  326. static bool drm_target_preferred(struct drm_device *dev,
  327. struct drm_display_mode **modes,
  328. bool *enabled, int width, int height)
  329. {
  330. struct drm_connector *connector;
  331. int i = 0;
  332. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  333. if (enabled[i] == false) {
  334. i++;
  335. continue;
  336. }
  337. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  338. connector->base.id);
  339. /* got for command line mode first */
  340. modes[i] = drm_pick_cmdline_mode(connector, width, height);
  341. if (!modes[i]) {
  342. DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  343. connector->base.id);
  344. modes[i] = drm_has_preferred_mode(connector, width, height);
  345. }
  346. /* No preferred modes, pick one off the list */
  347. if (!modes[i] && !list_empty(&connector->modes)) {
  348. list_for_each_entry(modes[i], &connector->modes, head)
  349. break;
  350. }
  351. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  352. "none");
  353. i++;
  354. }
  355. return true;
  356. }
  357. static int drm_pick_crtcs(struct drm_device *dev,
  358. struct drm_crtc **best_crtcs,
  359. struct drm_display_mode **modes,
  360. int n, int width, int height)
  361. {
  362. int c, o;
  363. struct drm_connector *connector;
  364. struct drm_connector_helper_funcs *connector_funcs;
  365. struct drm_encoder *encoder;
  366. struct drm_crtc *best_crtc;
  367. int my_score, best_score, score;
  368. struct drm_crtc **crtcs, *crtc;
  369. if (n == dev->mode_config.num_connector)
  370. return 0;
  371. c = 0;
  372. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  373. if (c == n)
  374. break;
  375. c++;
  376. }
  377. best_crtcs[n] = NULL;
  378. best_crtc = NULL;
  379. best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
  380. if (modes[n] == NULL)
  381. return best_score;
  382. crtcs = kmalloc(dev->mode_config.num_connector *
  383. sizeof(struct drm_crtc *), GFP_KERNEL);
  384. if (!crtcs)
  385. return best_score;
  386. my_score = 1;
  387. if (connector->status == connector_status_connected)
  388. my_score++;
  389. if (drm_has_cmdline_mode(connector))
  390. my_score++;
  391. if (drm_has_preferred_mode(connector, width, height))
  392. my_score++;
  393. connector_funcs = connector->helper_private;
  394. encoder = connector_funcs->best_encoder(connector);
  395. if (!encoder)
  396. goto out;
  397. connector->encoder = encoder;
  398. /* select a crtc for this connector and then attempt to configure
  399. remaining connectors */
  400. c = 0;
  401. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  402. if ((encoder->possible_crtcs & (1 << c)) == 0) {
  403. c++;
  404. continue;
  405. }
  406. for (o = 0; o < n; o++)
  407. if (best_crtcs[o] == crtc)
  408. break;
  409. if (o < n) {
  410. /* ignore cloning for now */
  411. c++;
  412. continue;
  413. }
  414. crtcs[n] = crtc;
  415. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
  416. score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
  417. width, height);
  418. if (score > best_score) {
  419. best_crtc = crtc;
  420. best_score = score;
  421. memcpy(best_crtcs, crtcs,
  422. dev->mode_config.num_connector *
  423. sizeof(struct drm_crtc *));
  424. }
  425. c++;
  426. }
  427. out:
  428. kfree(crtcs);
  429. return best_score;
  430. }
  431. static void drm_setup_crtcs(struct drm_device *dev)
  432. {
  433. struct drm_crtc **crtcs;
  434. struct drm_display_mode **modes;
  435. struct drm_encoder *encoder;
  436. struct drm_connector *connector;
  437. bool *enabled;
  438. int width, height;
  439. int i, ret;
  440. DRM_DEBUG_KMS("\n");
  441. width = dev->mode_config.max_width;
  442. height = dev->mode_config.max_height;
  443. /* clean out all the encoder/crtc combos */
  444. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  445. encoder->crtc = NULL;
  446. }
  447. crtcs = kcalloc(dev->mode_config.num_connector,
  448. sizeof(struct drm_crtc *), GFP_KERNEL);
  449. modes = kcalloc(dev->mode_config.num_connector,
  450. sizeof(struct drm_display_mode *), GFP_KERNEL);
  451. enabled = kcalloc(dev->mode_config.num_connector,
  452. sizeof(bool), GFP_KERNEL);
  453. drm_enable_connectors(dev, enabled);
  454. ret = drm_target_preferred(dev, modes, enabled, width, height);
  455. if (!ret)
  456. DRM_ERROR("Unable to find initial modes\n");
  457. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
  458. drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
  459. i = 0;
  460. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  461. struct drm_display_mode *mode = modes[i];
  462. struct drm_crtc *crtc = crtcs[i];
  463. if (connector->encoder == NULL) {
  464. i++;
  465. continue;
  466. }
  467. if (mode && crtc) {
  468. DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  469. mode->name, crtc->base.id);
  470. crtc->desired_mode = mode;
  471. connector->encoder->crtc = crtc;
  472. } else {
  473. connector->encoder->crtc = NULL;
  474. connector->encoder = NULL;
  475. }
  476. i++;
  477. }
  478. kfree(crtcs);
  479. kfree(modes);
  480. kfree(enabled);
  481. }
  482. /**
  483. * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  484. * @encoder: encoder to test
  485. * @crtc: crtc to test
  486. *
  487. * Return false if @encoder can't be driven by @crtc, true otherwise.
  488. */
  489. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  490. struct drm_crtc *crtc)
  491. {
  492. struct drm_device *dev;
  493. struct drm_crtc *tmp;
  494. int crtc_mask = 1;
  495. WARN(!crtc, "checking null crtc?");
  496. dev = crtc->dev;
  497. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  498. if (tmp == crtc)
  499. break;
  500. crtc_mask <<= 1;
  501. }
  502. if (encoder->possible_crtcs & crtc_mask)
  503. return true;
  504. return false;
  505. }
  506. /*
  507. * Check the CRTC we're going to map each output to vs. its current
  508. * CRTC. If they don't match, we have to disable the output and the CRTC
  509. * since the driver will have to re-route things.
  510. */
  511. static void
  512. drm_crtc_prepare_encoders(struct drm_device *dev)
  513. {
  514. struct drm_encoder_helper_funcs *encoder_funcs;
  515. struct drm_encoder *encoder;
  516. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  517. encoder_funcs = encoder->helper_private;
  518. /* Disable unused encoders */
  519. if (encoder->crtc == NULL)
  520. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  521. /* Disable encoders whose CRTC is about to change */
  522. if (encoder_funcs->get_crtc &&
  523. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  524. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  525. }
  526. }
  527. /**
  528. * drm_crtc_set_mode - set a mode
  529. * @crtc: CRTC to program
  530. * @mode: mode to use
  531. * @x: width of mode
  532. * @y: height of mode
  533. *
  534. * LOCKING:
  535. * Caller must hold mode config lock.
  536. *
  537. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  538. * to fixup or reject the mode prior to trying to set it.
  539. *
  540. * RETURNS:
  541. * True if the mode was set successfully, or false otherwise.
  542. */
  543. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  544. struct drm_display_mode *mode,
  545. int x, int y,
  546. struct drm_framebuffer *old_fb)
  547. {
  548. struct drm_device *dev = crtc->dev;
  549. struct drm_display_mode *adjusted_mode, saved_mode;
  550. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  551. struct drm_encoder_helper_funcs *encoder_funcs;
  552. int saved_x, saved_y;
  553. struct drm_encoder *encoder;
  554. bool ret = true;
  555. adjusted_mode = drm_mode_duplicate(dev, mode);
  556. crtc->enabled = drm_helper_crtc_in_use(crtc);
  557. if (!crtc->enabled)
  558. return true;
  559. saved_mode = crtc->mode;
  560. saved_x = crtc->x;
  561. saved_y = crtc->y;
  562. /* Update crtc values up front so the driver can rely on them for mode
  563. * setting.
  564. */
  565. crtc->mode = *mode;
  566. crtc->x = x;
  567. crtc->y = y;
  568. /* Pass our mode to the connectors and the CRTC to give them a chance to
  569. * adjust it according to limitations or connector properties, and also
  570. * a chance to reject the mode entirely.
  571. */
  572. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  573. if (encoder->crtc != crtc)
  574. continue;
  575. encoder_funcs = encoder->helper_private;
  576. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  577. adjusted_mode))) {
  578. goto done;
  579. }
  580. }
  581. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  582. goto done;
  583. }
  584. /* Prepare the encoders and CRTCs before setting the mode. */
  585. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  586. if (encoder->crtc != crtc)
  587. continue;
  588. encoder_funcs = encoder->helper_private;
  589. /* Disable the encoders as the first thing we do. */
  590. encoder_funcs->prepare(encoder);
  591. }
  592. drm_crtc_prepare_encoders(dev);
  593. crtc_funcs->prepare(crtc);
  594. /* Set up the DPLL and any encoders state that needs to adjust or depend
  595. * on the DPLL.
  596. */
  597. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  598. if (!ret)
  599. goto done;
  600. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  601. if (encoder->crtc != crtc)
  602. continue;
  603. DRM_DEBUG("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
  604. mode->name, mode->base.id);
  605. encoder_funcs = encoder->helper_private;
  606. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  607. }
  608. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  609. crtc_funcs->commit(crtc);
  610. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  611. if (encoder->crtc != crtc)
  612. continue;
  613. encoder_funcs = encoder->helper_private;
  614. encoder_funcs->commit(encoder);
  615. }
  616. /* XXX free adjustedmode */
  617. drm_mode_destroy(dev, adjusted_mode);
  618. /* FIXME: add subpixel order */
  619. done:
  620. if (!ret) {
  621. crtc->mode = saved_mode;
  622. crtc->x = saved_x;
  623. crtc->y = saved_y;
  624. }
  625. return ret;
  626. }
  627. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  628. /**
  629. * drm_crtc_helper_set_config - set a new config from userspace
  630. * @crtc: CRTC to setup
  631. * @crtc_info: user provided configuration
  632. * @new_mode: new mode to set
  633. * @connector_set: set of connectors for the new config
  634. * @fb: new framebuffer
  635. *
  636. * LOCKING:
  637. * Caller must hold mode config lock.
  638. *
  639. * Setup a new configuration, provided by the user in @crtc_info, and enable
  640. * it.
  641. *
  642. * RETURNS:
  643. * Zero. (FIXME)
  644. */
  645. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  646. {
  647. struct drm_device *dev;
  648. struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  649. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  650. struct drm_framebuffer *old_fb = NULL;
  651. bool mode_changed = false; /* if true do a full mode set */
  652. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  653. struct drm_connector *save_connectors, *connector;
  654. int count = 0, ro, fail = 0;
  655. struct drm_crtc_helper_funcs *crtc_funcs;
  656. int ret = 0;
  657. DRM_DEBUG_KMS("\n");
  658. if (!set)
  659. return -EINVAL;
  660. if (!set->crtc)
  661. return -EINVAL;
  662. if (!set->crtc->helper_private)
  663. return -EINVAL;
  664. crtc_funcs = set->crtc->helper_private;
  665. DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
  666. " %d (x, y) (%i, %i)\n",
  667. set->crtc, set->crtc->base.id, set->fb, set->connectors,
  668. (int)set->num_connectors, set->x, set->y);
  669. dev = set->crtc->dev;
  670. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  671. * connector data. */
  672. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  673. sizeof(struct drm_crtc), GFP_KERNEL);
  674. if (!save_crtcs)
  675. return -ENOMEM;
  676. save_encoders = kzalloc(dev->mode_config.num_encoder *
  677. sizeof(struct drm_encoder), GFP_KERNEL);
  678. if (!save_encoders) {
  679. kfree(save_crtcs);
  680. return -ENOMEM;
  681. }
  682. save_connectors = kzalloc(dev->mode_config.num_connector *
  683. sizeof(struct drm_connector), GFP_KERNEL);
  684. if (!save_connectors) {
  685. kfree(save_crtcs);
  686. kfree(save_encoders);
  687. return -ENOMEM;
  688. }
  689. /* Copy data. Note that driver private data is not affected.
  690. * Should anything bad happen only the expected state is
  691. * restored, not the drivers personal bookkeeping.
  692. */
  693. count = 0;
  694. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  695. save_crtcs[count++] = *crtc;
  696. }
  697. count = 0;
  698. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  699. save_encoders[count++] = *encoder;
  700. }
  701. count = 0;
  702. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  703. save_connectors[count++] = *connector;
  704. }
  705. /* We should be able to check here if the fb has the same properties
  706. * and then just flip_or_move it */
  707. if (set->crtc->fb != set->fb) {
  708. /* If we have no fb then treat it as a full mode set */
  709. if (set->crtc->fb == NULL) {
  710. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  711. mode_changed = true;
  712. } else if (set->fb == NULL) {
  713. mode_changed = true;
  714. } else if ((set->fb->bits_per_pixel !=
  715. set->crtc->fb->bits_per_pixel) ||
  716. set->fb->depth != set->crtc->fb->depth)
  717. fb_changed = true;
  718. else
  719. fb_changed = true;
  720. }
  721. if (set->x != set->crtc->x || set->y != set->crtc->y)
  722. fb_changed = true;
  723. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  724. DRM_DEBUG_KMS("modes are different, full mode set\n");
  725. drm_mode_debug_printmodeline(&set->crtc->mode);
  726. drm_mode_debug_printmodeline(set->mode);
  727. mode_changed = true;
  728. }
  729. /* a) traverse passed in connector list and get encoders for them */
  730. count = 0;
  731. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  732. struct drm_connector_helper_funcs *connector_funcs =
  733. connector->helper_private;
  734. new_encoder = connector->encoder;
  735. for (ro = 0; ro < set->num_connectors; ro++) {
  736. if (set->connectors[ro] == connector) {
  737. new_encoder = connector_funcs->best_encoder(connector);
  738. /* if we can't get an encoder for a connector
  739. we are setting now - then fail */
  740. if (new_encoder == NULL)
  741. /* don't break so fail path works correct */
  742. fail = 1;
  743. break;
  744. }
  745. }
  746. if (new_encoder != connector->encoder) {
  747. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  748. mode_changed = true;
  749. /* If the encoder is reused for another connector, then
  750. * the appropriate crtc will be set later.
  751. */
  752. if (connector->encoder)
  753. connector->encoder->crtc = NULL;
  754. connector->encoder = new_encoder;
  755. }
  756. }
  757. if (fail) {
  758. ret = -EINVAL;
  759. goto fail;
  760. }
  761. count = 0;
  762. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  763. if (!connector->encoder)
  764. continue;
  765. if (connector->encoder->crtc == set->crtc)
  766. new_crtc = NULL;
  767. else
  768. new_crtc = connector->encoder->crtc;
  769. for (ro = 0; ro < set->num_connectors; ro++) {
  770. if (set->connectors[ro] == connector)
  771. new_crtc = set->crtc;
  772. }
  773. /* Make sure the new CRTC will work with the encoder */
  774. if (new_crtc &&
  775. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  776. ret = -EINVAL;
  777. goto fail;
  778. }
  779. if (new_crtc != connector->encoder->crtc) {
  780. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  781. mode_changed = true;
  782. connector->encoder->crtc = new_crtc;
  783. }
  784. DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
  785. connector->base.id, new_crtc);
  786. }
  787. /* mode_set_base is not a required function */
  788. if (fb_changed && !crtc_funcs->mode_set_base)
  789. mode_changed = true;
  790. if (mode_changed) {
  791. old_fb = set->crtc->fb;
  792. set->crtc->fb = set->fb;
  793. set->crtc->enabled = (set->mode != NULL);
  794. if (set->mode != NULL) {
  795. DRM_DEBUG_KMS("attempting to set mode from"
  796. " userspace\n");
  797. drm_mode_debug_printmodeline(set->mode);
  798. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  799. set->x, set->y,
  800. old_fb)) {
  801. DRM_ERROR("failed to set mode on crtc %p\n",
  802. set->crtc);
  803. ret = -EINVAL;
  804. goto fail;
  805. }
  806. /* TODO are these needed? */
  807. set->crtc->desired_x = set->x;
  808. set->crtc->desired_y = set->y;
  809. set->crtc->desired_mode = set->mode;
  810. }
  811. drm_helper_disable_unused_functions(dev);
  812. } else if (fb_changed) {
  813. set->crtc->x = set->x;
  814. set->crtc->y = set->y;
  815. old_fb = set->crtc->fb;
  816. if (set->crtc->fb != set->fb)
  817. set->crtc->fb = set->fb;
  818. ret = crtc_funcs->mode_set_base(set->crtc,
  819. set->x, set->y, old_fb);
  820. if (ret != 0)
  821. goto fail;
  822. }
  823. kfree(save_connectors);
  824. kfree(save_encoders);
  825. kfree(save_crtcs);
  826. return 0;
  827. fail:
  828. /* Restore all previous data. */
  829. count = 0;
  830. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  831. *crtc = save_crtcs[count++];
  832. }
  833. count = 0;
  834. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  835. *encoder = save_encoders[count++];
  836. }
  837. count = 0;
  838. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  839. *connector = save_connectors[count++];
  840. }
  841. kfree(save_connectors);
  842. kfree(save_encoders);
  843. kfree(save_crtcs);
  844. return ret;
  845. }
  846. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  847. bool drm_helper_plugged_event(struct drm_device *dev)
  848. {
  849. DRM_DEBUG_KMS("\n");
  850. drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
  851. dev->mode_config.max_height);
  852. drm_setup_crtcs(dev);
  853. /* alert the driver fb layer */
  854. dev->mode_config.funcs->fb_changed(dev);
  855. /* FIXME: send hotplug event */
  856. return true;
  857. }
  858. /**
  859. * drm_initial_config - setup a sane initial connector configuration
  860. * @dev: DRM device
  861. *
  862. * LOCKING:
  863. * Called at init time, must take mode config lock.
  864. *
  865. * Scan the CRTCs and connectors and try to put together an initial setup.
  866. * At the moment, this is a cloned configuration across all heads with
  867. * a new framebuffer object as the backing store.
  868. *
  869. * RETURNS:
  870. * Zero if everything went ok, nonzero otherwise.
  871. */
  872. bool drm_helper_initial_config(struct drm_device *dev)
  873. {
  874. int count = 0;
  875. /* disable all the possible outputs/crtcs before entering KMS mode */
  876. drm_helper_disable_unused_functions(dev);
  877. drm_fb_helper_parse_command_line(dev);
  878. count = drm_helper_probe_connector_modes(dev,
  879. dev->mode_config.max_width,
  880. dev->mode_config.max_height);
  881. /*
  882. * we shouldn't end up with no modes here.
  883. */
  884. if (count == 0)
  885. printk(KERN_INFO "No connectors reported connected with modes\n");
  886. drm_setup_crtcs(dev);
  887. /* alert the driver fb layer */
  888. dev->mode_config.funcs->fb_changed(dev);
  889. return 0;
  890. }
  891. EXPORT_SYMBOL(drm_helper_initial_config);
  892. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  893. {
  894. int dpms = DRM_MODE_DPMS_OFF;
  895. struct drm_connector *connector;
  896. struct drm_device *dev = encoder->dev;
  897. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  898. if (connector->encoder == encoder)
  899. if (connector->dpms < dpms)
  900. dpms = connector->dpms;
  901. return dpms;
  902. }
  903. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  904. {
  905. int dpms = DRM_MODE_DPMS_OFF;
  906. struct drm_connector *connector;
  907. struct drm_device *dev = crtc->dev;
  908. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  909. if (connector->encoder && connector->encoder->crtc == crtc)
  910. if (connector->dpms < dpms)
  911. dpms = connector->dpms;
  912. return dpms;
  913. }
  914. /**
  915. * drm_helper_connector_dpms
  916. * @connector affected connector
  917. * @mode DPMS mode
  918. *
  919. * Calls the low-level connector DPMS function, then
  920. * calls appropriate encoder and crtc DPMS functions as well
  921. */
  922. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  923. {
  924. struct drm_encoder *encoder = connector->encoder;
  925. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  926. int old_dpms;
  927. if (mode == connector->dpms)
  928. return;
  929. old_dpms = connector->dpms;
  930. connector->dpms = mode;
  931. /* from off to on, do crtc then encoder */
  932. if (mode < old_dpms) {
  933. if (crtc) {
  934. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  935. if (crtc_funcs->dpms)
  936. (*crtc_funcs->dpms) (crtc,
  937. drm_helper_choose_crtc_dpms(crtc));
  938. }
  939. if (encoder) {
  940. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  941. if (encoder_funcs->dpms)
  942. (*encoder_funcs->dpms) (encoder,
  943. drm_helper_choose_encoder_dpms(encoder));
  944. }
  945. }
  946. /* from on to off, do encoder then crtc */
  947. if (mode > old_dpms) {
  948. if (encoder) {
  949. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  950. if (encoder_funcs->dpms)
  951. (*encoder_funcs->dpms) (encoder,
  952. drm_helper_choose_encoder_dpms(encoder));
  953. }
  954. if (crtc) {
  955. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  956. if (crtc_funcs->dpms)
  957. (*crtc_funcs->dpms) (crtc,
  958. drm_helper_choose_crtc_dpms(crtc));
  959. }
  960. }
  961. return;
  962. }
  963. EXPORT_SYMBOL(drm_helper_connector_dpms);
  964. /**
  965. * drm_hotplug_stage_two
  966. * @dev DRM device
  967. * @connector hotpluged connector
  968. *
  969. * LOCKING.
  970. * Caller must hold mode config lock, function might grab struct lock.
  971. *
  972. * Stage two of a hotplug.
  973. *
  974. * RETURNS:
  975. * Zero on success, errno on failure.
  976. */
  977. int drm_helper_hotplug_stage_two(struct drm_device *dev)
  978. {
  979. drm_helper_plugged_event(dev);
  980. return 0;
  981. }
  982. EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
  983. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  984. struct drm_mode_fb_cmd *mode_cmd)
  985. {
  986. fb->width = mode_cmd->width;
  987. fb->height = mode_cmd->height;
  988. fb->pitch = mode_cmd->pitch;
  989. fb->bits_per_pixel = mode_cmd->bpp;
  990. fb->depth = mode_cmd->depth;
  991. return 0;
  992. }
  993. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  994. int drm_helper_resume_force_mode(struct drm_device *dev)
  995. {
  996. struct drm_crtc *crtc;
  997. struct drm_encoder *encoder;
  998. struct drm_encoder_helper_funcs *encoder_funcs;
  999. struct drm_crtc_helper_funcs *crtc_funcs;
  1000. int ret;
  1001. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1002. if (!crtc->enabled)
  1003. continue;
  1004. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  1005. crtc->x, crtc->y, crtc->fb);
  1006. if (ret == false)
  1007. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  1008. /* Turn off outputs that were already powered off */
  1009. if (drm_helper_choose_crtc_dpms(crtc)) {
  1010. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1011. if(encoder->crtc != crtc)
  1012. continue;
  1013. encoder_funcs = encoder->helper_private;
  1014. if (encoder_funcs->dpms)
  1015. (*encoder_funcs->dpms) (encoder,
  1016. drm_helper_choose_encoder_dpms(encoder));
  1017. crtc_funcs = crtc->helper_private;
  1018. if (crtc_funcs->dpms)
  1019. (*crtc_funcs->dpms) (crtc,
  1020. drm_helper_choose_crtc_dpms(crtc));
  1021. }
  1022. }
  1023. }
  1024. /* disable the unused connectors while restoring the modesetting */
  1025. drm_helper_disable_unused_functions(dev);
  1026. return 0;
  1027. }
  1028. EXPORT_SYMBOL(drm_helper_resume_force_mode);