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