drm_crtc_helper.c 33 KB

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