drm_crtc_helper.c 30 KB

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