nv50_sor.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. struct drm_encoder *enc;
  83. uint32_t val;
  84. int or = nv_encoder->or;
  85. NV_DEBUG_KMS(dev, "or %d mode %d\n", or, mode);
  86. nv_encoder->last_dpms = mode;
  87. list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
  88. struct nouveau_encoder *nvenc = nouveau_encoder(enc);
  89. if (nvenc == nv_encoder ||
  90. nvenc->dcb->or != nv_encoder->dcb->or)
  91. continue;
  92. if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
  93. return;
  94. }
  95. /* wait for it to be done */
  96. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_CTRL(or),
  97. NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING, 0)) {
  98. NV_ERROR(dev, "timeout: SOR_DPMS_CTRL_PENDING(%d) == 0\n", or);
  99. NV_ERROR(dev, "SOR_DPMS_CTRL(%d) = 0x%08x\n", or,
  100. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or)));
  101. }
  102. val = nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or));
  103. if (mode == DRM_MODE_DPMS_ON)
  104. val |= NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
  105. else
  106. val &= ~NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
  107. nv_wr32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or), val |
  108. NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING);
  109. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_STATE(or),
  110. NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
  111. NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", or);
  112. NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", or,
  113. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(or)));
  114. }
  115. if (nv_encoder->dcb->type == OUTPUT_DP && mode == DRM_MODE_DPMS_ON)
  116. nv50_sor_dp_link_train(encoder);
  117. }
  118. static void
  119. nv50_sor_save(struct drm_encoder *encoder)
  120. {
  121. NV_ERROR(encoder->dev, "!!\n");
  122. }
  123. static void
  124. nv50_sor_restore(struct drm_encoder *encoder)
  125. {
  126. NV_ERROR(encoder->dev, "!!\n");
  127. }
  128. static bool
  129. nv50_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  130. struct drm_display_mode *adjusted_mode)
  131. {
  132. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  133. struct nouveau_connector *connector;
  134. NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
  135. connector = nouveau_encoder_connector_get(nv_encoder);
  136. if (!connector) {
  137. NV_ERROR(encoder->dev, "Encoder has no connector\n");
  138. return false;
  139. }
  140. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  141. connector->native_mode) {
  142. int id = adjusted_mode->base.id;
  143. *adjusted_mode = *connector->native_mode;
  144. adjusted_mode->base.id = id;
  145. }
  146. return true;
  147. }
  148. static void
  149. nv50_sor_prepare(struct drm_encoder *encoder)
  150. {
  151. }
  152. static void
  153. nv50_sor_commit(struct drm_encoder *encoder)
  154. {
  155. }
  156. static void
  157. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  158. struct drm_display_mode *adjusted_mode)
  159. {
  160. struct drm_nouveau_private *dev_priv = encoder->dev->dev_private;
  161. struct nouveau_channel *evo = dev_priv->evo;
  162. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  163. struct drm_device *dev = encoder->dev;
  164. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  165. uint32_t mode_ctl = 0;
  166. int ret;
  167. NV_DEBUG_KMS(dev, "or %d\n", nv_encoder->or);
  168. nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  169. switch (nv_encoder->dcb->type) {
  170. case OUTPUT_TMDS:
  171. if (nv_encoder->dcb->sorconf.link & 1) {
  172. if (adjusted_mode->clock < 165000)
  173. mode_ctl = 0x0100;
  174. else
  175. mode_ctl = 0x0500;
  176. } else
  177. mode_ctl = 0x0200;
  178. break;
  179. case OUTPUT_DP:
  180. mode_ctl |= 0x00050000;
  181. if (nv_encoder->dcb->sorconf.link & 1)
  182. mode_ctl |= 0x00000800;
  183. else
  184. mode_ctl |= 0x00000900;
  185. break;
  186. default:
  187. break;
  188. }
  189. if (crtc->index == 1)
  190. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
  191. else
  192. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
  193. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  194. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
  195. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  196. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
  197. ret = RING_SPACE(evo, 2);
  198. if (ret) {
  199. NV_ERROR(dev, "no space while connecting SOR\n");
  200. return;
  201. }
  202. BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  203. OUT_RING(evo, mode_ctl);
  204. }
  205. static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
  206. .dpms = nv50_sor_dpms,
  207. .save = nv50_sor_save,
  208. .restore = nv50_sor_restore,
  209. .mode_fixup = nv50_sor_mode_fixup,
  210. .prepare = nv50_sor_prepare,
  211. .commit = nv50_sor_commit,
  212. .mode_set = nv50_sor_mode_set,
  213. .detect = NULL
  214. };
  215. static void
  216. nv50_sor_destroy(struct drm_encoder *encoder)
  217. {
  218. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  219. if (!encoder)
  220. return;
  221. NV_DEBUG_KMS(encoder->dev, "\n");
  222. drm_encoder_cleanup(encoder);
  223. kfree(nv_encoder);
  224. }
  225. static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
  226. .destroy = nv50_sor_destroy,
  227. };
  228. int
  229. nv50_sor_create(struct drm_device *dev, struct dcb_entry *entry)
  230. {
  231. struct nouveau_encoder *nv_encoder = NULL;
  232. struct drm_encoder *encoder;
  233. bool dum;
  234. int type;
  235. NV_DEBUG_KMS(dev, "\n");
  236. switch (entry->type) {
  237. case OUTPUT_TMDS:
  238. NV_INFO(dev, "Detected a TMDS output\n");
  239. type = DRM_MODE_ENCODER_TMDS;
  240. break;
  241. case OUTPUT_LVDS:
  242. NV_INFO(dev, "Detected a LVDS output\n");
  243. type = DRM_MODE_ENCODER_LVDS;
  244. if (nouveau_bios_parse_lvds_table(dev, 0, &dum, &dum)) {
  245. NV_ERROR(dev, "Failed parsing LVDS table\n");
  246. return -EINVAL;
  247. }
  248. break;
  249. case OUTPUT_DP:
  250. NV_INFO(dev, "Detected a DP output\n");
  251. type = DRM_MODE_ENCODER_TMDS;
  252. break;
  253. default:
  254. return -EINVAL;
  255. }
  256. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  257. if (!nv_encoder)
  258. return -ENOMEM;
  259. encoder = to_drm_encoder(nv_encoder);
  260. nv_encoder->dcb = entry;
  261. nv_encoder->or = ffs(entry->or) - 1;
  262. nv_encoder->disconnect = nv50_sor_disconnect;
  263. drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
  264. drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
  265. encoder->possible_crtcs = entry->heads;
  266. encoder->possible_clones = 0;
  267. return 0;
  268. }