nv50_sor.c 8.5 KB

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