drm_crtc_helper.c 29 KB

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