nouveau_dp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2009 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <drm/drm_dp_helper.h>
  25. #include "nouveau_drv.h"
  26. #include "nouveau_connector.h"
  27. #include "nouveau_encoder.h"
  28. #include "nouveau_crtc.h"
  29. #include <nvif/class.h>
  30. #include <nvif/cl5070.h>
  31. MODULE_PARM_DESC(mst, "Enable DisplayPort multi-stream (default: enabled)");
  32. static int nouveau_mst = 1;
  33. module_param_named(mst, nouveau_mst, int, 0400);
  34. static bool
  35. nouveau_dp_has_sink_count(struct drm_connector *connector,
  36. struct nouveau_encoder *outp)
  37. {
  38. return drm_dp_read_sink_count_cap(connector, outp->dp.dpcd, &outp->dp.desc);
  39. }
  40. static enum drm_connector_status
  41. nouveau_dp_probe_dpcd(struct nouveau_connector *nv_connector,
  42. struct nouveau_encoder *outp)
  43. {
  44. struct drm_connector *connector = &nv_connector->base;
  45. struct drm_dp_aux *aux = &nv_connector->aux;
  46. struct nv50_mstm *mstm = NULL;
  47. enum drm_connector_status status = connector_status_disconnected;
  48. int ret;
  49. u8 *dpcd = outp->dp.dpcd;
  50. ret = drm_dp_read_dpcd_caps(aux, dpcd);
  51. if (ret < 0)
  52. goto out;
  53. ret = drm_dp_read_desc(aux, &outp->dp.desc, drm_dp_is_branch(dpcd));
  54. if (ret < 0)
  55. goto out;
  56. if (nouveau_mst) {
  57. mstm = outp->dp.mstm;
  58. if (mstm)
  59. mstm->can_mst = drm_dp_read_mst_cap(aux, dpcd);
  60. }
  61. if (nouveau_dp_has_sink_count(connector, outp)) {
  62. ret = drm_dp_read_sink_count(aux);
  63. if (ret < 0)
  64. goto out;
  65. outp->dp.sink_count = ret;
  66. /*
  67. * Dongle connected, but no display. Don't bother reading
  68. * downstream port info
  69. */
  70. if (!outp->dp.sink_count)
  71. return connector_status_disconnected;
  72. }
  73. ret = drm_dp_read_downstream_info(aux, dpcd,
  74. outp->dp.downstream_ports);
  75. if (ret < 0)
  76. goto out;
  77. status = connector_status_connected;
  78. out:
  79. if (status != connector_status_connected) {
  80. /* Clear any cached info */
  81. outp->dp.sink_count = 0;
  82. }
  83. return status;
  84. }
  85. int
  86. nouveau_dp_detect(struct nouveau_connector *nv_connector,
  87. struct nouveau_encoder *nv_encoder)
  88. {
  89. struct drm_device *dev = nv_encoder->base.base.dev;
  90. struct nouveau_drm *drm = nouveau_drm(dev);
  91. struct drm_connector *connector = &nv_connector->base;
  92. struct nv50_mstm *mstm = nv_encoder->dp.mstm;
  93. enum drm_connector_status status;
  94. u8 *dpcd = nv_encoder->dp.dpcd;
  95. int ret = NOUVEAU_DP_NONE;
  96. /* If we've already read the DPCD on an eDP device, we don't need to
  97. * reread it as it won't change
  98. */
  99. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP &&
  100. dpcd[DP_DPCD_REV] != 0)
  101. return NOUVEAU_DP_SST;
  102. mutex_lock(&nv_encoder->dp.hpd_irq_lock);
  103. if (mstm) {
  104. /* If we're not ready to handle MST state changes yet, just
  105. * report the last status of the connector. We'll reprobe it
  106. * once we've resumed.
  107. */
  108. if (mstm->suspended) {
  109. if (mstm->is_mst)
  110. ret = NOUVEAU_DP_MST;
  111. else if (connector->status ==
  112. connector_status_connected)
  113. ret = NOUVEAU_DP_SST;
  114. goto out;
  115. }
  116. }
  117. status = nouveau_dp_probe_dpcd(nv_connector, nv_encoder);
  118. if (status == connector_status_disconnected)
  119. goto out;
  120. /* If we're in MST mode, we're done here */
  121. if (mstm && mstm->can_mst && mstm->is_mst) {
  122. ret = NOUVEAU_DP_MST;
  123. goto out;
  124. }
  125. nv_encoder->dp.link_bw = 27000 * dpcd[DP_MAX_LINK_RATE];
  126. nv_encoder->dp.link_nr =
  127. dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
  128. NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
  129. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw,
  130. dpcd[DP_DPCD_REV]);
  131. NV_DEBUG(drm, "encoder: %dx%d\n",
  132. nv_encoder->dcb->dpconf.link_nr,
  133. nv_encoder->dcb->dpconf.link_bw);
  134. if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
  135. nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
  136. if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
  137. nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
  138. NV_DEBUG(drm, "maximum: %dx%d\n",
  139. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
  140. if (mstm && mstm->can_mst) {
  141. ret = nv50_mstm_detect(nv_encoder);
  142. if (ret == 1) {
  143. ret = NOUVEAU_DP_MST;
  144. goto out;
  145. } else if (ret != 0) {
  146. goto out;
  147. }
  148. }
  149. ret = NOUVEAU_DP_SST;
  150. out:
  151. if (mstm && !mstm->suspended && ret != NOUVEAU_DP_MST)
  152. nv50_mstm_remove(mstm);
  153. mutex_unlock(&nv_encoder->dp.hpd_irq_lock);
  154. return ret;
  155. }
  156. void nouveau_dp_irq(struct nouveau_drm *drm,
  157. struct nouveau_connector *nv_connector)
  158. {
  159. struct drm_connector *connector = &nv_connector->base;
  160. struct nouveau_encoder *outp = find_encoder(connector, DCB_OUTPUT_DP);
  161. struct nv50_mstm *mstm;
  162. int ret;
  163. bool send_hpd = false;
  164. if (!outp)
  165. return;
  166. mstm = outp->dp.mstm;
  167. NV_DEBUG(drm, "service %s\n", connector->name);
  168. mutex_lock(&outp->dp.hpd_irq_lock);
  169. if (mstm && mstm->is_mst) {
  170. if (!nv50_mstm_service(drm, nv_connector, mstm))
  171. send_hpd = true;
  172. } else {
  173. drm_dp_cec_irq(&nv_connector->aux);
  174. if (nouveau_dp_has_sink_count(connector, outp)) {
  175. ret = drm_dp_read_sink_count(&nv_connector->aux);
  176. if (ret != outp->dp.sink_count)
  177. send_hpd = true;
  178. if (ret >= 0)
  179. outp->dp.sink_count = ret;
  180. }
  181. }
  182. mutex_unlock(&outp->dp.hpd_irq_lock);
  183. if (send_hpd)
  184. nouveau_connector_hpd(connector);
  185. }
  186. /* TODO:
  187. * - Use the minimum possible BPC here, once we add support for the max bpc
  188. * property.
  189. * - Validate against the DP caps advertised by the GPU (we don't check these
  190. * yet)
  191. */
  192. enum drm_mode_status
  193. nv50_dp_mode_valid(struct drm_connector *connector,
  194. struct nouveau_encoder *outp,
  195. const struct drm_display_mode *mode,
  196. unsigned *out_clock)
  197. {
  198. const unsigned int min_clock = 25000;
  199. unsigned int max_rate, mode_rate, ds_max_dotclock, clock = mode->clock;
  200. const u8 bpp = connector->display_info.bpc * 3;
  201. if (mode->flags & DRM_MODE_FLAG_INTERLACE && !outp->caps.dp_interlace)
  202. return MODE_NO_INTERLACE;
  203. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
  204. clock *= 2;
  205. max_rate = outp->dp.link_nr * outp->dp.link_bw;
  206. mode_rate = DIV_ROUND_UP(clock * bpp, 8);
  207. if (mode_rate > max_rate)
  208. return MODE_CLOCK_HIGH;
  209. ds_max_dotclock = drm_dp_downstream_max_dotclock(outp->dp.dpcd, outp->dp.downstream_ports);
  210. if (ds_max_dotclock && clock > ds_max_dotclock)
  211. return MODE_CLOCK_HIGH;
  212. if (clock < min_clock)
  213. return MODE_CLOCK_LOW;
  214. if (out_clock)
  215. *out_clock = clock;
  216. return MODE_OK;
  217. }