drm_crtc_helper.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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 <drm/drmP.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_fourcc.h>
  36. #include <drm/drm_crtc_helper.h>
  37. #include <drm/drm_fb_helper.h>
  38. #include <drm/drm_edid.h>
  39. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  40. MODULE_DESCRIPTION("DRM KMS helper");
  41. MODULE_LICENSE("GPL and additional rights");
  42. /**
  43. * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  44. * connector list
  45. * @dev: drm device to operate on
  46. *
  47. * Some userspace presumes that the first connected connector is the main
  48. * display, where it's supposed to display e.g. the login screen. For
  49. * laptops, this should be the main panel. Use this function to sort all
  50. * (eDP/LVDS) panels to the front of the connector list, instead of
  51. * painstakingly trying to initialize them in the right order.
  52. */
  53. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  54. {
  55. struct drm_connector *connector, *tmp;
  56. struct list_head panel_list;
  57. INIT_LIST_HEAD(&panel_list);
  58. list_for_each_entry_safe(connector, tmp,
  59. &dev->mode_config.connector_list, head) {
  60. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  61. connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  62. list_move_tail(&connector->head, &panel_list);
  63. }
  64. list_splice(&panel_list, &dev->mode_config.connector_list);
  65. }
  66. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  67. static bool drm_kms_helper_poll = true;
  68. module_param_named(poll, drm_kms_helper_poll, bool, 0600);
  69. static void drm_mode_validate_flag(struct drm_connector *connector,
  70. int flags)
  71. {
  72. struct drm_display_mode *mode;
  73. if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
  74. DRM_MODE_FLAG_3D_MASK))
  75. return;
  76. list_for_each_entry(mode, &connector->modes, head) {
  77. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  78. !(flags & DRM_MODE_FLAG_INTERLACE))
  79. mode->status = MODE_NO_INTERLACE;
  80. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  81. !(flags & DRM_MODE_FLAG_DBLSCAN))
  82. mode->status = MODE_NO_DBLESCAN;
  83. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
  84. !(flags & DRM_MODE_FLAG_3D_MASK))
  85. mode->status = MODE_NO_STEREO;
  86. }
  87. return;
  88. }
  89. /**
  90. * drm_helper_probe_single_connector_modes - get complete set of display modes
  91. * @connector: connector to probe
  92. * @maxX: max width for modes
  93. * @maxY: max height for modes
  94. *
  95. * LOCKING:
  96. * Caller must hold mode config lock.
  97. *
  98. * Based on the helper callbacks implemented by @connector try to detect all
  99. * valid modes. Modes will first be added to the connector's probed_modes list,
  100. * then culled (based on validity and the @maxX, @maxY parameters) and put into
  101. * the normal modes list.
  102. *
  103. * Intended to be use as a generic implementation of the ->fill_modes()
  104. * @connector vfunc for drivers that use the crtc helpers for output mode
  105. * filtering and detection.
  106. *
  107. * RETURNS:
  108. * Number of modes found on @connector.
  109. */
  110. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  111. uint32_t maxX, uint32_t maxY)
  112. {
  113. struct drm_device *dev = connector->dev;
  114. struct drm_display_mode *mode;
  115. struct drm_connector_helper_funcs *connector_funcs =
  116. connector->helper_private;
  117. int count = 0;
  118. int mode_flags = 0;
  119. bool verbose_prune = true;
  120. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  121. drm_get_connector_name(connector));
  122. /* set all modes to the unverified state */
  123. list_for_each_entry(mode, &connector->modes, head)
  124. mode->status = MODE_UNVERIFIED;
  125. if (connector->force) {
  126. if (connector->force == DRM_FORCE_ON)
  127. connector->status = connector_status_connected;
  128. else
  129. connector->status = connector_status_disconnected;
  130. if (connector->funcs->force)
  131. connector->funcs->force(connector);
  132. } else {
  133. connector->status = connector->funcs->detect(connector, true);
  134. }
  135. /* Re-enable polling in case the global poll config changed. */
  136. if (drm_kms_helper_poll != dev->mode_config.poll_running)
  137. drm_kms_helper_poll_enable(dev);
  138. dev->mode_config.poll_running = drm_kms_helper_poll;
  139. if (connector->status == connector_status_disconnected) {
  140. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  141. connector->base.id, drm_get_connector_name(connector));
  142. drm_mode_connector_update_edid_property(connector, NULL);
  143. verbose_prune = false;
  144. goto prune;
  145. }
  146. #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
  147. count = drm_load_edid_firmware(connector);
  148. if (count == 0)
  149. #endif
  150. count = (*connector_funcs->get_modes)(connector);
  151. if (count == 0 && connector->status == connector_status_connected)
  152. count = drm_add_modes_noedid(connector, 1024, 768);
  153. if (count == 0)
  154. goto prune;
  155. drm_mode_connector_list_update(connector);
  156. if (maxX && maxY)
  157. drm_mode_validate_size(dev, &connector->modes, maxX,
  158. maxY, 0);
  159. if (connector->interlace_allowed)
  160. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  161. if (connector->doublescan_allowed)
  162. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  163. if (connector->stereo_allowed)
  164. mode_flags |= DRM_MODE_FLAG_3D_MASK;
  165. drm_mode_validate_flag(connector, mode_flags);
  166. list_for_each_entry(mode, &connector->modes, head) {
  167. if (mode->status == MODE_OK)
  168. mode->status = connector_funcs->mode_valid(connector,
  169. mode);
  170. }
  171. prune:
  172. drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
  173. if (list_empty(&connector->modes))
  174. return 0;
  175. list_for_each_entry(mode, &connector->modes, head)
  176. mode->vrefresh = drm_mode_vrefresh(mode);
  177. drm_mode_sort(&connector->modes);
  178. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  179. drm_get_connector_name(connector));
  180. list_for_each_entry(mode, &connector->modes, head) {
  181. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  182. drm_mode_debug_printmodeline(mode);
  183. }
  184. return count;
  185. }
  186. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  187. /**
  188. * drm_helper_encoder_in_use - check if a given encoder is in use
  189. * @encoder: encoder to check
  190. *
  191. * LOCKING:
  192. * Caller must hold mode config lock.
  193. *
  194. * Walk @encoders's DRM device's mode_config and see if it's in use.
  195. *
  196. * RETURNS:
  197. * True if @encoder is part of the mode_config, false otherwise.
  198. */
  199. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  200. {
  201. struct drm_connector *connector;
  202. struct drm_device *dev = encoder->dev;
  203. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  204. if (connector->encoder == encoder)
  205. return true;
  206. return false;
  207. }
  208. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  209. /**
  210. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  211. * @crtc: CRTC to check
  212. *
  213. * LOCKING:
  214. * Caller must hold mode config lock.
  215. *
  216. * Walk @crtc's DRM device's mode_config and see if it's in use.
  217. *
  218. * RETURNS:
  219. * True if @crtc is part of the mode_config, false otherwise.
  220. */
  221. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  222. {
  223. struct drm_encoder *encoder;
  224. struct drm_device *dev = crtc->dev;
  225. /* FIXME: Locking around list access? */
  226. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  227. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  228. return true;
  229. return false;
  230. }
  231. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  232. static void
  233. drm_encoder_disable(struct drm_encoder *encoder)
  234. {
  235. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  236. if (encoder->bridge)
  237. encoder->bridge->funcs->disable(encoder->bridge);
  238. if (encoder_funcs->disable)
  239. (*encoder_funcs->disable)(encoder);
  240. else
  241. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  242. if (encoder->bridge)
  243. encoder->bridge->funcs->post_disable(encoder->bridge);
  244. }
  245. /**
  246. * drm_helper_disable_unused_functions - disable unused objects
  247. * @dev: DRM device
  248. *
  249. * LOCKING:
  250. * Caller must hold mode config lock.
  251. *
  252. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  253. * by calling its dpms function, which should power it off.
  254. */
  255. void drm_helper_disable_unused_functions(struct drm_device *dev)
  256. {
  257. struct drm_encoder *encoder;
  258. struct drm_connector *connector;
  259. struct drm_crtc *crtc;
  260. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  261. if (!connector->encoder)
  262. continue;
  263. if (connector->status == connector_status_disconnected)
  264. connector->encoder = NULL;
  265. }
  266. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  267. if (!drm_helper_encoder_in_use(encoder)) {
  268. drm_encoder_disable(encoder);
  269. /* disconnector encoder from any connector */
  270. encoder->crtc = NULL;
  271. }
  272. }
  273. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  274. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  275. crtc->enabled = drm_helper_crtc_in_use(crtc);
  276. if (!crtc->enabled) {
  277. if (crtc_funcs->disable)
  278. (*crtc_funcs->disable)(crtc);
  279. else
  280. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  281. crtc->fb = NULL;
  282. }
  283. }
  284. }
  285. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  286. /**
  287. * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  288. * @encoder: encoder to test
  289. * @crtc: crtc to test
  290. *
  291. * Return false if @encoder can't be driven by @crtc, true otherwise.
  292. */
  293. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  294. struct drm_crtc *crtc)
  295. {
  296. struct drm_device *dev;
  297. struct drm_crtc *tmp;
  298. int crtc_mask = 1;
  299. WARN(!crtc, "checking null crtc?\n");
  300. dev = crtc->dev;
  301. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  302. if (tmp == crtc)
  303. break;
  304. crtc_mask <<= 1;
  305. }
  306. if (encoder->possible_crtcs & crtc_mask)
  307. return true;
  308. return false;
  309. }
  310. /*
  311. * Check the CRTC we're going to map each output to vs. its current
  312. * CRTC. If they don't match, we have to disable the output and the CRTC
  313. * since the driver will have to re-route things.
  314. */
  315. static void
  316. drm_crtc_prepare_encoders(struct drm_device *dev)
  317. {
  318. struct drm_encoder_helper_funcs *encoder_funcs;
  319. struct drm_encoder *encoder;
  320. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  321. encoder_funcs = encoder->helper_private;
  322. /* Disable unused encoders */
  323. if (encoder->crtc == NULL)
  324. drm_encoder_disable(encoder);
  325. /* Disable encoders whose CRTC is about to change */
  326. if (encoder_funcs->get_crtc &&
  327. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  328. drm_encoder_disable(encoder);
  329. }
  330. }
  331. /**
  332. * drm_crtc_helper_set_mode - internal helper to set a mode
  333. * @crtc: CRTC to program
  334. * @mode: mode to use
  335. * @x: horizontal offset into the surface
  336. * @y: vertical offset into the surface
  337. * @old_fb: old framebuffer, for cleanup
  338. *
  339. * LOCKING:
  340. * Caller must hold mode config lock.
  341. *
  342. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  343. * to fixup or reject the mode prior to trying to set it. This is an internal
  344. * helper that drivers could e.g. use to update properties that require the
  345. * entire output pipe to be disabled and re-enabled in a new configuration. For
  346. * example for changing whether audio is enabled on a hdmi link or for changing
  347. * panel fitter or dither attributes. It is also called by the
  348. * drm_crtc_helper_set_config() helper function to drive the mode setting
  349. * sequence.
  350. *
  351. * RETURNS:
  352. * True if the mode was set successfully, or false otherwise.
  353. */
  354. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  355. struct drm_display_mode *mode,
  356. int x, int y,
  357. struct drm_framebuffer *old_fb)
  358. {
  359. struct drm_device *dev = crtc->dev;
  360. struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
  361. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  362. struct drm_encoder_helper_funcs *encoder_funcs;
  363. int saved_x, saved_y;
  364. struct drm_encoder *encoder;
  365. bool ret = true;
  366. crtc->enabled = drm_helper_crtc_in_use(crtc);
  367. if (!crtc->enabled)
  368. return true;
  369. adjusted_mode = drm_mode_duplicate(dev, mode);
  370. if (!adjusted_mode)
  371. return false;
  372. saved_hwmode = crtc->hwmode;
  373. saved_mode = crtc->mode;
  374. saved_x = crtc->x;
  375. saved_y = crtc->y;
  376. /* Update crtc values up front so the driver can rely on them for mode
  377. * setting.
  378. */
  379. crtc->mode = *mode;
  380. crtc->x = x;
  381. crtc->y = y;
  382. /* Pass our mode to the connectors and the CRTC to give them a chance to
  383. * adjust it according to limitations or connector properties, and also
  384. * a chance to reject the mode entirely.
  385. */
  386. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  387. if (encoder->crtc != crtc)
  388. continue;
  389. if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
  390. ret = encoder->bridge->funcs->mode_fixup(
  391. encoder->bridge, mode, adjusted_mode);
  392. if (!ret) {
  393. DRM_DEBUG_KMS("Bridge fixup failed\n");
  394. goto done;
  395. }
  396. }
  397. encoder_funcs = encoder->helper_private;
  398. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  399. adjusted_mode))) {
  400. DRM_DEBUG_KMS("Encoder fixup failed\n");
  401. goto done;
  402. }
  403. }
  404. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  405. DRM_DEBUG_KMS("CRTC fixup failed\n");
  406. goto done;
  407. }
  408. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  409. /* Prepare the encoders and CRTCs before setting the mode. */
  410. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  411. if (encoder->crtc != crtc)
  412. continue;
  413. if (encoder->bridge)
  414. encoder->bridge->funcs->disable(encoder->bridge);
  415. encoder_funcs = encoder->helper_private;
  416. /* Disable the encoders as the first thing we do. */
  417. encoder_funcs->prepare(encoder);
  418. if (encoder->bridge)
  419. encoder->bridge->funcs->post_disable(encoder->bridge);
  420. }
  421. drm_crtc_prepare_encoders(dev);
  422. crtc_funcs->prepare(crtc);
  423. /* Set up the DPLL and any encoders state that needs to adjust or depend
  424. * on the DPLL.
  425. */
  426. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  427. if (!ret)
  428. goto done;
  429. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  430. if (encoder->crtc != crtc)
  431. continue;
  432. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  433. encoder->base.id, drm_get_encoder_name(encoder),
  434. mode->base.id, mode->name);
  435. encoder_funcs = encoder->helper_private;
  436. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  437. if (encoder->bridge && encoder->bridge->funcs->mode_set)
  438. encoder->bridge->funcs->mode_set(encoder->bridge, mode,
  439. adjusted_mode);
  440. }
  441. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  442. crtc_funcs->commit(crtc);
  443. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  444. if (encoder->crtc != crtc)
  445. continue;
  446. if (encoder->bridge)
  447. encoder->bridge->funcs->pre_enable(encoder->bridge);
  448. encoder_funcs = encoder->helper_private;
  449. encoder_funcs->commit(encoder);
  450. if (encoder->bridge)
  451. encoder->bridge->funcs->enable(encoder->bridge);
  452. }
  453. /* Store real post-adjustment hardware mode. */
  454. crtc->hwmode = *adjusted_mode;
  455. /* Calculate and store various constants which
  456. * are later needed by vblank and swap-completion
  457. * timestamping. They are derived from true hwmode.
  458. */
  459. drm_calc_timestamping_constants(crtc);
  460. /* FIXME: add subpixel order */
  461. done:
  462. drm_mode_destroy(dev, adjusted_mode);
  463. if (!ret) {
  464. crtc->hwmode = saved_hwmode;
  465. crtc->mode = saved_mode;
  466. crtc->x = saved_x;
  467. crtc->y = saved_y;
  468. }
  469. return ret;
  470. }
  471. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  472. static int
  473. drm_crtc_helper_disable(struct drm_crtc *crtc)
  474. {
  475. struct drm_device *dev = crtc->dev;
  476. struct drm_connector *connector;
  477. struct drm_encoder *encoder;
  478. /* Decouple all encoders and their attached connectors from this crtc */
  479. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  480. if (encoder->crtc != crtc)
  481. continue;
  482. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  483. if (connector->encoder != encoder)
  484. continue;
  485. connector->encoder = NULL;
  486. /*
  487. * drm_helper_disable_unused_functions() ought to be
  488. * doing this, but since we've decoupled the encoder
  489. * from the connector above, the required connection
  490. * between them is henceforth no longer available.
  491. */
  492. connector->dpms = DRM_MODE_DPMS_OFF;
  493. }
  494. }
  495. drm_helper_disable_unused_functions(dev);
  496. return 0;
  497. }
  498. /**
  499. * drm_crtc_helper_set_config - set a new config from userspace
  500. * @set: mode set configuration
  501. *
  502. * LOCKING:
  503. * Caller must hold mode config lock.
  504. *
  505. * Setup a new configuration, provided by the upper layers (either an ioctl call
  506. * from userspace or internally e.g. from the fbdev suppport code) in @set, and
  507. * enable it. This is the main helper functions for drivers that implement
  508. * kernel mode setting with the crtc helper functions and the assorted
  509. * ->prepare(), ->modeset() and ->commit() helper callbacks.
  510. *
  511. * RETURNS:
  512. * Returns 0 on success, -ERRNO on failure.
  513. */
  514. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  515. {
  516. struct drm_device *dev;
  517. struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  518. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  519. struct drm_framebuffer *old_fb = NULL;
  520. bool mode_changed = false; /* if true do a full mode set */
  521. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  522. struct drm_connector *save_connectors, *connector;
  523. int count = 0, ro, fail = 0;
  524. struct drm_crtc_helper_funcs *crtc_funcs;
  525. struct drm_mode_set save_set;
  526. int ret;
  527. int i;
  528. DRM_DEBUG_KMS("\n");
  529. BUG_ON(!set);
  530. BUG_ON(!set->crtc);
  531. BUG_ON(!set->crtc->helper_private);
  532. /* Enforce sane interface api - has been abused by the fb helper. */
  533. BUG_ON(!set->mode && set->fb);
  534. BUG_ON(set->fb && set->num_connectors == 0);
  535. crtc_funcs = set->crtc->helper_private;
  536. if (!set->mode)
  537. set->fb = NULL;
  538. if (set->fb) {
  539. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  540. set->crtc->base.id, set->fb->base.id,
  541. (int)set->num_connectors, set->x, set->y);
  542. } else {
  543. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  544. return drm_crtc_helper_disable(set->crtc);
  545. }
  546. dev = set->crtc->dev;
  547. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  548. * connector data. */
  549. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  550. sizeof(struct drm_crtc), GFP_KERNEL);
  551. if (!save_crtcs)
  552. return -ENOMEM;
  553. save_encoders = kzalloc(dev->mode_config.num_encoder *
  554. sizeof(struct drm_encoder), GFP_KERNEL);
  555. if (!save_encoders) {
  556. kfree(save_crtcs);
  557. return -ENOMEM;
  558. }
  559. save_connectors = kzalloc(dev->mode_config.num_connector *
  560. sizeof(struct drm_connector), GFP_KERNEL);
  561. if (!save_connectors) {
  562. kfree(save_crtcs);
  563. kfree(save_encoders);
  564. return -ENOMEM;
  565. }
  566. /* Copy data. Note that driver private data is not affected.
  567. * Should anything bad happen only the expected state is
  568. * restored, not the drivers personal bookkeeping.
  569. */
  570. count = 0;
  571. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  572. save_crtcs[count++] = *crtc;
  573. }
  574. count = 0;
  575. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  576. save_encoders[count++] = *encoder;
  577. }
  578. count = 0;
  579. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  580. save_connectors[count++] = *connector;
  581. }
  582. save_set.crtc = set->crtc;
  583. save_set.mode = &set->crtc->mode;
  584. save_set.x = set->crtc->x;
  585. save_set.y = set->crtc->y;
  586. save_set.fb = set->crtc->fb;
  587. /* We should be able to check here if the fb has the same properties
  588. * and then just flip_or_move it */
  589. if (set->crtc->fb != set->fb) {
  590. /* If we have no fb then treat it as a full mode set */
  591. if (set->crtc->fb == NULL) {
  592. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  593. mode_changed = true;
  594. } else if (set->fb == NULL) {
  595. mode_changed = true;
  596. } else if (set->fb->pixel_format !=
  597. set->crtc->fb->pixel_format) {
  598. mode_changed = true;
  599. } else
  600. fb_changed = true;
  601. }
  602. if (set->x != set->crtc->x || set->y != set->crtc->y)
  603. fb_changed = true;
  604. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  605. DRM_DEBUG_KMS("modes are different, full mode set\n");
  606. drm_mode_debug_printmodeline(&set->crtc->mode);
  607. drm_mode_debug_printmodeline(set->mode);
  608. mode_changed = true;
  609. }
  610. /* a) traverse passed in connector list and get encoders for them */
  611. count = 0;
  612. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  613. struct drm_connector_helper_funcs *connector_funcs =
  614. connector->helper_private;
  615. new_encoder = connector->encoder;
  616. for (ro = 0; ro < set->num_connectors; ro++) {
  617. if (set->connectors[ro] == connector) {
  618. new_encoder = connector_funcs->best_encoder(connector);
  619. /* if we can't get an encoder for a connector
  620. we are setting now - then fail */
  621. if (new_encoder == NULL)
  622. /* don't break so fail path works correct */
  623. fail = 1;
  624. break;
  625. if (connector->dpms != DRM_MODE_DPMS_ON) {
  626. DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
  627. mode_changed = true;
  628. }
  629. }
  630. }
  631. if (new_encoder != connector->encoder) {
  632. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  633. mode_changed = true;
  634. /* If the encoder is reused for another connector, then
  635. * the appropriate crtc will be set later.
  636. */
  637. if (connector->encoder)
  638. connector->encoder->crtc = NULL;
  639. connector->encoder = new_encoder;
  640. }
  641. }
  642. if (fail) {
  643. ret = -EINVAL;
  644. goto fail;
  645. }
  646. count = 0;
  647. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  648. if (!connector->encoder)
  649. continue;
  650. if (connector->encoder->crtc == set->crtc)
  651. new_crtc = NULL;
  652. else
  653. new_crtc = connector->encoder->crtc;
  654. for (ro = 0; ro < set->num_connectors; ro++) {
  655. if (set->connectors[ro] == connector)
  656. new_crtc = set->crtc;
  657. }
  658. /* Make sure the new CRTC will work with the encoder */
  659. if (new_crtc &&
  660. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  661. ret = -EINVAL;
  662. goto fail;
  663. }
  664. if (new_crtc != connector->encoder->crtc) {
  665. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  666. mode_changed = true;
  667. connector->encoder->crtc = new_crtc;
  668. }
  669. if (new_crtc) {
  670. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  671. connector->base.id, drm_get_connector_name(connector),
  672. new_crtc->base.id);
  673. } else {
  674. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  675. connector->base.id, drm_get_connector_name(connector));
  676. }
  677. }
  678. /* mode_set_base is not a required function */
  679. if (fb_changed && !crtc_funcs->mode_set_base)
  680. mode_changed = true;
  681. if (mode_changed) {
  682. set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
  683. if (set->crtc->enabled) {
  684. DRM_DEBUG_KMS("attempting to set mode from"
  685. " userspace\n");
  686. drm_mode_debug_printmodeline(set->mode);
  687. old_fb = set->crtc->fb;
  688. set->crtc->fb = set->fb;
  689. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  690. set->x, set->y,
  691. old_fb)) {
  692. DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  693. set->crtc->base.id);
  694. set->crtc->fb = old_fb;
  695. ret = -EINVAL;
  696. goto fail;
  697. }
  698. DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  699. for (i = 0; i < set->num_connectors; i++) {
  700. DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  701. drm_get_connector_name(set->connectors[i]));
  702. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  703. }
  704. }
  705. drm_helper_disable_unused_functions(dev);
  706. } else if (fb_changed) {
  707. set->crtc->x = set->x;
  708. set->crtc->y = set->y;
  709. old_fb = set->crtc->fb;
  710. if (set->crtc->fb != set->fb)
  711. set->crtc->fb = set->fb;
  712. ret = crtc_funcs->mode_set_base(set->crtc,
  713. set->x, set->y, old_fb);
  714. if (ret != 0) {
  715. set->crtc->fb = old_fb;
  716. goto fail;
  717. }
  718. }
  719. kfree(save_connectors);
  720. kfree(save_encoders);
  721. kfree(save_crtcs);
  722. return 0;
  723. fail:
  724. /* Restore all previous data. */
  725. count = 0;
  726. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  727. *crtc = save_crtcs[count++];
  728. }
  729. count = 0;
  730. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  731. *encoder = save_encoders[count++];
  732. }
  733. count = 0;
  734. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  735. *connector = save_connectors[count++];
  736. }
  737. /* Try to restore the config */
  738. if (mode_changed &&
  739. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  740. save_set.y, save_set.fb))
  741. DRM_ERROR("failed to restore config after modeset failure\n");
  742. kfree(save_connectors);
  743. kfree(save_encoders);
  744. kfree(save_crtcs);
  745. return ret;
  746. }
  747. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  748. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  749. {
  750. int dpms = DRM_MODE_DPMS_OFF;
  751. struct drm_connector *connector;
  752. struct drm_device *dev = encoder->dev;
  753. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  754. if (connector->encoder == encoder)
  755. if (connector->dpms < dpms)
  756. dpms = connector->dpms;
  757. return dpms;
  758. }
  759. /* Helper which handles bridge ordering around encoder dpms */
  760. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  761. {
  762. struct drm_bridge *bridge = encoder->bridge;
  763. struct drm_encoder_helper_funcs *encoder_funcs;
  764. if (bridge) {
  765. if (mode == DRM_MODE_DPMS_ON)
  766. bridge->funcs->pre_enable(bridge);
  767. else
  768. bridge->funcs->disable(bridge);
  769. }
  770. encoder_funcs = encoder->helper_private;
  771. if (encoder_funcs->dpms)
  772. encoder_funcs->dpms(encoder, mode);
  773. if (bridge) {
  774. if (mode == DRM_MODE_DPMS_ON)
  775. bridge->funcs->enable(bridge);
  776. else
  777. bridge->funcs->post_disable(bridge);
  778. }
  779. }
  780. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  781. {
  782. int dpms = DRM_MODE_DPMS_OFF;
  783. struct drm_connector *connector;
  784. struct drm_device *dev = crtc->dev;
  785. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  786. if (connector->encoder && connector->encoder->crtc == crtc)
  787. if (connector->dpms < dpms)
  788. dpms = connector->dpms;
  789. return dpms;
  790. }
  791. /**
  792. * drm_helper_connector_dpms() - connector dpms helper implementation
  793. * @connector: affected connector
  794. * @mode: DPMS mode
  795. *
  796. * This is the main helper function provided by the crtc helper framework for
  797. * implementing the DPMS connector attribute. It computes the new desired DPMS
  798. * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  799. * callback provided by the driver appropriately.
  800. */
  801. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  802. {
  803. struct drm_encoder *encoder = connector->encoder;
  804. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  805. int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  806. if (mode == connector->dpms)
  807. return;
  808. old_dpms = connector->dpms;
  809. connector->dpms = mode;
  810. if (encoder)
  811. encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  812. /* from off to on, do crtc then encoder */
  813. if (mode < old_dpms) {
  814. if (crtc) {
  815. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  816. if (crtc_funcs->dpms)
  817. (*crtc_funcs->dpms) (crtc,
  818. drm_helper_choose_crtc_dpms(crtc));
  819. }
  820. if (encoder)
  821. drm_helper_encoder_dpms(encoder, encoder_dpms);
  822. }
  823. /* from on to off, do encoder then crtc */
  824. if (mode > old_dpms) {
  825. if (encoder)
  826. drm_helper_encoder_dpms(encoder, encoder_dpms);
  827. if (crtc) {
  828. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  829. if (crtc_funcs->dpms)
  830. (*crtc_funcs->dpms) (crtc,
  831. drm_helper_choose_crtc_dpms(crtc));
  832. }
  833. }
  834. return;
  835. }
  836. EXPORT_SYMBOL(drm_helper_connector_dpms);
  837. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  838. struct drm_mode_fb_cmd2 *mode_cmd)
  839. {
  840. int i;
  841. fb->width = mode_cmd->width;
  842. fb->height = mode_cmd->height;
  843. for (i = 0; i < 4; i++) {
  844. fb->pitches[i] = mode_cmd->pitches[i];
  845. fb->offsets[i] = mode_cmd->offsets[i];
  846. }
  847. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  848. &fb->bits_per_pixel);
  849. fb->pixel_format = mode_cmd->pixel_format;
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  853. int drm_helper_resume_force_mode(struct drm_device *dev)
  854. {
  855. struct drm_crtc *crtc;
  856. struct drm_encoder *encoder;
  857. struct drm_crtc_helper_funcs *crtc_funcs;
  858. int ret, encoder_dpms;
  859. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  860. if (!crtc->enabled)
  861. continue;
  862. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  863. crtc->x, crtc->y, crtc->fb);
  864. if (ret == false)
  865. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  866. /* Turn off outputs that were already powered off */
  867. if (drm_helper_choose_crtc_dpms(crtc)) {
  868. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  869. if(encoder->crtc != crtc)
  870. continue;
  871. encoder_dpms = drm_helper_choose_encoder_dpms(
  872. encoder);
  873. drm_helper_encoder_dpms(encoder, encoder_dpms);
  874. }
  875. crtc_funcs = crtc->helper_private;
  876. if (crtc_funcs->dpms)
  877. (*crtc_funcs->dpms) (crtc,
  878. drm_helper_choose_crtc_dpms(crtc));
  879. }
  880. }
  881. /* disable the unused connectors while restoring the modesetting */
  882. drm_helper_disable_unused_functions(dev);
  883. return 0;
  884. }
  885. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  886. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  887. {
  888. /* send a uevent + call fbdev */
  889. drm_sysfs_hotplug_event(dev);
  890. if (dev->mode_config.funcs->output_poll_changed)
  891. dev->mode_config.funcs->output_poll_changed(dev);
  892. }
  893. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  894. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  895. static void output_poll_execute(struct work_struct *work)
  896. {
  897. struct delayed_work *delayed_work = to_delayed_work(work);
  898. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  899. struct drm_connector *connector;
  900. enum drm_connector_status old_status;
  901. bool repoll = false, changed = false;
  902. if (!drm_kms_helper_poll)
  903. return;
  904. mutex_lock(&dev->mode_config.mutex);
  905. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  906. /* Ignore forced connectors. */
  907. if (connector->force)
  908. continue;
  909. /* Ignore HPD capable connectors and connectors where we don't
  910. * want any hotplug detection at all for polling. */
  911. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  912. continue;
  913. repoll = true;
  914. old_status = connector->status;
  915. /* if we are connected and don't want to poll for disconnect
  916. skip it */
  917. if (old_status == connector_status_connected &&
  918. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  919. continue;
  920. connector->status = connector->funcs->detect(connector, false);
  921. if (old_status != connector->status) {
  922. const char *old, *new;
  923. old = drm_get_connector_status_name(old_status);
  924. new = drm_get_connector_status_name(connector->status);
  925. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  926. "status updated from %s to %s\n",
  927. connector->base.id,
  928. drm_get_connector_name(connector),
  929. old, new);
  930. changed = true;
  931. }
  932. }
  933. mutex_unlock(&dev->mode_config.mutex);
  934. if (changed)
  935. drm_kms_helper_hotplug_event(dev);
  936. if (repoll)
  937. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  938. }
  939. void drm_kms_helper_poll_disable(struct drm_device *dev)
  940. {
  941. if (!dev->mode_config.poll_enabled)
  942. return;
  943. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  944. }
  945. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  946. void drm_kms_helper_poll_enable(struct drm_device *dev)
  947. {
  948. bool poll = false;
  949. struct drm_connector *connector;
  950. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  951. return;
  952. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  953. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  954. DRM_CONNECTOR_POLL_DISCONNECT))
  955. poll = true;
  956. }
  957. if (poll)
  958. schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  959. }
  960. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  961. void drm_kms_helper_poll_init(struct drm_device *dev)
  962. {
  963. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  964. dev->mode_config.poll_enabled = true;
  965. drm_kms_helper_poll_enable(dev);
  966. }
  967. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  968. void drm_kms_helper_poll_fini(struct drm_device *dev)
  969. {
  970. drm_kms_helper_poll_disable(dev);
  971. }
  972. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  973. bool drm_helper_hpd_irq_event(struct drm_device *dev)
  974. {
  975. struct drm_connector *connector;
  976. enum drm_connector_status old_status;
  977. bool changed = false;
  978. if (!dev->mode_config.poll_enabled)
  979. return false;
  980. mutex_lock(&dev->mode_config.mutex);
  981. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  982. /* Only handle HPD capable connectors. */
  983. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  984. continue;
  985. old_status = connector->status;
  986. connector->status = connector->funcs->detect(connector, false);
  987. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  988. connector->base.id,
  989. drm_get_connector_name(connector),
  990. drm_get_connector_status_name(old_status),
  991. drm_get_connector_status_name(connector->status));
  992. if (old_status != connector->status)
  993. changed = true;
  994. }
  995. mutex_unlock(&dev->mode_config.mutex);
  996. if (changed)
  997. drm_kms_helper_hotplug_event(dev);
  998. return changed;
  999. }
  1000. EXPORT_SYMBOL(drm_helper_hpd_irq_event);