drm_crtc_helper.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. *
  5. * DRM core CRTC related functions
  6. *
  7. * Permission to use, copy, modify, distribute, and sell this software and its
  8. * documentation for any purpose is hereby granted without fee, provided that
  9. * the above copyright notice appear in all copies and that both that copyright
  10. * notice and this permission notice appear in supporting documentation, and
  11. * that the name of the copyright holders not be used in advertising or
  12. * publicity pertaining to distribution of the software without specific,
  13. * written prior permission. The copyright holders make no representations
  14. * about the suitability of this software for any purpose. It is provided "as
  15. * is" without express or implied warranty.
  16. *
  17. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  19. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  21. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  22. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. *
  25. * Authors:
  26. * Keith Packard
  27. * Eric Anholt <eric@anholt.net>
  28. * Dave Airlie <airlied@linux.ie>
  29. * Jesse Barnes <jesse.barnes@intel.com>
  30. */
  31. #include <linux/export.h>
  32. #include <linux/moduleparam.h>
  33. #include <drm/drmP.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_fourcc.h>
  36. #include <drm/drm_crtc_helper.h>
  37. #include <drm/drm_fb_helper.h>
  38. #include <drm/drm_edid.h>
  39. /**
  40. * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  41. * connector list
  42. * @dev: drm device to operate on
  43. *
  44. * Some userspace presumes that the first connected connector is the main
  45. * display, where it's supposed to display e.g. the login screen. For
  46. * laptops, this should be the main panel. Use this function to sort all
  47. * (eDP/LVDS) panels to the front of the connector list, instead of
  48. * painstakingly trying to initialize them in the right order.
  49. */
  50. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  51. {
  52. struct drm_connector *connector, *tmp;
  53. struct list_head panel_list;
  54. INIT_LIST_HEAD(&panel_list);
  55. list_for_each_entry_safe(connector, tmp,
  56. &dev->mode_config.connector_list, head) {
  57. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  58. connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  59. list_move_tail(&connector->head, &panel_list);
  60. }
  61. list_splice(&panel_list, &dev->mode_config.connector_list);
  62. }
  63. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  64. static bool drm_kms_helper_poll = true;
  65. module_param_named(poll, drm_kms_helper_poll, bool, 0600);
  66. static void drm_mode_validate_flag(struct drm_connector *connector,
  67. int flags)
  68. {
  69. struct drm_display_mode *mode;
  70. if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
  71. return;
  72. list_for_each_entry(mode, &connector->modes, head) {
  73. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  74. !(flags & DRM_MODE_FLAG_INTERLACE))
  75. mode->status = MODE_NO_INTERLACE;
  76. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  77. !(flags & DRM_MODE_FLAG_DBLSCAN))
  78. mode->status = MODE_NO_DBLESCAN;
  79. }
  80. return;
  81. }
  82. /**
  83. * drm_helper_probe_single_connector_modes - get complete set of display modes
  84. * @connector: connector to probe
  85. * @maxX: max width for modes
  86. * @maxY: max height for modes
  87. *
  88. * LOCKING:
  89. * Caller must hold mode config lock.
  90. *
  91. * Based on the helper callbacks implemented by @connector try to detect all
  92. * valid modes. Modes will first be added to the connector's probed_modes list,
  93. * then culled (based on validity and the @maxX, @maxY parameters) and put into
  94. * the normal modes list.
  95. *
  96. * Intended to be use as a generic implementation of the ->probe() @connector
  97. * callback for drivers that use the crtc helpers for output mode filtering and
  98. * detection.
  99. *
  100. * RETURNS:
  101. * Number of modes found on @connector.
  102. */
  103. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  104. uint32_t maxX, uint32_t maxY)
  105. {
  106. struct drm_device *dev = connector->dev;
  107. struct drm_display_mode *mode;
  108. struct drm_connector_helper_funcs *connector_funcs =
  109. connector->helper_private;
  110. int count = 0;
  111. int mode_flags = 0;
  112. bool verbose_prune = true;
  113. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  114. drm_get_connector_name(connector));
  115. /* set all modes to the unverified state */
  116. list_for_each_entry(mode, &connector->modes, head)
  117. mode->status = MODE_UNVERIFIED;
  118. if (connector->force) {
  119. if (connector->force == DRM_FORCE_ON)
  120. connector->status = connector_status_connected;
  121. else
  122. connector->status = connector_status_disconnected;
  123. if (connector->funcs->force)
  124. connector->funcs->force(connector);
  125. } else {
  126. connector->status = connector->funcs->detect(connector, true);
  127. }
  128. /* Re-enable polling in case the global poll config changed. */
  129. if (drm_kms_helper_poll != dev->mode_config.poll_running)
  130. drm_kms_helper_poll_enable(dev);
  131. dev->mode_config.poll_running = drm_kms_helper_poll;
  132. if (connector->status == connector_status_disconnected) {
  133. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  134. connector->base.id, drm_get_connector_name(connector));
  135. drm_mode_connector_update_edid_property(connector, NULL);
  136. verbose_prune = false;
  137. goto prune;
  138. }
  139. #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
  140. count = drm_load_edid_firmware(connector);
  141. if (count == 0)
  142. #endif
  143. count = (*connector_funcs->get_modes)(connector);
  144. if (count == 0 && connector->status == connector_status_connected)
  145. count = drm_add_modes_noedid(connector, 1024, 768);
  146. if (count == 0)
  147. goto prune;
  148. drm_mode_connector_list_update(connector);
  149. if (maxX && maxY)
  150. drm_mode_validate_size(dev, &connector->modes, maxX,
  151. maxY, 0);
  152. if (connector->interlace_allowed)
  153. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  154. if (connector->doublescan_allowed)
  155. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  156. drm_mode_validate_flag(connector, mode_flags);
  157. list_for_each_entry(mode, &connector->modes, head) {
  158. if (mode->status == MODE_OK)
  159. mode->status = connector_funcs->mode_valid(connector,
  160. mode);
  161. }
  162. prune:
  163. drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
  164. if (list_empty(&connector->modes))
  165. return 0;
  166. list_for_each_entry(mode, &connector->modes, head)
  167. mode->vrefresh = drm_mode_vrefresh(mode);
  168. drm_mode_sort(&connector->modes);
  169. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  170. drm_get_connector_name(connector));
  171. list_for_each_entry(mode, &connector->modes, head) {
  172. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  173. drm_mode_debug_printmodeline(mode);
  174. }
  175. return count;
  176. }
  177. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  178. /**
  179. * drm_helper_encoder_in_use - check if a given encoder is in use
  180. * @encoder: encoder to check
  181. *
  182. * LOCKING:
  183. * Caller must hold mode config lock.
  184. *
  185. * Walk @encoders's DRM device's mode_config and see if it's in use.
  186. *
  187. * RETURNS:
  188. * True if @encoder is part of the mode_config, false otherwise.
  189. */
  190. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  191. {
  192. struct drm_connector *connector;
  193. struct drm_device *dev = encoder->dev;
  194. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  195. if (connector->encoder == encoder)
  196. return true;
  197. return false;
  198. }
  199. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  200. /**
  201. * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  202. * @crtc: CRTC to check
  203. *
  204. * LOCKING:
  205. * Caller must hold mode config lock.
  206. *
  207. * Walk @crtc's DRM device's mode_config and see if it's in use.
  208. *
  209. * RETURNS:
  210. * True if @crtc is part of the mode_config, false otherwise.
  211. */
  212. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  213. {
  214. struct drm_encoder *encoder;
  215. struct drm_device *dev = crtc->dev;
  216. /* FIXME: Locking around list access? */
  217. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  218. if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  219. return true;
  220. return false;
  221. }
  222. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  223. static void
  224. drm_encoder_disable(struct drm_encoder *encoder)
  225. {
  226. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  227. if (encoder_funcs->disable)
  228. (*encoder_funcs->disable)(encoder);
  229. else
  230. (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  231. }
  232. /**
  233. * drm_helper_disable_unused_functions - disable unused objects
  234. * @dev: DRM device
  235. *
  236. * LOCKING:
  237. * Caller must hold mode config lock.
  238. *
  239. * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  240. * by calling its dpms function, which should power it off.
  241. */
  242. void drm_helper_disable_unused_functions(struct drm_device *dev)
  243. {
  244. struct drm_encoder *encoder;
  245. struct drm_connector *connector;
  246. struct drm_crtc *crtc;
  247. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  248. if (!connector->encoder)
  249. continue;
  250. if (connector->status == connector_status_disconnected)
  251. connector->encoder = NULL;
  252. }
  253. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  254. if (!drm_helper_encoder_in_use(encoder)) {
  255. drm_encoder_disable(encoder);
  256. /* disconnector encoder from any connector */
  257. encoder->crtc = NULL;
  258. }
  259. }
  260. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  261. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  262. crtc->enabled = drm_helper_crtc_in_use(crtc);
  263. if (!crtc->enabled) {
  264. if (crtc_funcs->disable)
  265. (*crtc_funcs->disable)(crtc);
  266. else
  267. (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  268. crtc->fb = NULL;
  269. }
  270. }
  271. }
  272. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  273. /**
  274. * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  275. * @encoder: encoder to test
  276. * @crtc: crtc to test
  277. *
  278. * Return false if @encoder can't be driven by @crtc, true otherwise.
  279. */
  280. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  281. struct drm_crtc *crtc)
  282. {
  283. struct drm_device *dev;
  284. struct drm_crtc *tmp;
  285. int crtc_mask = 1;
  286. WARN(!crtc, "checking null crtc?\n");
  287. dev = crtc->dev;
  288. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  289. if (tmp == crtc)
  290. break;
  291. crtc_mask <<= 1;
  292. }
  293. if (encoder->possible_crtcs & crtc_mask)
  294. return true;
  295. return false;
  296. }
  297. /*
  298. * Check the CRTC we're going to map each output to vs. its current
  299. * CRTC. If they don't match, we have to disable the output and the CRTC
  300. * since the driver will have to re-route things.
  301. */
  302. static void
  303. drm_crtc_prepare_encoders(struct drm_device *dev)
  304. {
  305. struct drm_encoder_helper_funcs *encoder_funcs;
  306. struct drm_encoder *encoder;
  307. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  308. encoder_funcs = encoder->helper_private;
  309. /* Disable unused encoders */
  310. if (encoder->crtc == NULL)
  311. drm_encoder_disable(encoder);
  312. /* Disable encoders whose CRTC is about to change */
  313. if (encoder_funcs->get_crtc &&
  314. encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  315. drm_encoder_disable(encoder);
  316. }
  317. }
  318. /**
  319. * drm_crtc_helper_set_mode - internal helper to set a mode
  320. * @crtc: CRTC to program
  321. * @mode: mode to use
  322. * @x: horizontal offset into the surface
  323. * @y: vertical offset into the surface
  324. * @old_fb: old framebuffer, for cleanup
  325. *
  326. * LOCKING:
  327. * Caller must hold mode config lock.
  328. *
  329. * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
  330. * to fixup or reject the mode prior to trying to set it. This is an internal
  331. * helper that drivers could e.g. use to update properties that require the
  332. * entire output pipe to be disabled and re-enabled in a new configuration. For
  333. * example for changing whether audio is enabled on a hdmi link or for changing
  334. * panel fitter or dither attributes. It is also called by the
  335. * drm_crtc_helper_set_config() helper function to drive the mode setting
  336. * sequence.
  337. *
  338. * RETURNS:
  339. * True if the mode was set successfully, or false otherwise.
  340. */
  341. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  342. struct drm_display_mode *mode,
  343. int x, int y,
  344. struct drm_framebuffer *old_fb)
  345. {
  346. struct drm_device *dev = crtc->dev;
  347. struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
  348. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  349. struct drm_encoder_helper_funcs *encoder_funcs;
  350. int saved_x, saved_y;
  351. struct drm_encoder *encoder;
  352. bool ret = true;
  353. crtc->enabled = drm_helper_crtc_in_use(crtc);
  354. if (!crtc->enabled)
  355. return true;
  356. adjusted_mode = drm_mode_duplicate(dev, mode);
  357. if (!adjusted_mode)
  358. return false;
  359. saved_hwmode = crtc->hwmode;
  360. saved_mode = crtc->mode;
  361. saved_x = crtc->x;
  362. saved_y = crtc->y;
  363. /* Update crtc values up front so the driver can rely on them for mode
  364. * setting.
  365. */
  366. crtc->mode = *mode;
  367. crtc->x = x;
  368. crtc->y = y;
  369. /* Pass our mode to the connectors and the CRTC to give them a chance to
  370. * adjust it according to limitations or connector properties, and also
  371. * a chance to reject the mode entirely.
  372. */
  373. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  374. if (encoder->crtc != crtc)
  375. continue;
  376. encoder_funcs = encoder->helper_private;
  377. if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  378. adjusted_mode))) {
  379. DRM_DEBUG_KMS("Encoder fixup failed\n");
  380. goto done;
  381. }
  382. }
  383. if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  384. DRM_DEBUG_KMS("CRTC fixup failed\n");
  385. goto done;
  386. }
  387. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  388. /* Prepare the encoders and CRTCs before setting the mode. */
  389. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  390. if (encoder->crtc != crtc)
  391. continue;
  392. encoder_funcs = encoder->helper_private;
  393. /* Disable the encoders as the first thing we do. */
  394. encoder_funcs->prepare(encoder);
  395. }
  396. drm_crtc_prepare_encoders(dev);
  397. crtc_funcs->prepare(crtc);
  398. /* Set up the DPLL and any encoders state that needs to adjust or depend
  399. * on the DPLL.
  400. */
  401. ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  402. if (!ret)
  403. goto done;
  404. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  405. if (encoder->crtc != crtc)
  406. continue;
  407. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  408. encoder->base.id, drm_get_encoder_name(encoder),
  409. mode->base.id, mode->name);
  410. encoder_funcs = encoder->helper_private;
  411. encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  412. }
  413. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  414. crtc_funcs->commit(crtc);
  415. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  416. if (encoder->crtc != crtc)
  417. continue;
  418. encoder_funcs = encoder->helper_private;
  419. encoder_funcs->commit(encoder);
  420. }
  421. /* Store real post-adjustment hardware mode. */
  422. crtc->hwmode = *adjusted_mode;
  423. /* Calculate and store various constants which
  424. * are later needed by vblank and swap-completion
  425. * timestamping. They are derived from true hwmode.
  426. */
  427. drm_calc_timestamping_constants(crtc);
  428. /* FIXME: add subpixel order */
  429. done:
  430. drm_mode_destroy(dev, adjusted_mode);
  431. if (!ret) {
  432. crtc->hwmode = saved_hwmode;
  433. crtc->mode = saved_mode;
  434. crtc->x = saved_x;
  435. crtc->y = saved_y;
  436. }
  437. return ret;
  438. }
  439. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  440. static int
  441. drm_crtc_helper_disable(struct drm_crtc *crtc)
  442. {
  443. struct drm_device *dev = crtc->dev;
  444. struct drm_connector *connector;
  445. struct drm_encoder *encoder;
  446. /* Decouple all encoders and their attached connectors from this crtc */
  447. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  448. if (encoder->crtc != crtc)
  449. continue;
  450. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  451. if (connector->encoder != encoder)
  452. continue;
  453. connector->encoder = NULL;
  454. }
  455. }
  456. drm_helper_disable_unused_functions(dev);
  457. return 0;
  458. }
  459. /**
  460. * drm_crtc_helper_set_config - set a new config from userspace
  461. * @set: mode set configuration
  462. *
  463. * LOCKING:
  464. * Caller must hold mode config lock.
  465. *
  466. * Setup a new configuration, provided by the upper layers (either an ioctl call
  467. * from userspace or internally e.g. from the fbdev suppport code) in @set, and
  468. * enable it. This is the main helper functions for drivers that implement
  469. * kernel mode setting with the crtc helper functions and the assorted
  470. * ->prepare(), ->modeset() and ->commit() helper callbacks.
  471. *
  472. * RETURNS:
  473. * Returns 0 on success, -ERRNO on failure.
  474. */
  475. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  476. {
  477. struct drm_device *dev;
  478. struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  479. struct drm_encoder *save_encoders, *new_encoder, *encoder;
  480. struct drm_framebuffer *old_fb = NULL;
  481. bool mode_changed = false; /* if true do a full mode set */
  482. bool fb_changed = false; /* if true and !mode_changed just do a flip */
  483. struct drm_connector *save_connectors, *connector;
  484. int count = 0, ro, fail = 0;
  485. struct drm_crtc_helper_funcs *crtc_funcs;
  486. struct drm_mode_set save_set;
  487. int ret;
  488. int i;
  489. DRM_DEBUG_KMS("\n");
  490. BUG_ON(!set);
  491. BUG_ON(!set->crtc);
  492. BUG_ON(!set->crtc->helper_private);
  493. /* Enforce sane interface api - has been abused by the fb helper. */
  494. BUG_ON(!set->mode && set->fb);
  495. BUG_ON(set->fb && set->num_connectors == 0);
  496. crtc_funcs = set->crtc->helper_private;
  497. if (!set->mode)
  498. set->fb = NULL;
  499. if (set->fb) {
  500. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  501. set->crtc->base.id, set->fb->base.id,
  502. (int)set->num_connectors, set->x, set->y);
  503. } else {
  504. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  505. return drm_crtc_helper_disable(set->crtc);
  506. }
  507. dev = set->crtc->dev;
  508. /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  509. * connector data. */
  510. save_crtcs = kzalloc(dev->mode_config.num_crtc *
  511. sizeof(struct drm_crtc), GFP_KERNEL);
  512. if (!save_crtcs)
  513. return -ENOMEM;
  514. save_encoders = kzalloc(dev->mode_config.num_encoder *
  515. sizeof(struct drm_encoder), GFP_KERNEL);
  516. if (!save_encoders) {
  517. kfree(save_crtcs);
  518. return -ENOMEM;
  519. }
  520. save_connectors = kzalloc(dev->mode_config.num_connector *
  521. sizeof(struct drm_connector), GFP_KERNEL);
  522. if (!save_connectors) {
  523. kfree(save_crtcs);
  524. kfree(save_encoders);
  525. return -ENOMEM;
  526. }
  527. /* Copy data. Note that driver private data is not affected.
  528. * Should anything bad happen only the expected state is
  529. * restored, not the drivers personal bookkeeping.
  530. */
  531. count = 0;
  532. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  533. save_crtcs[count++] = *crtc;
  534. }
  535. count = 0;
  536. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  537. save_encoders[count++] = *encoder;
  538. }
  539. count = 0;
  540. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  541. save_connectors[count++] = *connector;
  542. }
  543. save_set.crtc = set->crtc;
  544. save_set.mode = &set->crtc->mode;
  545. save_set.x = set->crtc->x;
  546. save_set.y = set->crtc->y;
  547. save_set.fb = set->crtc->fb;
  548. /* We should be able to check here if the fb has the same properties
  549. * and then just flip_or_move it */
  550. if (set->crtc->fb != set->fb) {
  551. /* If we have no fb then treat it as a full mode set */
  552. if (set->crtc->fb == NULL) {
  553. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  554. mode_changed = true;
  555. } else if (set->fb == NULL) {
  556. mode_changed = true;
  557. } else if (set->fb->pixel_format !=
  558. set->crtc->fb->pixel_format) {
  559. mode_changed = true;
  560. } else
  561. fb_changed = true;
  562. }
  563. if (set->x != set->crtc->x || set->y != set->crtc->y)
  564. fb_changed = true;
  565. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  566. DRM_DEBUG_KMS("modes are different, full mode set\n");
  567. drm_mode_debug_printmodeline(&set->crtc->mode);
  568. drm_mode_debug_printmodeline(set->mode);
  569. mode_changed = true;
  570. }
  571. /* a) traverse passed in connector list and get encoders for them */
  572. count = 0;
  573. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  574. struct drm_connector_helper_funcs *connector_funcs =
  575. connector->helper_private;
  576. new_encoder = connector->encoder;
  577. for (ro = 0; ro < set->num_connectors; ro++) {
  578. if (set->connectors[ro] == connector) {
  579. new_encoder = connector_funcs->best_encoder(connector);
  580. /* if we can't get an encoder for a connector
  581. we are setting now - then fail */
  582. if (new_encoder == NULL)
  583. /* don't break so fail path works correct */
  584. fail = 1;
  585. break;
  586. }
  587. }
  588. if (new_encoder != connector->encoder) {
  589. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  590. mode_changed = true;
  591. /* If the encoder is reused for another connector, then
  592. * the appropriate crtc will be set later.
  593. */
  594. if (connector->encoder)
  595. connector->encoder->crtc = NULL;
  596. connector->encoder = new_encoder;
  597. }
  598. }
  599. if (fail) {
  600. ret = -EINVAL;
  601. goto fail;
  602. }
  603. count = 0;
  604. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  605. if (!connector->encoder)
  606. continue;
  607. if (connector->encoder->crtc == set->crtc)
  608. new_crtc = NULL;
  609. else
  610. new_crtc = connector->encoder->crtc;
  611. for (ro = 0; ro < set->num_connectors; ro++) {
  612. if (set->connectors[ro] == connector)
  613. new_crtc = set->crtc;
  614. }
  615. /* Make sure the new CRTC will work with the encoder */
  616. if (new_crtc &&
  617. !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  618. ret = -EINVAL;
  619. goto fail;
  620. }
  621. if (new_crtc != connector->encoder->crtc) {
  622. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  623. mode_changed = true;
  624. connector->encoder->crtc = new_crtc;
  625. }
  626. if (new_crtc) {
  627. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  628. connector->base.id, drm_get_connector_name(connector),
  629. new_crtc->base.id);
  630. } else {
  631. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  632. connector->base.id, drm_get_connector_name(connector));
  633. }
  634. }
  635. /* mode_set_base is not a required function */
  636. if (fb_changed && !crtc_funcs->mode_set_base)
  637. mode_changed = true;
  638. if (mode_changed) {
  639. set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
  640. if (set->crtc->enabled) {
  641. DRM_DEBUG_KMS("attempting to set mode from"
  642. " userspace\n");
  643. drm_mode_debug_printmodeline(set->mode);
  644. old_fb = set->crtc->fb;
  645. set->crtc->fb = set->fb;
  646. if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  647. set->x, set->y,
  648. old_fb)) {
  649. DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  650. set->crtc->base.id);
  651. set->crtc->fb = old_fb;
  652. ret = -EINVAL;
  653. goto fail;
  654. }
  655. }
  656. drm_helper_disable_unused_functions(dev);
  657. } else if (fb_changed) {
  658. set->crtc->x = set->x;
  659. set->crtc->y = set->y;
  660. old_fb = set->crtc->fb;
  661. if (set->crtc->fb != set->fb)
  662. set->crtc->fb = set->fb;
  663. ret = crtc_funcs->mode_set_base(set->crtc,
  664. set->x, set->y, old_fb);
  665. if (ret != 0) {
  666. set->crtc->fb = old_fb;
  667. goto fail;
  668. }
  669. }
  670. /*
  671. * crtc set_config helpers implicit set the crtc and all connected
  672. * encoders to DPMS on for a full mode set. But for just an fb update it
  673. * doesn't do that. To not confuse userspace, do an explicit DPMS_ON
  674. * unconditionally. This will also ensure driver internal dpms state is
  675. * consistent again.
  676. */
  677. if (set->crtc->enabled) {
  678. DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  679. for (i = 0; i < set->num_connectors; i++) {
  680. DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  681. drm_get_connector_name(set->connectors[i]));
  682. set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  683. }
  684. }
  685. kfree(save_connectors);
  686. kfree(save_encoders);
  687. kfree(save_crtcs);
  688. return 0;
  689. fail:
  690. /* Restore all previous data. */
  691. count = 0;
  692. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  693. *crtc = save_crtcs[count++];
  694. }
  695. count = 0;
  696. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  697. *encoder = save_encoders[count++];
  698. }
  699. count = 0;
  700. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  701. *connector = save_connectors[count++];
  702. }
  703. /* Try to restore the config */
  704. if (mode_changed &&
  705. !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  706. save_set.y, save_set.fb))
  707. DRM_ERROR("failed to restore config after modeset failure\n");
  708. kfree(save_connectors);
  709. kfree(save_encoders);
  710. kfree(save_crtcs);
  711. return ret;
  712. }
  713. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  714. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  715. {
  716. int dpms = DRM_MODE_DPMS_OFF;
  717. struct drm_connector *connector;
  718. struct drm_device *dev = encoder->dev;
  719. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  720. if (connector->encoder == encoder)
  721. if (connector->dpms < dpms)
  722. dpms = connector->dpms;
  723. return dpms;
  724. }
  725. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  726. {
  727. int dpms = DRM_MODE_DPMS_OFF;
  728. struct drm_connector *connector;
  729. struct drm_device *dev = crtc->dev;
  730. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  731. if (connector->encoder && connector->encoder->crtc == crtc)
  732. if (connector->dpms < dpms)
  733. dpms = connector->dpms;
  734. return dpms;
  735. }
  736. /**
  737. * drm_helper_connector_dpms() - connector dpms helper implementation
  738. * @connector: affected connector
  739. * @mode: DPMS mode
  740. *
  741. * This is the main helper function provided by the crtc helper framework for
  742. * implementing the DPMS connector attribute. It computes the new desired DPMS
  743. * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  744. * callback provided by the driver appropriately.
  745. */
  746. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  747. {
  748. struct drm_encoder *encoder = connector->encoder;
  749. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  750. int old_dpms;
  751. if (mode == connector->dpms)
  752. return;
  753. old_dpms = connector->dpms;
  754. connector->dpms = mode;
  755. /* from off to on, do crtc then encoder */
  756. if (mode < old_dpms) {
  757. if (crtc) {
  758. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  759. if (crtc_funcs->dpms)
  760. (*crtc_funcs->dpms) (crtc,
  761. drm_helper_choose_crtc_dpms(crtc));
  762. }
  763. if (encoder) {
  764. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  765. if (encoder_funcs->dpms)
  766. (*encoder_funcs->dpms) (encoder,
  767. drm_helper_choose_encoder_dpms(encoder));
  768. }
  769. }
  770. /* from on to off, do encoder then crtc */
  771. if (mode > old_dpms) {
  772. if (encoder) {
  773. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  774. if (encoder_funcs->dpms)
  775. (*encoder_funcs->dpms) (encoder,
  776. drm_helper_choose_encoder_dpms(encoder));
  777. }
  778. if (crtc) {
  779. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  780. if (crtc_funcs->dpms)
  781. (*crtc_funcs->dpms) (crtc,
  782. drm_helper_choose_crtc_dpms(crtc));
  783. }
  784. }
  785. return;
  786. }
  787. EXPORT_SYMBOL(drm_helper_connector_dpms);
  788. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  789. struct drm_mode_fb_cmd2 *mode_cmd)
  790. {
  791. int i;
  792. fb->width = mode_cmd->width;
  793. fb->height = mode_cmd->height;
  794. for (i = 0; i < 4; i++) {
  795. fb->pitches[i] = mode_cmd->pitches[i];
  796. fb->offsets[i] = mode_cmd->offsets[i];
  797. }
  798. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  799. &fb->bits_per_pixel);
  800. fb->pixel_format = mode_cmd->pixel_format;
  801. return 0;
  802. }
  803. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  804. int drm_helper_resume_force_mode(struct drm_device *dev)
  805. {
  806. struct drm_crtc *crtc;
  807. struct drm_encoder *encoder;
  808. struct drm_encoder_helper_funcs *encoder_funcs;
  809. struct drm_crtc_helper_funcs *crtc_funcs;
  810. int ret;
  811. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  812. if (!crtc->enabled)
  813. continue;
  814. ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  815. crtc->x, crtc->y, crtc->fb);
  816. if (ret == false)
  817. DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  818. /* Turn off outputs that were already powered off */
  819. if (drm_helper_choose_crtc_dpms(crtc)) {
  820. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  821. if(encoder->crtc != crtc)
  822. continue;
  823. encoder_funcs = encoder->helper_private;
  824. if (encoder_funcs->dpms)
  825. (*encoder_funcs->dpms) (encoder,
  826. drm_helper_choose_encoder_dpms(encoder));
  827. }
  828. crtc_funcs = crtc->helper_private;
  829. if (crtc_funcs->dpms)
  830. (*crtc_funcs->dpms) (crtc,
  831. drm_helper_choose_crtc_dpms(crtc));
  832. }
  833. }
  834. /* disable the unused connectors while restoring the modesetting */
  835. drm_helper_disable_unused_functions(dev);
  836. return 0;
  837. }
  838. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  839. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  840. {
  841. /* send a uevent + call fbdev */
  842. drm_sysfs_hotplug_event(dev);
  843. if (dev->mode_config.funcs->output_poll_changed)
  844. dev->mode_config.funcs->output_poll_changed(dev);
  845. }
  846. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  847. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  848. static void output_poll_execute(struct work_struct *work)
  849. {
  850. struct delayed_work *delayed_work = to_delayed_work(work);
  851. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  852. struct drm_connector *connector;
  853. enum drm_connector_status old_status;
  854. bool repoll = false, changed = false;
  855. if (!drm_kms_helper_poll)
  856. return;
  857. mutex_lock(&dev->mode_config.mutex);
  858. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  859. /* Ignore forced connectors. */
  860. if (connector->force)
  861. continue;
  862. /* Ignore HPD capable connectors and connectors where we don't
  863. * want any hotplug detection at all for polling. */
  864. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  865. continue;
  866. repoll = true;
  867. old_status = connector->status;
  868. /* if we are connected and don't want to poll for disconnect
  869. skip it */
  870. if (old_status == connector_status_connected &&
  871. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  872. continue;
  873. connector->status = connector->funcs->detect(connector, false);
  874. if (old_status != connector->status) {
  875. const char *old, *new;
  876. old = drm_get_connector_status_name(old_status);
  877. new = drm_get_connector_status_name(connector->status);
  878. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  879. "status updated from %s to %s\n",
  880. connector->base.id,
  881. drm_get_connector_name(connector),
  882. old, new);
  883. changed = true;
  884. }
  885. }
  886. mutex_unlock(&dev->mode_config.mutex);
  887. if (changed)
  888. drm_kms_helper_hotplug_event(dev);
  889. if (repoll)
  890. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  891. }
  892. void drm_kms_helper_poll_disable(struct drm_device *dev)
  893. {
  894. if (!dev->mode_config.poll_enabled)
  895. return;
  896. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  897. }
  898. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  899. void drm_kms_helper_poll_enable(struct drm_device *dev)
  900. {
  901. bool poll = false;
  902. struct drm_connector *connector;
  903. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  904. return;
  905. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  906. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  907. DRM_CONNECTOR_POLL_DISCONNECT))
  908. poll = true;
  909. }
  910. if (poll)
  911. schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  912. }
  913. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  914. void drm_kms_helper_poll_init(struct drm_device *dev)
  915. {
  916. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  917. dev->mode_config.poll_enabled = true;
  918. drm_kms_helper_poll_enable(dev);
  919. }
  920. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  921. void drm_kms_helper_poll_fini(struct drm_device *dev)
  922. {
  923. drm_kms_helper_poll_disable(dev);
  924. }
  925. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  926. void drm_helper_hpd_irq_event(struct drm_device *dev)
  927. {
  928. struct drm_connector *connector;
  929. enum drm_connector_status old_status;
  930. bool changed = false;
  931. if (!dev->mode_config.poll_enabled)
  932. return;
  933. mutex_lock(&dev->mode_config.mutex);
  934. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  935. /* Only handle HPD capable connectors. */
  936. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  937. continue;
  938. old_status = connector->status;
  939. connector->status = connector->funcs->detect(connector, false);
  940. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  941. connector->base.id,
  942. drm_get_connector_name(connector),
  943. drm_get_connector_status_name(old_status),
  944. drm_get_connector_status_name(connector->status));
  945. if (old_status != connector->status)
  946. changed = true;
  947. }
  948. mutex_unlock(&dev->mode_config.mutex);
  949. if (changed)
  950. drm_kms_helper_hotplug_event(dev);
  951. }
  952. EXPORT_SYMBOL(drm_helper_hpd_irq_event);