drm_crtc_helper.c 29 KB

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