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. drm_mode_sort(&connector->modes);
  167. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  168. drm_get_connector_name(connector));
  169. list_for_each_entry(mode, &connector->modes, head) {
  170. mode->vrefresh = drm_mode_vrefresh(mode);
  171. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  172. drm_mode_debug_printmodeline(mode);
  173. }
  174. return count;
  175. }
  176. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  177. /**
  178. * drm_helper_encoder_in_use - check if a given encoder is in use
  179. * @encoder: encoder to check
  180. *
  181. * LOCKING:
  182. * Caller must hold mode config lock.
  183. *
  184. * Walk @encoders's DRM device's mode_config and see if it's in use.
  185. *
  186. * RETURNS:
  187. * True if @encoder is part of the mode_config, false otherwise.
  188. */
  189. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  190. {
  191. struct drm_connector *connector;
  192. struct drm_device *dev = encoder->dev;
  193. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  194. if (connector->encoder == encoder)
  195. return true;
  196. return false;
  197. }
  198. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  199. /**
  200. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  201. * @crtc: CRTC to check
  202. *
  203. * LOCKING:
  204. * Caller must hold mode config lock.
  205. *
  206. * Walk @crtc's DRM device's mode_config and see if it's in use.
  207. *
  208. * RETURNS:
  209. * True if @crtc is part of the mode_config, false otherwise.
  210. */
  211. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  212. {
  213. struct drm_encoder *encoder;
  214. struct drm_device *dev = crtc->dev;
  215. /* FIXME: Locking around list access? */
  216. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  217. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  218. return true;
  219. return false;
  220. }
  221. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  222. static void
  223. drm_encoder_disable(struct drm_encoder *encoder)
  224. {
  225. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  226. if (encoder_funcs->disable)
  227. (*encoder_funcs->disable)(encoder);
  228. else
  229. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  230. }
  231. /**
  232. * drm_helper_disable_unused_functions - disable unused objects
  233. * @dev: DRM device
  234. *
  235. * LOCKING:
  236. * Caller must hold mode config lock.
  237. *
  238. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  239. * by calling its dpms function, which should power it off.
  240. */
  241. void drm_helper_disable_unused_functions(struct drm_device *dev)
  242. {
  243. struct drm_encoder *encoder;
  244. struct drm_connector *connector;
  245. struct drm_crtc *crtc;
  246. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  247. if (!connector->encoder)
  248. continue;
  249. if (connector->status == connector_status_disconnected)
  250. connector->encoder = NULL;
  251. }
  252. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  253. if (!drm_helper_encoder_in_use(encoder)) {
  254. drm_encoder_disable(encoder);
  255. /* disconnector encoder from any connector */
  256. encoder->crtc = NULL;
  257. }
  258. }
  259. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  260. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  261. crtc->enabled = drm_helper_crtc_in_use(crtc);
  262. if (!crtc->enabled) {
  263. if (crtc_funcs->disable)
  264. (*crtc_funcs->disable)(crtc);
  265. else
  266. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  267. crtc->fb = NULL;
  268. }
  269. }
  270. }
  271. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  272. /**
  273. * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  274. * @encoder: encoder to test
  275. * @crtc: crtc to test
  276. *
  277. * Return false if @encoder can't be driven by @crtc, true otherwise.
  278. */
  279. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  280. struct drm_crtc *crtc)
  281. {
  282. struct drm_device *dev;
  283. struct drm_crtc *tmp;
  284. int crtc_mask = 1;
  285. WARN(!crtc, "checking null crtc?\n");
  286. dev = crtc->dev;
  287. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  288. if (tmp == crtc)
  289. break;
  290. crtc_mask <<= 1;
  291. }
  292. if (encoder->possible_crtcs & crtc_mask)
  293. return true;
  294. return false;
  295. }
  296. /*
  297. * Check the CRTC we're going to map each output to vs. its current
  298. * CRTC. If they don't match, we have to disable the output and the CRTC
  299. * since the driver will have to re-route things.
  300. */
  301. static void
  302. drm_crtc_prepare_encoders(struct drm_device *dev)
  303. {
  304. struct drm_encoder_helper_funcs *encoder_funcs;
  305. struct drm_encoder *encoder;
  306. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  307. encoder_funcs = encoder->helper_private;
  308. /* Disable unused encoders */
  309. if (encoder->crtc == NULL)
  310. drm_encoder_disable(encoder);
  311. /* Disable encoders whose CRTC is about to change */
  312. if (encoder_funcs->get_crtc &&
  313. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  314. drm_encoder_disable(encoder);
  315. }
  316. }
  317. /**
  318. * drm_crtc_helper_set_mode - internal helper to set a mode
  319. * @crtc: CRTC to program
  320. * @mode: mode to use
  321. * @x: horizontal offset into the surface
  322. * @y: vertical offset into the surface
  323. * @old_fb: old framebuffer, for cleanup
  324. *
  325. * LOCKING:
  326. * Caller must hold mode config lock.
  327. *
  328. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  329. * to fixup or reject the mode prior to trying to set it. This is an internal
  330. * helper that drivers could e.g. use to update properties that require the
  331. * entire output pipe to be disabled and re-enabled in a new configuration. For
  332. * example for changing whether audio is enabled on a hdmi link or for changing
  333. * panel fitter or dither attributes. It is also called by the
  334. * drm_crtc_helper_set_config() helper function to drive the mode setting
  335. * sequence.
  336. *
  337. * RETURNS:
  338. * True if the mode was set successfully, or false otherwise.
  339. */
  340. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  341. struct drm_display_mode *mode,
  342. int x, int y,
  343. struct drm_framebuffer *old_fb)
  344. {
  345. struct drm_device *dev = crtc->dev;
  346. struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
  347. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  348. struct drm_encoder_helper_funcs *encoder_funcs;
  349. int saved_x, saved_y;
  350. struct drm_encoder *encoder;
  351. bool ret = true;
  352. crtc->enabled = drm_helper_crtc_in_use(crtc);
  353. if (!crtc->enabled)
  354. return true;
  355. adjusted_mode = drm_mode_duplicate(dev, mode);
  356. if (!adjusted_mode)
  357. return false;
  358. saved_hwmode = crtc->hwmode;
  359. saved_mode = crtc->mode;
  360. saved_x = crtc->x;
  361. saved_y = crtc->y;
  362. /* Update crtc values up front so the driver can rely on them for mode
  363. * setting.
  364. */
  365. crtc->mode = *mode;
  366. crtc->x = x;
  367. crtc->y = y;
  368. /* Pass our mode to the connectors and the CRTC to give them a chance to
  369. * adjust it according to limitations or connector properties, and also
  370. * a chance to reject the mode entirely.
  371. */
  372. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  373. if (encoder->crtc != crtc)
  374. continue;
  375. encoder_funcs = encoder->helper_private;
  376. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  377. adjusted_mode))) {
  378. DRM_DEBUG_KMS("Encoder fixup failed\n");
  379. goto done;
  380. }
  381. }
  382. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  383. DRM_DEBUG_KMS("CRTC fixup failed\n");
  384. goto done;
  385. }
  386. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  387. /* Prepare the encoders and CRTCs before setting the mode. */
  388. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  389. if (encoder->crtc != crtc)
  390. continue;
  391. encoder_funcs = encoder->helper_private;
  392. /* Disable the encoders as the first thing we do. */
  393. encoder_funcs->prepare(encoder);
  394. }
  395. drm_crtc_prepare_encoders(dev);
  396. crtc_funcs->prepare(crtc);
  397. /* Set up the DPLL and any encoders state that needs to adjust or depend
  398. * on the DPLL.
  399. */
  400. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  401. if (!ret)
  402. goto done;
  403. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  404. if (encoder->crtc != crtc)
  405. continue;
  406. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  407. encoder->base.id, drm_get_encoder_name(encoder),
  408. mode->base.id, mode->name);
  409. encoder_funcs = encoder->helper_private;
  410. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  411. }
  412. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  413. crtc_funcs->commit(crtc);
  414. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  415. if (encoder->crtc != crtc)
  416. continue;
  417. encoder_funcs = encoder->helper_private;
  418. encoder_funcs->commit(encoder);
  419. }
  420. /* Store real post-adjustment hardware mode. */
  421. crtc->hwmode = *adjusted_mode;
  422. /* Calculate and store various constants which
  423. * are later needed by vblank and swap-completion
  424. * timestamping. They are derived from true hwmode.
  425. */
  426. drm_calc_timestamping_constants(crtc);
  427. /* FIXME: add subpixel order */
  428. done:
  429. drm_mode_destroy(dev, adjusted_mode);
  430. if (!ret) {
  431. crtc->hwmode = saved_hwmode;
  432. crtc->mode = saved_mode;
  433. crtc->x = saved_x;
  434. crtc->y = saved_y;
  435. }
  436. return ret;
  437. }
  438. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  439. static int
  440. drm_crtc_helper_disable(struct drm_crtc *crtc)
  441. {
  442. struct drm_device *dev = crtc->dev;
  443. struct drm_connector *connector;
  444. struct drm_encoder *encoder;
  445. /* Decouple all encoders and their attached connectors from this crtc */
  446. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  447. if (encoder->crtc != crtc)
  448. continue;
  449. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  450. if (connector->encoder != encoder)
  451. continue;
  452. connector->encoder = NULL;
  453. }
  454. }
  455. drm_helper_disable_unused_functions(dev);
  456. return 0;
  457. }
  458. /**
  459. * drm_crtc_helper_set_config - set a new config from userspace
  460. * @set: mode set configuration
  461. *
  462. * LOCKING:
  463. * Caller must hold mode config lock.
  464. *
  465. * Setup a new configuration, provided by the upper layers (either an ioctl call
  466. * from userspace or internally e.g. from the fbdev suppport code) in @set, and
  467. * enable it. This is the main helper functions for drivers that implement
  468. * kernel mode setting with the crtc helper functions and the assorted
  469. * ->prepare(), ->modeset() and ->commit() helper callbacks.
  470. *
  471. * RETURNS:
  472. * Returns 0 on success, -ERRNO on failure.
  473. */
  474. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  475. {
  476. struct drm_device *dev;
  477. struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  478. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  479. struct drm_framebuffer *old_fb = NULL;
  480. bool mode_changed = false; /* if true do a full mode set */
  481. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  482. struct drm_connector *save_connectors, *connector;
  483. int count = 0, ro, fail = 0;
  484. struct drm_crtc_helper_funcs *crtc_funcs;
  485. struct drm_mode_set save_set;
  486. int ret;
  487. int i;
  488. DRM_DEBUG_KMS("\n");
  489. if (!set)
  490. return -EINVAL;
  491. if (!set->crtc)
  492. return -EINVAL;
  493. if (!set->crtc->helper_private)
  494. return -EINVAL;
  495. crtc_funcs = set->crtc->helper_private;
  496. if (!set->mode)
  497. set->fb = NULL;
  498. if (set->fb) {
  499. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  500. set->crtc->base.id, set->fb->base.id,
  501. (int)set->num_connectors, set->x, set->y);
  502. } else {
  503. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  504. return drm_crtc_helper_disable(set->crtc);
  505. }
  506. dev = set->crtc->dev;
  507. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  508. * connector data. */
  509. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  510. sizeof(struct drm_crtc), GFP_KERNEL);
  511. if (!save_crtcs)
  512. return -ENOMEM;
  513. save_encoders = kzalloc(dev->mode_config.num_encoder *
  514. sizeof(struct drm_encoder), GFP_KERNEL);
  515. if (!save_encoders) {
  516. kfree(save_crtcs);
  517. return -ENOMEM;
  518. }
  519. save_connectors = kzalloc(dev->mode_config.num_connector *
  520. sizeof(struct drm_connector), GFP_KERNEL);
  521. if (!save_connectors) {
  522. kfree(save_crtcs);
  523. kfree(save_encoders);
  524. return -ENOMEM;
  525. }
  526. /* Copy data. Note that driver private data is not affected.
  527. * Should anything bad happen only the expected state is
  528. * restored, not the drivers personal bookkeeping.
  529. */
  530. count = 0;
  531. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  532. save_crtcs[count++] = *crtc;
  533. }
  534. count = 0;
  535. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  536. save_encoders[count++] = *encoder;
  537. }
  538. count = 0;
  539. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  540. save_connectors[count++] = *connector;
  541. }
  542. save_set.crtc = set->crtc;
  543. save_set.mode = &set->crtc->mode;
  544. save_set.x = set->crtc->x;
  545. save_set.y = set->crtc->y;
  546. save_set.fb = set->crtc->fb;
  547. /* We should be able to check here if the fb has the same properties
  548. * and then just flip_or_move it */
  549. if (set->crtc->fb != set->fb) {
  550. /* If we have no fb then treat it as a full mode set */
  551. if (set->crtc->fb == NULL) {
  552. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  553. mode_changed = true;
  554. } else if (set->fb == NULL) {
  555. mode_changed = true;
  556. } else if (set->fb->depth != set->crtc->fb->depth) {
  557. mode_changed = true;
  558. } else if (set->fb->bits_per_pixel !=
  559. set->crtc->fb->bits_per_pixel) {
  560. mode_changed = true;
  561. } else if (set->fb->pixel_format !=
  562. set->crtc->fb->pixel_format) {
  563. mode_changed = true;
  564. } else
  565. fb_changed = true;
  566. }
  567. if (set->x != set->crtc->x || set->y != set->crtc->y)
  568. fb_changed = true;
  569. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  570. DRM_DEBUG_KMS("modes are different, full mode set\n");
  571. drm_mode_debug_printmodeline(&set->crtc->mode);
  572. drm_mode_debug_printmodeline(set->mode);
  573. mode_changed = true;
  574. }
  575. /* a) traverse passed in connector list and get encoders for them */
  576. count = 0;
  577. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  578. struct drm_connector_helper_funcs *connector_funcs =
  579. connector->helper_private;
  580. new_encoder = connector->encoder;
  581. for (ro = 0; ro < set->num_connectors; ro++) {
  582. if (set->connectors[ro] == connector) {
  583. new_encoder = connector_funcs->best_encoder(connector);
  584. /* if we can't get an encoder for a connector
  585. we are setting now - then fail */
  586. if (new_encoder == NULL)
  587. /* don't break so fail path works correct */
  588. fail = 1;
  589. break;
  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);