nv50_sor.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 drm_encoder *encoder)
  38. {
  39. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  40. struct drm_device *dev = encoder->dev;
  41. struct drm_nouveau_private *dev_priv = dev->dev_private;
  42. struct nouveau_channel *evo = dev_priv->evo;
  43. int ret;
  44. if (!nv_encoder->crtc)
  45. return;
  46. nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
  47. NV_DEBUG_KMS(dev, "Disconnecting SOR %d\n", nv_encoder->or);
  48. ret = RING_SPACE(evo, 4);
  49. if (ret) {
  50. NV_ERROR(dev, "no space while disconnecting SOR\n");
  51. return;
  52. }
  53. BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  54. OUT_RING (evo, 0);
  55. BEGIN_RING(evo, 0, NV50_EVO_UPDATE, 1);
  56. OUT_RING (evo, 0);
  57. nv_encoder->crtc = NULL;
  58. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  59. }
  60. static void
  61. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  62. {
  63. struct drm_device *dev = encoder->dev;
  64. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  65. struct drm_encoder *enc;
  66. uint32_t val;
  67. int or = nv_encoder->or;
  68. NV_DEBUG_KMS(dev, "or %d type %d mode %d\n", or, nv_encoder->dcb->type, mode);
  69. nv_encoder->last_dpms = mode;
  70. list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
  71. struct nouveau_encoder *nvenc = nouveau_encoder(enc);
  72. if (nvenc == nv_encoder ||
  73. (nvenc->dcb->type != OUTPUT_TMDS &&
  74. nvenc->dcb->type != OUTPUT_LVDS &&
  75. nvenc->dcb->type != OUTPUT_DP) ||
  76. nvenc->dcb->or != nv_encoder->dcb->or)
  77. continue;
  78. if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
  79. return;
  80. }
  81. /* wait for it to be done */
  82. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_CTRL(or),
  83. NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING, 0)) {
  84. NV_ERROR(dev, "timeout: SOR_DPMS_CTRL_PENDING(%d) == 0\n", or);
  85. NV_ERROR(dev, "SOR_DPMS_CTRL(%d) = 0x%08x\n", or,
  86. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or)));
  87. }
  88. val = nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or));
  89. if (mode == DRM_MODE_DPMS_ON)
  90. val |= NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
  91. else
  92. val &= ~NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
  93. nv_wr32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or), val |
  94. NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING);
  95. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_STATE(or),
  96. NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
  97. NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", or);
  98. NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", or,
  99. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(or)));
  100. }
  101. if (nv_encoder->dcb->type == OUTPUT_DP && mode == DRM_MODE_DPMS_ON)
  102. nouveau_dp_link_train(encoder);
  103. }
  104. static void
  105. nv50_sor_save(struct drm_encoder *encoder)
  106. {
  107. NV_ERROR(encoder->dev, "!!\n");
  108. }
  109. static void
  110. nv50_sor_restore(struct drm_encoder *encoder)
  111. {
  112. NV_ERROR(encoder->dev, "!!\n");
  113. }
  114. static bool
  115. nv50_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  116. struct drm_display_mode *adjusted_mode)
  117. {
  118. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  119. struct nouveau_connector *connector;
  120. NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
  121. connector = nouveau_encoder_connector_get(nv_encoder);
  122. if (!connector) {
  123. NV_ERROR(encoder->dev, "Encoder has no connector\n");
  124. return false;
  125. }
  126. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  127. connector->native_mode) {
  128. int id = adjusted_mode->base.id;
  129. *adjusted_mode = *connector->native_mode;
  130. adjusted_mode->base.id = id;
  131. }
  132. return true;
  133. }
  134. static void
  135. nv50_sor_prepare(struct drm_encoder *encoder)
  136. {
  137. }
  138. static void
  139. nv50_sor_commit(struct drm_encoder *encoder)
  140. {
  141. }
  142. static void
  143. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  144. struct drm_display_mode *adjusted_mode)
  145. {
  146. struct drm_nouveau_private *dev_priv = encoder->dev->dev_private;
  147. struct nouveau_channel *evo = dev_priv->evo;
  148. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  149. struct drm_device *dev = encoder->dev;
  150. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  151. uint32_t mode_ctl = 0;
  152. int ret;
  153. NV_DEBUG_KMS(dev, "or %d type %d -> crtc %d\n",
  154. nv_encoder->or, nv_encoder->dcb->type, crtc->index);
  155. nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  156. switch (nv_encoder->dcb->type) {
  157. case OUTPUT_TMDS:
  158. if (nv_encoder->dcb->sorconf.link & 1) {
  159. if (adjusted_mode->clock < 165000)
  160. mode_ctl = 0x0100;
  161. else
  162. mode_ctl = 0x0500;
  163. } else
  164. mode_ctl = 0x0200;
  165. break;
  166. case OUTPUT_DP:
  167. mode_ctl |= (nv_encoder->dp.mc_unknown << 16);
  168. if (nv_encoder->dcb->sorconf.link & 1)
  169. mode_ctl |= 0x00000800;
  170. else
  171. mode_ctl |= 0x00000900;
  172. break;
  173. default:
  174. break;
  175. }
  176. if (crtc->index == 1)
  177. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
  178. else
  179. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
  180. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  181. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
  182. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  183. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
  184. ret = RING_SPACE(evo, 2);
  185. if (ret) {
  186. NV_ERROR(dev, "no space while connecting SOR\n");
  187. return;
  188. }
  189. BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  190. OUT_RING(evo, mode_ctl);
  191. nv_encoder->crtc = encoder->crtc;
  192. }
  193. static struct drm_crtc *
  194. nv50_sor_crtc_get(struct drm_encoder *encoder)
  195. {
  196. return nouveau_encoder(encoder)->crtc;
  197. }
  198. static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
  199. .dpms = nv50_sor_dpms,
  200. .save = nv50_sor_save,
  201. .restore = nv50_sor_restore,
  202. .mode_fixup = nv50_sor_mode_fixup,
  203. .prepare = nv50_sor_prepare,
  204. .commit = nv50_sor_commit,
  205. .mode_set = nv50_sor_mode_set,
  206. .get_crtc = nv50_sor_crtc_get,
  207. .detect = NULL,
  208. .disable = nv50_sor_disconnect
  209. };
  210. static void
  211. nv50_sor_destroy(struct drm_encoder *encoder)
  212. {
  213. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  214. if (!encoder)
  215. return;
  216. NV_DEBUG_KMS(encoder->dev, "\n");
  217. drm_encoder_cleanup(encoder);
  218. kfree(nv_encoder);
  219. }
  220. static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
  221. .destroy = nv50_sor_destroy,
  222. };
  223. int
  224. nv50_sor_create(struct drm_connector *connector, struct dcb_entry *entry)
  225. {
  226. struct nouveau_encoder *nv_encoder = NULL;
  227. struct drm_device *dev = connector->dev;
  228. struct drm_encoder *encoder;
  229. int type;
  230. NV_DEBUG_KMS(dev, "\n");
  231. switch (entry->type) {
  232. case OUTPUT_TMDS:
  233. case OUTPUT_DP:
  234. type = DRM_MODE_ENCODER_TMDS;
  235. break;
  236. case OUTPUT_LVDS:
  237. type = DRM_MODE_ENCODER_LVDS;
  238. break;
  239. default:
  240. return -EINVAL;
  241. }
  242. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  243. if (!nv_encoder)
  244. return -ENOMEM;
  245. encoder = to_drm_encoder(nv_encoder);
  246. nv_encoder->dcb = entry;
  247. nv_encoder->or = ffs(entry->or) - 1;
  248. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  249. drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
  250. drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
  251. encoder->possible_crtcs = entry->heads;
  252. encoder->possible_clones = 0;
  253. if (nv_encoder->dcb->type == OUTPUT_DP) {
  254. int or = nv_encoder->or, link = !(entry->dpconf.sor.link & 1);
  255. uint32_t tmp;
  256. tmp = nv_rd32(dev, 0x61c700 + (or * 0x800));
  257. switch ((tmp & 0x00000f00) >> 8) {
  258. case 8:
  259. case 9:
  260. nv_encoder->dp.mc_unknown = (tmp & 0x000f0000) >> 16;
  261. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  262. nv_encoder->dp.unk0 = tmp & 0x000001fc;
  263. tmp = nv_rd32(dev, NV50_SOR_DP_UNK128(or, link));
  264. nv_encoder->dp.unk1 = tmp & 0x010f7f3f;
  265. break;
  266. default:
  267. break;
  268. }
  269. if (!nv_encoder->dp.mc_unknown)
  270. nv_encoder->dp.mc_unknown = 5;
  271. }
  272. drm_mode_connector_attach_encoder(connector, encoder);
  273. return 0;
  274. }