drm_crtc_helper.c 31 KB

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