drm_crtc_helper.c 33 KB

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