drm_crtc_helper.c 32 KB

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