drm_crtc_helper.c 33 KB

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