drm_crtc_helper.c 29 KB

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