nv50_sor.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2008 Maarten Maathuis.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "drmP.h"
  27. #include "drm_crtc_helper.h"
  28. #define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
  29. #include "nouveau_reg.h"
  30. #include "nouveau_drv.h"
  31. #include "nouveau_dma.h"
  32. #include "nouveau_encoder.h"
  33. #include "nouveau_connector.h"
  34. #include "nouveau_crtc.h"
  35. #include "nv50_display.h"
  36. static void
  37. nv50_sor_disconnect(struct nouveau_encoder *nv_encoder)
  38. {
  39. struct drm_device *dev = to_drm_encoder(nv_encoder)->dev;
  40. struct drm_nouveau_private *dev_priv = dev->dev_private;
  41. struct nouveau_channel *evo = dev_priv->evo;
  42. int ret;
  43. NV_DEBUG_KMS(dev, "Disconnecting SOR %d\n", nv_encoder->or);
  44. ret = RING_SPACE(evo, 2);
  45. if (ret) {
  46. NV_ERROR(dev, "no space while disconnecting SOR\n");
  47. return;
  48. }
  49. BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  50. OUT_RING(evo, 0);
  51. }
  52. static void
  53. nv50_sor_dp_link_train(struct drm_encoder *encoder)
  54. {
  55. struct drm_device *dev = encoder->dev;
  56. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  57. struct bit_displayport_encoder_table *dpe;
  58. int dpe_headerlen;
  59. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  60. if (!dpe) {
  61. NV_ERROR(dev, "SOR-%d: no DP encoder table!\n", nv_encoder->or);
  62. return;
  63. }
  64. if (dpe->script0) {
  65. NV_DEBUG_KMS(dev, "SOR-%d: running DP script 0\n", nv_encoder->or);
  66. nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script0),
  67. nv_encoder->dcb);
  68. }
  69. if (!nouveau_dp_link_train(encoder))
  70. NV_ERROR(dev, "SOR-%d: link training failed\n", nv_encoder->or);
  71. if (dpe->script1) {
  72. NV_DEBUG_KMS(dev, "SOR-%d: running DP script 1\n", nv_encoder->or);
  73. nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script1),
  74. nv_encoder->dcb);
  75. }
  76. }
  77. static void
  78. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  79. {
  80. struct drm_device *dev = encoder->dev;
  81. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  82. uint32_t val;
  83. int or = nv_encoder->or;
  84. NV_DEBUG_KMS(dev, "or %d mode %d\n", or, mode);
  85. /* wait for it to be done */
  86. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_CTRL(or),
  87. NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING, 0)) {
  88. NV_ERROR(dev, "timeout: SOR_DPMS_CTRL_PENDING(%d) == 0\n", or);
  89. NV_ERROR(dev, "SOR_DPMS_CTRL(%d) = 0x%08x\n", or,
  90. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or)));
  91. }
  92. val = nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or));
  93. if (mode == DRM_MODE_DPMS_ON)
  94. val |= NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
  95. else
  96. val &= ~NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
  97. nv_wr32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or), val |
  98. NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING);
  99. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_STATE(or),
  100. NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
  101. NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", or);
  102. NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", or,
  103. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(or)));
  104. }
  105. if (nv_encoder->dcb->type == OUTPUT_DP && mode == DRM_MODE_DPMS_ON)
  106. nv50_sor_dp_link_train(encoder);
  107. }
  108. static void
  109. nv50_sor_save(struct drm_encoder *encoder)
  110. {
  111. NV_ERROR(encoder->dev, "!!\n");
  112. }
  113. static void
  114. nv50_sor_restore(struct drm_encoder *encoder)
  115. {
  116. NV_ERROR(encoder->dev, "!!\n");
  117. }
  118. static bool
  119. nv50_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  120. struct drm_display_mode *adjusted_mode)
  121. {
  122. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  123. struct nouveau_connector *connector;
  124. NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
  125. connector = nouveau_encoder_connector_get(nv_encoder);
  126. if (!connector) {
  127. NV_ERROR(encoder->dev, "Encoder has no connector\n");
  128. return false;
  129. }
  130. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  131. connector->native_mode) {
  132. int id = adjusted_mode->base.id;
  133. *adjusted_mode = *connector->native_mode;
  134. adjusted_mode->base.id = id;
  135. }
  136. return true;
  137. }
  138. static void
  139. nv50_sor_prepare(struct drm_encoder *encoder)
  140. {
  141. }
  142. static void
  143. nv50_sor_commit(struct drm_encoder *encoder)
  144. {
  145. }
  146. static void
  147. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  148. struct drm_display_mode *adjusted_mode)
  149. {
  150. struct drm_nouveau_private *dev_priv = encoder->dev->dev_private;
  151. struct nouveau_channel *evo = dev_priv->evo;
  152. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  153. struct drm_device *dev = encoder->dev;
  154. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  155. uint32_t mode_ctl = 0;
  156. int ret;
  157. NV_DEBUG_KMS(dev, "or %d\n", nv_encoder->or);
  158. nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  159. switch (nv_encoder->dcb->type) {
  160. case OUTPUT_TMDS:
  161. if (nv_encoder->dcb->sorconf.link & 1) {
  162. if (adjusted_mode->clock < 165000)
  163. mode_ctl = 0x0100;
  164. else
  165. mode_ctl = 0x0500;
  166. } else
  167. mode_ctl = 0x0200;
  168. break;
  169. case OUTPUT_DP:
  170. mode_ctl |= 0x00050000;
  171. if (nv_encoder->dcb->sorconf.link & 1)
  172. mode_ctl |= 0x00000800;
  173. else
  174. mode_ctl |= 0x00000900;
  175. break;
  176. default:
  177. break;
  178. }
  179. if (crtc->index == 1)
  180. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
  181. else
  182. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
  183. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  184. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
  185. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  186. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
  187. ret = RING_SPACE(evo, 2);
  188. if (ret) {
  189. NV_ERROR(dev, "no space while connecting SOR\n");
  190. return;
  191. }
  192. BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  193. OUT_RING(evo, mode_ctl);
  194. }
  195. static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
  196. .dpms = nv50_sor_dpms,
  197. .save = nv50_sor_save,
  198. .restore = nv50_sor_restore,
  199. .mode_fixup = nv50_sor_mode_fixup,
  200. .prepare = nv50_sor_prepare,
  201. .commit = nv50_sor_commit,
  202. .mode_set = nv50_sor_mode_set,
  203. .detect = NULL
  204. };
  205. static void
  206. nv50_sor_destroy(struct drm_encoder *encoder)
  207. {
  208. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  209. if (!encoder)
  210. return;
  211. NV_DEBUG_KMS(encoder->dev, "\n");
  212. drm_encoder_cleanup(encoder);
  213. kfree(nv_encoder);
  214. }
  215. static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
  216. .destroy = nv50_sor_destroy,
  217. };
  218. int
  219. nv50_sor_create(struct drm_device *dev, struct dcb_entry *entry)
  220. {
  221. struct nouveau_encoder *nv_encoder = NULL;
  222. struct drm_encoder *encoder;
  223. bool dum;
  224. int type;
  225. NV_DEBUG_KMS(dev, "\n");
  226. switch (entry->type) {
  227. case OUTPUT_TMDS:
  228. NV_INFO(dev, "Detected a TMDS output\n");
  229. type = DRM_MODE_ENCODER_TMDS;
  230. break;
  231. case OUTPUT_LVDS:
  232. NV_INFO(dev, "Detected a LVDS output\n");
  233. type = DRM_MODE_ENCODER_LVDS;
  234. if (nouveau_bios_parse_lvds_table(dev, 0, &dum, &dum)) {
  235. NV_ERROR(dev, "Failed parsing LVDS table\n");
  236. return -EINVAL;
  237. }
  238. break;
  239. case OUTPUT_DP:
  240. NV_INFO(dev, "Detected a DP output\n");
  241. type = DRM_MODE_ENCODER_TMDS;
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  247. if (!nv_encoder)
  248. return -ENOMEM;
  249. encoder = to_drm_encoder(nv_encoder);
  250. nv_encoder->dcb = entry;
  251. nv_encoder->or = ffs(entry->or) - 1;
  252. nv_encoder->disconnect = nv50_sor_disconnect;
  253. drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
  254. drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
  255. encoder->possible_crtcs = entry->heads;
  256. encoder->possible_clones = 0;
  257. return 0;
  258. }