drm_crtc_helper.c 28 KB

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