drm_crtc_helper.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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/kernel.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/dynamic_debug.h>
  35. #include <drm/drm_atomic.h>
  36. #include <drm/drm_atomic_helper.h>
  37. #include <drm/drm_atomic_uapi.h>
  38. #include <drm/drm_bridge.h>
  39. #include <drm/drm_crtc.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_drv.h>
  42. #include <drm/drm_edid.h>
  43. #include <drm/drm_encoder.h>
  44. #include <drm/drm_fourcc.h>
  45. #include <drm/drm_framebuffer.h>
  46. #include <drm/drm_print.h>
  47. #include <drm/drm_vblank.h>
  48. #include "drm_crtc_helper_internal.h"
  49. DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
  50. "DRM_UT_CORE",
  51. "DRM_UT_DRIVER",
  52. "DRM_UT_KMS",
  53. "DRM_UT_PRIME",
  54. "DRM_UT_ATOMIC",
  55. "DRM_UT_VBL",
  56. "DRM_UT_STATE",
  57. "DRM_UT_LEASE",
  58. "DRM_UT_DP",
  59. "DRM_UT_DRMRES");
  60. /**
  61. * DOC: overview
  62. *
  63. * The CRTC modeset helper library provides a default set_config implementation
  64. * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
  65. * the same callbacks which drivers can use to e.g. restore the modeset
  66. * configuration on resume with drm_helper_resume_force_mode().
  67. *
  68. * Note that this helper library doesn't track the current power state of CRTCs
  69. * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
  70. * though the hardware is already in the desired state. This deficiency has been
  71. * fixed in the atomic helpers.
  72. *
  73. * The driver callbacks are mostly compatible with the atomic modeset helpers,
  74. * except for the handling of the primary plane: Atomic helpers require that the
  75. * primary plane is implemented as a real standalone plane and not directly tied
  76. * to the CRTC state. For easier transition this library provides functions to
  77. * implement the old semantics required by the CRTC helpers using the new plane
  78. * and atomic helper callbacks.
  79. *
  80. * Drivers are strongly urged to convert to the atomic helpers (by way of first
  81. * converting to the plane helpers). New drivers must not use these functions
  82. * but need to implement the atomic interface instead, potentially using the
  83. * atomic helpers for that.
  84. *
  85. * These legacy modeset helpers use the same function table structures as
  86. * all other modesetting helpers. See the documentation for struct
  87. * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
  88. * &drm_connector_helper_funcs.
  89. */
  90. /**
  91. * drm_helper_encoder_in_use - check if a given encoder is in use
  92. * @encoder: encoder to check
  93. *
  94. * Checks whether @encoder is with the current mode setting output configuration
  95. * in use by any connector. This doesn't mean that it is actually enabled since
  96. * the DPMS state is tracked separately.
  97. *
  98. * Returns:
  99. * True if @encoder is used, false otherwise.
  100. */
  101. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  102. {
  103. struct drm_connector *connector;
  104. struct drm_connector_list_iter conn_iter;
  105. struct drm_device *dev = encoder->dev;
  106. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  107. /*
  108. * We can expect this mutex to be locked if we are not panicking.
  109. * Locking is currently fubar in the panic handler.
  110. */
  111. if (!oops_in_progress) {
  112. drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
  113. drm_WARN_ON(dev, !drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  114. }
  115. drm_connector_list_iter_begin(dev, &conn_iter);
  116. drm_for_each_connector_iter(connector, &conn_iter) {
  117. if (connector->encoder == encoder) {
  118. drm_connector_list_iter_end(&conn_iter);
  119. return true;
  120. }
  121. }
  122. drm_connector_list_iter_end(&conn_iter);
  123. return false;
  124. }
  125. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  126. /**
  127. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  128. * @crtc: CRTC to check
  129. *
  130. * Checks whether @crtc is with the current mode setting output configuration
  131. * in use by any connector. This doesn't mean that it is actually enabled since
  132. * the DPMS state is tracked separately.
  133. *
  134. * Returns:
  135. * True if @crtc is used, false otherwise.
  136. */
  137. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  138. {
  139. struct drm_encoder *encoder;
  140. struct drm_device *dev = crtc->dev;
  141. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  142. /*
  143. * We can expect this mutex to be locked if we are not panicking.
  144. * Locking is currently fubar in the panic handler.
  145. */
  146. if (!oops_in_progress)
  147. drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
  148. drm_for_each_encoder(encoder, dev)
  149. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  150. return true;
  151. return false;
  152. }
  153. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  154. static void
  155. drm_encoder_disable(struct drm_encoder *encoder)
  156. {
  157. const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  158. if (!encoder_funcs)
  159. return;
  160. if (encoder_funcs->disable)
  161. (*encoder_funcs->disable)(encoder);
  162. else if (encoder_funcs->dpms)
  163. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  164. }
  165. static void __drm_helper_disable_unused_functions(struct drm_device *dev)
  166. {
  167. struct drm_encoder *encoder;
  168. struct drm_crtc *crtc;
  169. drm_warn_on_modeset_not_all_locked(dev);
  170. drm_for_each_encoder(encoder, dev) {
  171. if (!drm_helper_encoder_in_use(encoder)) {
  172. drm_encoder_disable(encoder);
  173. /* disconnect encoder from any connector */
  174. encoder->crtc = NULL;
  175. }
  176. }
  177. drm_for_each_crtc(crtc, dev) {
  178. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  179. crtc->enabled = drm_helper_crtc_in_use(crtc);
  180. if (!crtc->enabled) {
  181. if (crtc_funcs->disable)
  182. (*crtc_funcs->disable)(crtc);
  183. else
  184. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  185. crtc->primary->fb = NULL;
  186. }
  187. }
  188. }
  189. /**
  190. * drm_helper_disable_unused_functions - disable unused objects
  191. * @dev: DRM device
  192. *
  193. * This function walks through the entire mode setting configuration of @dev. It
  194. * will remove any CRTC links of unused encoders and encoder links of
  195. * disconnected connectors. Then it will disable all unused encoders and CRTCs
  196. * either by calling their disable callback if available or by calling their
  197. * dpms callback with DRM_MODE_DPMS_OFF.
  198. *
  199. * NOTE:
  200. *
  201. * This function is part of the legacy modeset helper library and will cause
  202. * major confusion with atomic drivers. This is because atomic helpers guarantee
  203. * to never call ->disable() hooks on a disabled function, or ->enable() hooks
  204. * on an enabled functions. drm_helper_disable_unused_functions() on the other
  205. * hand throws such guarantees into the wind and calls disable hooks
  206. * unconditionally on unused functions.
  207. */
  208. void drm_helper_disable_unused_functions(struct drm_device *dev)
  209. {
  210. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  211. drm_modeset_lock_all(dev);
  212. __drm_helper_disable_unused_functions(dev);
  213. drm_modeset_unlock_all(dev);
  214. }
  215. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  216. /*
  217. * Check the CRTC we're going to map each output to vs. its current
  218. * CRTC. If they don't match, we have to disable the output and the CRTC
  219. * since the driver will have to re-route things.
  220. */
  221. static void
  222. drm_crtc_prepare_encoders(struct drm_device *dev)
  223. {
  224. const struct drm_encoder_helper_funcs *encoder_funcs;
  225. struct drm_encoder *encoder;
  226. drm_for_each_encoder(encoder, dev) {
  227. encoder_funcs = encoder->helper_private;
  228. if (!encoder_funcs)
  229. continue;
  230. /* Disable unused encoders */
  231. if (encoder->crtc == NULL)
  232. drm_encoder_disable(encoder);
  233. }
  234. }
  235. /**
  236. * drm_crtc_helper_set_mode - internal helper to set a mode
  237. * @crtc: CRTC to program
  238. * @mode: mode to use
  239. * @x: horizontal offset into the surface
  240. * @y: vertical offset into the surface
  241. * @old_fb: old framebuffer, for cleanup
  242. *
  243. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  244. * to fixup or reject the mode prior to trying to set it. This is an internal
  245. * helper that drivers could e.g. use to update properties that require the
  246. * entire output pipe to be disabled and re-enabled in a new configuration. For
  247. * example for changing whether audio is enabled on a hdmi link or for changing
  248. * panel fitter or dither attributes. It is also called by the
  249. * drm_crtc_helper_set_config() helper function to drive the mode setting
  250. * sequence.
  251. *
  252. * Returns:
  253. * True if the mode was set successfully, false otherwise.
  254. */
  255. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  256. struct drm_display_mode *mode,
  257. int x, int y,
  258. struct drm_framebuffer *old_fb)
  259. {
  260. struct drm_device *dev = crtc->dev;
  261. struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
  262. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  263. const struct drm_encoder_helper_funcs *encoder_funcs;
  264. int saved_x, saved_y;
  265. bool saved_enabled;
  266. struct drm_encoder *encoder;
  267. bool ret = true;
  268. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  269. drm_warn_on_modeset_not_all_locked(dev);
  270. saved_enabled = crtc->enabled;
  271. crtc->enabled = drm_helper_crtc_in_use(crtc);
  272. if (!crtc->enabled)
  273. return true;
  274. adjusted_mode = drm_mode_duplicate(dev, mode);
  275. if (!adjusted_mode) {
  276. crtc->enabled = saved_enabled;
  277. return false;
  278. }
  279. drm_mode_init(&saved_mode, &crtc->mode);
  280. drm_mode_init(&saved_hwmode, &crtc->hwmode);
  281. saved_x = crtc->x;
  282. saved_y = crtc->y;
  283. /* Update crtc values up front so the driver can rely on them for mode
  284. * setting.
  285. */
  286. drm_mode_copy(&crtc->mode, mode);
  287. crtc->x = x;
  288. crtc->y = y;
  289. /* Pass our mode to the connectors and the CRTC to give them a chance to
  290. * adjust it according to limitations or connector properties, and also
  291. * a chance to reject the mode entirely.
  292. */
  293. drm_for_each_encoder(encoder, dev) {
  294. if (encoder->crtc != crtc)
  295. continue;
  296. encoder_funcs = encoder->helper_private;
  297. if (!encoder_funcs)
  298. continue;
  299. if (encoder_funcs->mode_fixup) {
  300. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  301. adjusted_mode))) {
  302. drm_dbg_kms(dev, "[ENCODER:%d:%s] mode fixup failed\n",
  303. encoder->base.id, encoder->name);
  304. goto done;
  305. }
  306. }
  307. }
  308. if (crtc_funcs->mode_fixup) {
  309. if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
  310. adjusted_mode))) {
  311. drm_dbg_kms(dev, "[CRTC:%d:%s] mode fixup failed\n",
  312. crtc->base.id, crtc->name);
  313. goto done;
  314. }
  315. }
  316. drm_dbg_kms(dev, "[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  317. drm_mode_copy(&crtc->hwmode, adjusted_mode);
  318. /* Prepare the encoders and CRTCs before setting the mode. */
  319. drm_for_each_encoder(encoder, dev) {
  320. if (encoder->crtc != crtc)
  321. continue;
  322. encoder_funcs = encoder->helper_private;
  323. if (!encoder_funcs)
  324. continue;
  325. /* Disable the encoders as the first thing we do. */
  326. if (encoder_funcs->prepare)
  327. encoder_funcs->prepare(encoder);
  328. }
  329. drm_crtc_prepare_encoders(dev);
  330. crtc_funcs->prepare(crtc);
  331. /* Set up the DPLL and any encoders state that needs to adjust or depend
  332. * on the DPLL.
  333. */
  334. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  335. if (!ret)
  336. goto done;
  337. drm_for_each_encoder(encoder, dev) {
  338. if (encoder->crtc != crtc)
  339. continue;
  340. encoder_funcs = encoder->helper_private;
  341. if (!encoder_funcs)
  342. continue;
  343. drm_dbg_kms(dev, "[ENCODER:%d:%s] set [MODE:%s]\n",
  344. encoder->base.id, encoder->name, mode->name);
  345. if (encoder_funcs->mode_set)
  346. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  347. }
  348. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  349. crtc_funcs->commit(crtc);
  350. drm_for_each_encoder(encoder, dev) {
  351. if (encoder->crtc != crtc)
  352. continue;
  353. encoder_funcs = encoder->helper_private;
  354. if (!encoder_funcs)
  355. continue;
  356. if (encoder_funcs->commit)
  357. encoder_funcs->commit(encoder);
  358. }
  359. /* Calculate and store various constants which
  360. * are later needed by vblank and swap-completion
  361. * timestamping. They are derived from true hwmode.
  362. */
  363. drm_calc_timestamping_constants(crtc, &crtc->hwmode);
  364. /* FIXME: add subpixel order */
  365. done:
  366. drm_mode_destroy(dev, adjusted_mode);
  367. if (!ret) {
  368. crtc->enabled = saved_enabled;
  369. drm_mode_copy(&crtc->mode, &saved_mode);
  370. drm_mode_copy(&crtc->hwmode, &saved_hwmode);
  371. crtc->x = saved_x;
  372. crtc->y = saved_y;
  373. }
  374. return ret;
  375. }
  376. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  377. /**
  378. * drm_crtc_helper_atomic_check() - Helper to check CRTC atomic-state
  379. * @crtc: CRTC to check
  380. * @state: atomic state object
  381. *
  382. * Provides a default CRTC-state check handler for CRTCs that only have
  383. * one primary plane attached to it. This is often the case for the CRTC
  384. * of simple framebuffers.
  385. *
  386. * RETURNS:
  387. * Zero on success, or an errno code otherwise.
  388. */
  389. int drm_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
  390. {
  391. struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  392. if (!new_crtc_state->enable)
  393. return 0;
  394. return drm_atomic_helper_check_crtc_primary_plane(new_crtc_state);
  395. }
  396. EXPORT_SYMBOL(drm_crtc_helper_atomic_check);
  397. static void
  398. drm_crtc_helper_disable(struct drm_crtc *crtc)
  399. {
  400. struct drm_device *dev = crtc->dev;
  401. struct drm_connector *connector;
  402. struct drm_encoder *encoder;
  403. /* Decouple all encoders and their attached connectors from this crtc */
  404. drm_for_each_encoder(encoder, dev) {
  405. struct drm_connector_list_iter conn_iter;
  406. if (encoder->crtc != crtc)
  407. continue;
  408. drm_connector_list_iter_begin(dev, &conn_iter);
  409. drm_for_each_connector_iter(connector, &conn_iter) {
  410. if (connector->encoder != encoder)
  411. continue;
  412. connector->encoder = NULL;
  413. /*
  414. * drm_helper_disable_unused_functions() ought to be
  415. * doing this, but since we've decoupled the encoder
  416. * from the connector above, the required connection
  417. * between them is henceforth no longer available.
  418. */
  419. connector->dpms = DRM_MODE_DPMS_OFF;
  420. /* we keep a reference while the encoder is bound */
  421. drm_connector_put(connector);
  422. }
  423. drm_connector_list_iter_end(&conn_iter);
  424. }
  425. __drm_helper_disable_unused_functions(dev);
  426. }
  427. /*
  428. * For connectors that support multiple encoders, either the
  429. * .atomic_best_encoder() or .best_encoder() operation must be implemented.
  430. */
  431. struct drm_encoder *
  432. drm_connector_get_single_encoder(struct drm_connector *connector)
  433. {
  434. struct drm_encoder *encoder;
  435. drm_WARN_ON(connector->dev, hweight32(connector->possible_encoders) > 1);
  436. drm_connector_for_each_possible_encoder(connector, encoder)
  437. return encoder;
  438. return NULL;
  439. }
  440. /**
  441. * drm_crtc_helper_set_config - set a new config from userspace
  442. * @set: mode set configuration
  443. * @ctx: lock acquire context, not used here
  444. *
  445. * The drm_crtc_helper_set_config() helper function implements the of
  446. * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
  447. * helpers.
  448. *
  449. * It first tries to locate the best encoder for each connector by calling the
  450. * connector @drm_connector_helper_funcs.best_encoder helper operation.
  451. *
  452. * After locating the appropriate encoders, the helper function will call the
  453. * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
  454. * or reject it completely in which case an error will be returned to the
  455. * application. If the new configuration after mode adjustment is identical to
  456. * the current configuration the helper function will return without performing
  457. * any other operation.
  458. *
  459. * If the adjusted mode is identical to the current mode but changes to the
  460. * frame buffer need to be applied, the drm_crtc_helper_set_config() function
  461. * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
  462. *
  463. * If the adjusted mode differs from the current mode, or if the
  464. * ->mode_set_base() helper operation is not provided, the helper function
  465. * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
  466. * and ->commit() CRTC and encoder helper operations, in that order.
  467. * Alternatively it can also use the dpms and disable helper operations. For
  468. * details see &struct drm_crtc_helper_funcs and struct
  469. * &drm_encoder_helper_funcs.
  470. *
  471. * This function is deprecated. New drivers must implement atomic modeset
  472. * support, for which this function is unsuitable. Instead drivers should use
  473. * drm_atomic_helper_set_config().
  474. *
  475. * Returns:
  476. * Returns 0 on success, negative errno numbers on failure.
  477. */
  478. int drm_crtc_helper_set_config(struct drm_mode_set *set,
  479. struct drm_modeset_acquire_ctx *ctx)
  480. {
  481. struct drm_device *dev;
  482. struct drm_crtc **save_encoder_crtcs, *new_crtc;
  483. struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
  484. bool mode_changed = false; /* if true do a full mode set */
  485. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  486. struct drm_connector *connector;
  487. struct drm_connector_list_iter conn_iter;
  488. int count = 0, ro, fail = 0;
  489. const struct drm_crtc_helper_funcs *crtc_funcs;
  490. struct drm_mode_set save_set;
  491. int ret;
  492. int i;
  493. BUG_ON(!set);
  494. BUG_ON(!set->crtc);
  495. BUG_ON(!set->crtc->helper_private);
  496. /* Enforce sane interface api - has been abused by the fb helper. */
  497. BUG_ON(!set->mode && set->fb);
  498. BUG_ON(set->fb && set->num_connectors == 0);
  499. crtc_funcs = set->crtc->helper_private;
  500. dev = set->crtc->dev;
  501. drm_dbg_kms(dev, "\n");
  502. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  503. if (!set->mode)
  504. set->fb = NULL;
  505. if (set->fb) {
  506. drm_dbg_kms(dev, "[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  507. set->crtc->base.id, set->crtc->name,
  508. set->fb->base.id,
  509. (int)set->num_connectors, set->x, set->y);
  510. } else {
  511. drm_dbg_kms(dev, "[CRTC:%d:%s] [NOFB]\n",
  512. set->crtc->base.id, set->crtc->name);
  513. drm_crtc_helper_disable(set->crtc);
  514. return 0;
  515. }
  516. drm_warn_on_modeset_not_all_locked(dev);
  517. /*
  518. * Allocate space for the backup of all (non-pointer) encoder and
  519. * connector data.
  520. */
  521. save_encoder_crtcs = kzalloc_objs(struct drm_crtc *,
  522. dev->mode_config.num_encoder);
  523. if (!save_encoder_crtcs)
  524. return -ENOMEM;
  525. save_connector_encoders = kzalloc_objs(struct drm_encoder *,
  526. dev->mode_config.num_connector);
  527. if (!save_connector_encoders) {
  528. kfree(save_encoder_crtcs);
  529. return -ENOMEM;
  530. }
  531. /*
  532. * Copy data. Note that driver private data is not affected.
  533. * Should anything bad happen only the expected state is
  534. * restored, not the drivers personal bookkeeping.
  535. */
  536. count = 0;
  537. drm_for_each_encoder(encoder, dev) {
  538. save_encoder_crtcs[count++] = encoder->crtc;
  539. }
  540. count = 0;
  541. drm_connector_list_iter_begin(dev, &conn_iter);
  542. drm_for_each_connector_iter(connector, &conn_iter)
  543. save_connector_encoders[count++] = connector->encoder;
  544. drm_connector_list_iter_end(&conn_iter);
  545. save_set.crtc = set->crtc;
  546. save_set.mode = &set->crtc->mode;
  547. save_set.x = set->crtc->x;
  548. save_set.y = set->crtc->y;
  549. save_set.fb = set->crtc->primary->fb;
  550. /* We should be able to check here if the fb has the same properties
  551. * and then just flip_or_move it */
  552. if (set->crtc->primary->fb != set->fb) {
  553. /* If we have no fb then treat it as a full mode set */
  554. if (set->crtc->primary->fb == NULL) {
  555. drm_dbg_kms(dev, "[CRTC:%d:%s] no fb, full mode set\n",
  556. set->crtc->base.id, set->crtc->name);
  557. mode_changed = true;
  558. } else if (set->fb->format != set->crtc->primary->fb->format) {
  559. mode_changed = true;
  560. } else
  561. fb_changed = true;
  562. }
  563. if (set->x != set->crtc->x || set->y != set->crtc->y)
  564. fb_changed = true;
  565. if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
  566. drm_dbg_kms(dev, "[CRTC:%d:%s] modes are different, full mode set:\n",
  567. set->crtc->base.id, set->crtc->name);
  568. drm_dbg_kms(dev, DRM_MODE_FMT "\n", DRM_MODE_ARG(&set->crtc->mode));
  569. drm_dbg_kms(dev, DRM_MODE_FMT "\n", DRM_MODE_ARG(set->mode));
  570. mode_changed = true;
  571. }
  572. /* take a reference on all unbound connectors in set, reuse the
  573. * already taken reference for bound connectors
  574. */
  575. for (ro = 0; ro < set->num_connectors; ro++) {
  576. if (set->connectors[ro]->encoder)
  577. continue;
  578. drm_connector_get(set->connectors[ro]);
  579. }
  580. /* a) traverse passed in connector list and get encoders for them */
  581. count = 0;
  582. drm_connector_list_iter_begin(dev, &conn_iter);
  583. drm_for_each_connector_iter(connector, &conn_iter) {
  584. const struct drm_connector_helper_funcs *connector_funcs =
  585. connector->helper_private;
  586. new_encoder = connector->encoder;
  587. for (ro = 0; ro < set->num_connectors; ro++) {
  588. if (set->connectors[ro] == connector) {
  589. if (connector_funcs->best_encoder)
  590. new_encoder = connector_funcs->best_encoder(connector);
  591. else
  592. new_encoder = drm_connector_get_single_encoder(connector);
  593. /* if we can't get an encoder for a connector
  594. we are setting now - then fail */
  595. if (new_encoder == NULL)
  596. /* don't break so fail path works correct */
  597. fail = 1;
  598. if (connector->dpms != DRM_MODE_DPMS_ON) {
  599. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] DPMS not on, full mode switch\n",
  600. connector->base.id, connector->name);
  601. mode_changed = true;
  602. }
  603. break;
  604. }
  605. }
  606. if (new_encoder != connector->encoder) {
  607. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  608. connector->base.id, connector->name);
  609. mode_changed = true;
  610. /* If the encoder is reused for another connector, then
  611. * the appropriate crtc will be set later.
  612. */
  613. if (connector->encoder)
  614. connector->encoder->crtc = NULL;
  615. connector->encoder = new_encoder;
  616. }
  617. }
  618. drm_connector_list_iter_end(&conn_iter);
  619. if (fail) {
  620. ret = -EINVAL;
  621. goto fail;
  622. }
  623. count = 0;
  624. drm_connector_list_iter_begin(dev, &conn_iter);
  625. drm_for_each_connector_iter(connector, &conn_iter) {
  626. if (!connector->encoder)
  627. continue;
  628. if (connector->encoder->crtc == set->crtc)
  629. new_crtc = NULL;
  630. else
  631. new_crtc = connector->encoder->crtc;
  632. for (ro = 0; ro < set->num_connectors; ro++) {
  633. if (set->connectors[ro] == connector)
  634. new_crtc = set->crtc;
  635. }
  636. /* Make sure the new CRTC will work with the encoder */
  637. if (new_crtc &&
  638. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  639. ret = -EINVAL;
  640. drm_connector_list_iter_end(&conn_iter);
  641. goto fail;
  642. }
  643. if (new_crtc != connector->encoder->crtc) {
  644. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] CRTC changed, full mode switch\n",
  645. connector->base.id, connector->name);
  646. mode_changed = true;
  647. connector->encoder->crtc = new_crtc;
  648. }
  649. if (new_crtc) {
  650. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
  651. connector->base.id, connector->name,
  652. new_crtc->base.id, new_crtc->name);
  653. } else {
  654. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] to [NOCRTC]\n",
  655. connector->base.id, connector->name);
  656. }
  657. }
  658. drm_connector_list_iter_end(&conn_iter);
  659. /* mode_set_base is not a required function */
  660. if (fb_changed && !crtc_funcs->mode_set_base)
  661. mode_changed = true;
  662. if (mode_changed) {
  663. if (drm_helper_crtc_in_use(set->crtc)) {
  664. drm_dbg_kms(dev, "[CRTC:%d:%s] attempting to set mode from userspace: " DRM_MODE_FMT "\n",
  665. set->crtc->base.id, set->crtc->name, DRM_MODE_ARG(set->mode));
  666. set->crtc->primary->fb = set->fb;
  667. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  668. set->x, set->y,
  669. save_set.fb)) {
  670. drm_err(dev, "[CRTC:%d:%s] failed to set mode\n",
  671. set->crtc->base.id, set->crtc->name);
  672. set->crtc->primary->fb = save_set.fb;
  673. ret = -EINVAL;
  674. goto fail;
  675. }
  676. drm_dbg_kms(dev, "[CRTC:%d:%s] Setting connector DPMS state to on\n",
  677. set->crtc->base.id, set->crtc->name);
  678. for (i = 0; i < set->num_connectors; i++) {
  679. drm_dbg_kms(dev, "\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  680. set->connectors[i]->name);
  681. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  682. }
  683. }
  684. __drm_helper_disable_unused_functions(dev);
  685. } else if (fb_changed) {
  686. set->crtc->x = set->x;
  687. set->crtc->y = set->y;
  688. set->crtc->primary->fb = set->fb;
  689. ret = crtc_funcs->mode_set_base(set->crtc,
  690. set->x, set->y, save_set.fb);
  691. if (ret != 0) {
  692. set->crtc->x = save_set.x;
  693. set->crtc->y = save_set.y;
  694. set->crtc->primary->fb = save_set.fb;
  695. goto fail;
  696. }
  697. }
  698. kfree(save_connector_encoders);
  699. kfree(save_encoder_crtcs);
  700. return 0;
  701. fail:
  702. /* Restore all previous data. */
  703. count = 0;
  704. drm_for_each_encoder(encoder, dev) {
  705. encoder->crtc = save_encoder_crtcs[count++];
  706. }
  707. count = 0;
  708. drm_connector_list_iter_begin(dev, &conn_iter);
  709. drm_for_each_connector_iter(connector, &conn_iter)
  710. connector->encoder = save_connector_encoders[count++];
  711. drm_connector_list_iter_end(&conn_iter);
  712. /* after fail drop reference on all unbound connectors in set, let
  713. * bound connectors keep their reference
  714. */
  715. for (ro = 0; ro < set->num_connectors; ro++) {
  716. if (set->connectors[ro]->encoder)
  717. continue;
  718. drm_connector_put(set->connectors[ro]);
  719. }
  720. /* Try to restore the config */
  721. if (mode_changed &&
  722. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  723. save_set.y, save_set.fb))
  724. drm_err(dev, "failed to restore config after modeset failure\n");
  725. kfree(save_connector_encoders);
  726. kfree(save_encoder_crtcs);
  727. return ret;
  728. }
  729. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  730. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  731. {
  732. int dpms = DRM_MODE_DPMS_OFF;
  733. struct drm_connector *connector;
  734. struct drm_connector_list_iter conn_iter;
  735. struct drm_device *dev = encoder->dev;
  736. drm_connector_list_iter_begin(dev, &conn_iter);
  737. drm_for_each_connector_iter(connector, &conn_iter)
  738. if (connector->encoder == encoder)
  739. if (connector->dpms < dpms)
  740. dpms = connector->dpms;
  741. drm_connector_list_iter_end(&conn_iter);
  742. return dpms;
  743. }
  744. /* Helper which handles bridge ordering around encoder dpms */
  745. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  746. {
  747. const struct drm_encoder_helper_funcs *encoder_funcs;
  748. encoder_funcs = encoder->helper_private;
  749. if (!encoder_funcs)
  750. return;
  751. if (encoder_funcs->dpms)
  752. encoder_funcs->dpms(encoder, mode);
  753. }
  754. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  755. {
  756. int dpms = DRM_MODE_DPMS_OFF;
  757. struct drm_connector *connector;
  758. struct drm_connector_list_iter conn_iter;
  759. struct drm_device *dev = crtc->dev;
  760. drm_connector_list_iter_begin(dev, &conn_iter);
  761. drm_for_each_connector_iter(connector, &conn_iter)
  762. if (connector->encoder && connector->encoder->crtc == crtc)
  763. if (connector->dpms < dpms)
  764. dpms = connector->dpms;
  765. drm_connector_list_iter_end(&conn_iter);
  766. return dpms;
  767. }
  768. /**
  769. * drm_helper_connector_dpms() - connector dpms helper implementation
  770. * @connector: affected connector
  771. * @mode: DPMS mode
  772. *
  773. * The drm_helper_connector_dpms() helper function implements the
  774. * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
  775. * helpers.
  776. *
  777. * This is the main helper function provided by the CRTC helper framework for
  778. * implementing the DPMS connector attribute. It computes the new desired DPMS
  779. * state for all encoders and CRTCs in the output mesh and calls the
  780. * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
  781. * provided by the driver.
  782. *
  783. * This function is deprecated. New drivers must implement atomic modeset
  784. * support, where DPMS is handled in the DRM core.
  785. *
  786. * Returns:
  787. * Always returns 0.
  788. */
  789. int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  790. {
  791. struct drm_encoder *encoder = connector->encoder;
  792. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  793. int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  794. drm_WARN_ON(connector->dev, drm_drv_uses_atomic_modeset(connector->dev));
  795. if (mode == connector->dpms)
  796. return 0;
  797. old_dpms = connector->dpms;
  798. connector->dpms = mode;
  799. if (encoder)
  800. encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  801. /* from off to on, do crtc then encoder */
  802. if (mode < old_dpms) {
  803. if (crtc) {
  804. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  805. if (crtc_funcs->dpms)
  806. (*crtc_funcs->dpms) (crtc,
  807. drm_helper_choose_crtc_dpms(crtc));
  808. }
  809. if (encoder)
  810. drm_helper_encoder_dpms(encoder, encoder_dpms);
  811. }
  812. /* from on to off, do encoder then crtc */
  813. if (mode > old_dpms) {
  814. if (encoder)
  815. drm_helper_encoder_dpms(encoder, encoder_dpms);
  816. if (crtc) {
  817. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  818. if (crtc_funcs->dpms)
  819. (*crtc_funcs->dpms) (crtc,
  820. drm_helper_choose_crtc_dpms(crtc));
  821. }
  822. }
  823. return 0;
  824. }
  825. EXPORT_SYMBOL(drm_helper_connector_dpms);
  826. /**
  827. * drm_helper_resume_force_mode - force-restore mode setting configuration
  828. * @dev: drm_device which should be restored
  829. *
  830. * Drivers which use the mode setting helpers can use this function to
  831. * force-restore the mode setting configuration e.g. on resume or when something
  832. * else might have trampled over the hw state (like some overzealous old BIOSen
  833. * tended to do).
  834. *
  835. * This helper doesn't provide a error return value since restoring the old
  836. * config should never fail due to resource allocation issues since the driver
  837. * has successfully set the restored configuration already. Hence this should
  838. * boil down to the equivalent of a few dpms on calls, which also don't provide
  839. * an error code.
  840. *
  841. * Drivers where simply restoring an old configuration again might fail (e.g.
  842. * due to slight differences in allocating shared resources when the
  843. * configuration is restored in a different order than when userspace set it up)
  844. * need to use their own restore logic.
  845. *
  846. * This function is deprecated. New drivers should implement atomic mode-
  847. * setting and use the atomic suspend/resume helpers.
  848. *
  849. * See also:
  850. * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
  851. */
  852. void drm_helper_resume_force_mode(struct drm_device *dev)
  853. {
  854. struct drm_crtc *crtc;
  855. struct drm_encoder *encoder;
  856. const struct drm_crtc_helper_funcs *crtc_funcs;
  857. int encoder_dpms;
  858. bool ret;
  859. drm_WARN_ON(dev, drm_drv_uses_atomic_modeset(dev));
  860. drm_modeset_lock_all(dev);
  861. drm_for_each_crtc(crtc, dev) {
  862. if (!crtc->enabled)
  863. continue;
  864. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  865. crtc->x, crtc->y, crtc->primary->fb);
  866. /* Restoring the old config should never fail! */
  867. if (ret == false)
  868. drm_err(dev, "failed to set mode on crtc %p\n", crtc);
  869. /* Turn off outputs that were already powered off */
  870. if (drm_helper_choose_crtc_dpms(crtc)) {
  871. drm_for_each_encoder(encoder, dev) {
  872. if(encoder->crtc != crtc)
  873. continue;
  874. encoder_dpms = drm_helper_choose_encoder_dpms(
  875. encoder);
  876. drm_helper_encoder_dpms(encoder, encoder_dpms);
  877. }
  878. crtc_funcs = crtc->helper_private;
  879. if (crtc_funcs->dpms)
  880. (*crtc_funcs->dpms) (crtc,
  881. drm_helper_choose_crtc_dpms(crtc));
  882. }
  883. }
  884. /* disable the unused connectors while restoring the modesetting */
  885. __drm_helper_disable_unused_functions(dev);
  886. drm_modeset_unlock_all(dev);
  887. }
  888. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  889. /**
  890. * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
  891. * @dev: DRM device whose CRTCs to turn off
  892. *
  893. * Drivers may want to call this on unload to ensure that all displays are
  894. * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
  895. *
  896. * Note: This should only be used by non-atomic legacy drivers. For an atomic
  897. * version look at drm_atomic_helper_shutdown().
  898. *
  899. * Returns:
  900. * Zero on success, error code on failure.
  901. */
  902. int drm_helper_force_disable_all(struct drm_device *dev)
  903. {
  904. struct drm_crtc *crtc;
  905. int ret = 0;
  906. drm_modeset_lock_all(dev);
  907. drm_for_each_crtc(crtc, dev)
  908. if (crtc->enabled) {
  909. struct drm_mode_set set = {
  910. .crtc = crtc,
  911. };
  912. ret = drm_mode_set_config_internal(&set);
  913. if (ret)
  914. goto out;
  915. }
  916. out:
  917. drm_modeset_unlock_all(dev);
  918. return ret;
  919. }
  920. EXPORT_SYMBOL(drm_helper_force_disable_all);