nouveau_dp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/display/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, hpd;
  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. /* Check status of HPD pin before attempting an AUX transaction that
  118. * would result in a number of (futile) retries on a connector which
  119. * has no display plugged.
  120. *
  121. * TODO: look into checking this before probing I2C to detect DVI/HDMI
  122. */
  123. hpd = nvif_conn_hpd_status(&nv_connector->conn);
  124. if (hpd == NVIF_CONN_HPD_STATUS_NOT_PRESENT)
  125. goto out;
  126. status = nouveau_dp_probe_dpcd(nv_connector, nv_encoder);
  127. if (status == connector_status_disconnected)
  128. goto out;
  129. /* If we're in MST mode, we're done here */
  130. if (mstm && mstm->can_mst && mstm->is_mst) {
  131. ret = NOUVEAU_DP_MST;
  132. goto out;
  133. }
  134. nv_encoder->dp.link_bw = 27000 * dpcd[DP_MAX_LINK_RATE];
  135. nv_encoder->dp.link_nr =
  136. dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
  137. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP && dpcd[DP_DPCD_REV] >= 0x13) {
  138. struct drm_dp_aux *aux = &nv_connector->aux;
  139. int ret, i;
  140. u8 sink_rates[16];
  141. ret = drm_dp_dpcd_read(aux, DP_SUPPORTED_LINK_RATES, sink_rates, sizeof(sink_rates));
  142. if (ret == sizeof(sink_rates)) {
  143. for (i = 0; i < ARRAY_SIZE(sink_rates); i += 2) {
  144. int val = ((sink_rates[i + 1] << 8) | sink_rates[i]) * 200 / 10;
  145. if (val && (i == 0 || val > nv_encoder->dp.link_bw))
  146. nv_encoder->dp.link_bw = val;
  147. }
  148. }
  149. }
  150. NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
  151. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw,
  152. dpcd[DP_DPCD_REV]);
  153. NV_DEBUG(drm, "encoder: %dx%d\n",
  154. nv_encoder->dcb->dpconf.link_nr,
  155. nv_encoder->dcb->dpconf.link_bw);
  156. if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
  157. nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
  158. if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
  159. nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
  160. NV_DEBUG(drm, "maximum: %dx%d\n",
  161. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
  162. if (mstm && mstm->can_mst) {
  163. ret = nv50_mstm_detect(nv_encoder);
  164. if (ret == 1) {
  165. ret = NOUVEAU_DP_MST;
  166. goto out;
  167. } else if (ret != 0) {
  168. goto out;
  169. }
  170. }
  171. ret = NOUVEAU_DP_SST;
  172. out:
  173. if (mstm && !mstm->suspended && ret != NOUVEAU_DP_MST)
  174. nv50_mstm_remove(mstm);
  175. mutex_unlock(&nv_encoder->dp.hpd_irq_lock);
  176. return ret;
  177. }
  178. void nouveau_dp_irq(struct nouveau_drm *drm,
  179. struct nouveau_connector *nv_connector)
  180. {
  181. struct drm_connector *connector = &nv_connector->base;
  182. struct nouveau_encoder *outp = find_encoder(connector, DCB_OUTPUT_DP);
  183. struct nv50_mstm *mstm;
  184. int ret;
  185. bool send_hpd = false;
  186. if (!outp)
  187. return;
  188. mstm = outp->dp.mstm;
  189. NV_DEBUG(drm, "service %s\n", connector->name);
  190. mutex_lock(&outp->dp.hpd_irq_lock);
  191. if (mstm && mstm->is_mst) {
  192. if (!nv50_mstm_service(drm, nv_connector, mstm))
  193. send_hpd = true;
  194. } else {
  195. drm_dp_cec_irq(&nv_connector->aux);
  196. if (nouveau_dp_has_sink_count(connector, outp)) {
  197. ret = drm_dp_read_sink_count(&nv_connector->aux);
  198. if (ret != outp->dp.sink_count)
  199. send_hpd = true;
  200. if (ret >= 0)
  201. outp->dp.sink_count = ret;
  202. }
  203. }
  204. mutex_unlock(&outp->dp.hpd_irq_lock);
  205. if (send_hpd)
  206. nouveau_connector_hpd(connector);
  207. }
  208. /* TODO:
  209. * - Validate against the DP caps advertised by the GPU (we don't check these
  210. * yet)
  211. */
  212. enum drm_mode_status
  213. nv50_dp_mode_valid(struct drm_connector *connector,
  214. struct nouveau_encoder *outp,
  215. const struct drm_display_mode *mode,
  216. unsigned *out_clock)
  217. {
  218. const unsigned int min_clock = 25000;
  219. unsigned int max_rate, mode_rate, ds_max_dotclock, clock = mode->clock;
  220. /* Check with the minmum bpc always, so we can advertise better modes.
  221. * In particlar not doing this causes modes to be dropped on HDR
  222. * displays as we might check with a bpc of 16 even.
  223. */
  224. const u8 bpp = 6 * 3;
  225. if (mode->flags & DRM_MODE_FLAG_INTERLACE && !outp->caps.dp_interlace)
  226. return MODE_NO_INTERLACE;
  227. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
  228. clock *= 2;
  229. max_rate = outp->dp.link_nr * outp->dp.link_bw;
  230. mode_rate = DIV_ROUND_UP(clock * bpp, 8);
  231. if (mode_rate > max_rate)
  232. return MODE_CLOCK_HIGH;
  233. ds_max_dotclock = drm_dp_downstream_max_dotclock(outp->dp.dpcd, outp->dp.downstream_ports);
  234. if (ds_max_dotclock && clock > ds_max_dotclock)
  235. return MODE_CLOCK_HIGH;
  236. if (clock < min_clock)
  237. return MODE_CLOCK_LOW;
  238. if (out_clock)
  239. *out_clock = clock;
  240. return MODE_OK;
  241. }