drm_crtc_helper.c 31 KB

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