drm_crtc_helper.c 33 KB

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