nv50_sor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <drm/drmP.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
  29. #include "nouveau_reg.h"
  30. #include "nouveau_drm.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. #include <core/class.h>
  37. #include <subdev/timer.h>
  38. static void
  39. nv50_sor_dp_train_set(struct drm_device *dev, struct dcb_output *dcb, u8 pattern)
  40. {
  41. struct nv50_display *disp = nv50_display(dev);
  42. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  43. const u32 moff = (link << 2) | or;
  44. nv_call(disp->core, NV94_DISP_SOR_DP_TRAIN + moff, pattern);
  45. }
  46. static void
  47. nv50_sor_dp_train_adj(struct drm_device *dev, struct dcb_output *dcb,
  48. u8 lane, u8 swing, u8 preem)
  49. {
  50. struct nv50_display *disp = nv50_display(dev);
  51. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  52. const u32 moff = (link << 2) | or;
  53. const u32 data = (swing << 8) | preem;
  54. nv_call(disp->core, NV94_DISP_SOR_DP_DRVCTL(lane) + moff, data);
  55. }
  56. static void
  57. nv50_sor_dp_link_set(struct drm_device *dev, struct dcb_output *dcb, int crtc,
  58. int link_nr, u32 link_bw, bool enhframe)
  59. {
  60. struct nv50_display *disp = nv50_display(dev);
  61. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  62. const u32 moff = (crtc << 3) | (link << 2) | or;
  63. u32 data = ((link_bw / 27000) << 8) | link_nr;
  64. if (enhframe)
  65. data |= NV94_DISP_SOR_DP_LNKCTL_FRAME_ENH;
  66. nv_call(disp->core, NV94_DISP_SOR_DP_LNKCTL + moff, data);
  67. }
  68. static void
  69. nv50_sor_disconnect(struct drm_encoder *encoder)
  70. {
  71. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  72. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  73. struct drm_device *dev = encoder->dev;
  74. struct nouveau_channel *evo = nv50_display(dev)->master;
  75. int ret;
  76. if (!nv_encoder->crtc)
  77. return;
  78. nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
  79. NV_DEBUG(drm, "Disconnecting SOR %d\n", nv_encoder->or);
  80. ret = RING_SPACE(evo, 4);
  81. if (ret) {
  82. NV_ERROR(drm, "no space while disconnecting SOR\n");
  83. return;
  84. }
  85. BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  86. OUT_RING (evo, 0);
  87. BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
  88. OUT_RING (evo, 0);
  89. nouveau_hdmi_mode_set(encoder, NULL);
  90. nv_encoder->crtc = NULL;
  91. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  92. }
  93. static void
  94. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  95. {
  96. struct nv50_display *priv = nv50_display(encoder->dev);
  97. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  98. struct drm_device *dev = encoder->dev;
  99. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  100. struct drm_encoder *enc;
  101. int or = nv_encoder->or;
  102. NV_DEBUG(drm, "or %d type %d mode %d\n", or, nv_encoder->dcb->type, mode);
  103. nv_encoder->last_dpms = mode;
  104. list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
  105. struct nouveau_encoder *nvenc = nouveau_encoder(enc);
  106. if (nvenc == nv_encoder ||
  107. (nvenc->dcb->type != DCB_OUTPUT_TMDS &&
  108. nvenc->dcb->type != DCB_OUTPUT_LVDS &&
  109. nvenc->dcb->type != DCB_OUTPUT_DP) ||
  110. nvenc->dcb->or != nv_encoder->dcb->or)
  111. continue;
  112. if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
  113. return;
  114. }
  115. nv_call(priv->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
  116. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  117. struct dp_train_func func = {
  118. .link_set = nv50_sor_dp_link_set,
  119. .train_set = nv50_sor_dp_train_set,
  120. .train_adj = nv50_sor_dp_train_adj
  121. };
  122. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
  123. }
  124. }
  125. static void
  126. nv50_sor_save(struct drm_encoder *encoder)
  127. {
  128. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  129. NV_ERROR(drm, "!!\n");
  130. }
  131. static void
  132. nv50_sor_restore(struct drm_encoder *encoder)
  133. {
  134. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  135. NV_ERROR(drm, "!!\n");
  136. }
  137. static bool
  138. nv50_sor_mode_fixup(struct drm_encoder *encoder,
  139. const struct drm_display_mode *mode,
  140. struct drm_display_mode *adjusted_mode)
  141. {
  142. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  143. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  144. struct nouveau_connector *connector;
  145. NV_DEBUG(drm, "or %d\n", nv_encoder->or);
  146. connector = nouveau_encoder_connector_get(nv_encoder);
  147. if (!connector) {
  148. NV_ERROR(drm, "Encoder has no connector\n");
  149. return false;
  150. }
  151. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  152. connector->native_mode)
  153. drm_mode_copy(adjusted_mode, connector->native_mode);
  154. return true;
  155. }
  156. static void
  157. nv50_sor_prepare(struct drm_encoder *encoder)
  158. {
  159. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  160. nv50_sor_disconnect(encoder);
  161. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  162. /* avoid race between link training and supervisor intr */
  163. nv50_display_sync(encoder->dev);
  164. }
  165. }
  166. static void
  167. nv50_sor_commit(struct drm_encoder *encoder)
  168. {
  169. }
  170. static void
  171. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  172. struct drm_display_mode *mode)
  173. {
  174. struct nouveau_channel *evo = nv50_display(encoder->dev)->master;
  175. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  176. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  177. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  178. struct nouveau_connector *nv_connector;
  179. struct nv50_display *disp = nv50_display(encoder->dev);
  180. struct nvbios *bios = &drm->vbios;
  181. uint32_t mode_ctl = 0, script;
  182. int ret;
  183. NV_DEBUG(drm, "or %d type %d -> crtc %d\n",
  184. nv_encoder->or, nv_encoder->dcb->type, crtc->index);
  185. nv_encoder->crtc = encoder->crtc;
  186. switch (nv_encoder->dcb->type) {
  187. case DCB_OUTPUT_TMDS:
  188. if (nv_encoder->dcb->sorconf.link & 1) {
  189. if (mode->clock < 165000)
  190. mode_ctl = 0x0100;
  191. else
  192. mode_ctl = 0x0500;
  193. } else
  194. mode_ctl = 0x0200;
  195. nouveau_hdmi_mode_set(encoder, mode);
  196. break;
  197. case DCB_OUTPUT_LVDS:
  198. script = 0x0000;
  199. if (bios->fp_no_ddc) {
  200. if (bios->fp.dual_link)
  201. script |= 0x0100;
  202. if (bios->fp.if_is_24bit)
  203. script |= 0x0200;
  204. } else {
  205. /* determine number of lvds links */
  206. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  207. if (nv_connector && nv_connector->edid &&
  208. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  209. /* http://www.spwg.org */
  210. if (((u8 *)nv_connector->edid)[121] == 2)
  211. script |= 0x0100;
  212. } else
  213. if (mode->clock >= bios->fp.duallink_transition_clk) {
  214. script |= 0x0100;
  215. }
  216. /* determine panel depth */
  217. if (script & 0x0100) {
  218. if (bios->fp.strapless_is_24bit & 2)
  219. script |= 0x0200;
  220. } else {
  221. if (bios->fp.strapless_is_24bit & 1)
  222. script |= 0x0200;
  223. }
  224. if (nv_connector && nv_connector->edid &&
  225. (nv_connector->edid->revision >= 4) &&
  226. (nv_connector->edid->input & 0x70) >= 0x20)
  227. script |= 0x0200;
  228. }
  229. nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + nv_encoder->or, script);
  230. break;
  231. case DCB_OUTPUT_DP:
  232. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  233. if (nv_connector && nv_connector->base.display_info.bpc == 6) {
  234. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  235. mode_ctl |= 0x00020000;
  236. } else {
  237. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  238. mode_ctl |= 0x00050000;
  239. }
  240. if (nv_encoder->dcb->sorconf.link & 1)
  241. mode_ctl |= 0x00000800;
  242. else
  243. mode_ctl |= 0x00000900;
  244. break;
  245. default:
  246. break;
  247. }
  248. if (crtc->index == 1)
  249. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
  250. else
  251. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
  252. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  253. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
  254. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  255. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
  256. nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  257. ret = RING_SPACE(evo, 2);
  258. if (ret) {
  259. NV_ERROR(drm, "no space while connecting SOR\n");
  260. nv_encoder->crtc = NULL;
  261. return;
  262. }
  263. BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  264. OUT_RING(evo, mode_ctl);
  265. }
  266. static struct drm_crtc *
  267. nv50_sor_crtc_get(struct drm_encoder *encoder)
  268. {
  269. return nouveau_encoder(encoder)->crtc;
  270. }
  271. static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
  272. .dpms = nv50_sor_dpms,
  273. .save = nv50_sor_save,
  274. .restore = nv50_sor_restore,
  275. .mode_fixup = nv50_sor_mode_fixup,
  276. .prepare = nv50_sor_prepare,
  277. .commit = nv50_sor_commit,
  278. .mode_set = nv50_sor_mode_set,
  279. .get_crtc = nv50_sor_crtc_get,
  280. .detect = NULL,
  281. .disable = nv50_sor_disconnect
  282. };
  283. static void
  284. nv50_sor_destroy(struct drm_encoder *encoder)
  285. {
  286. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  287. drm_encoder_cleanup(encoder);
  288. kfree(nv_encoder);
  289. }
  290. static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
  291. .destroy = nv50_sor_destroy,
  292. };
  293. int
  294. nv50_sor_create(struct drm_connector *connector, struct dcb_output *entry)
  295. {
  296. struct nouveau_encoder *nv_encoder = NULL;
  297. struct drm_device *dev = connector->dev;
  298. struct drm_encoder *encoder;
  299. int type;
  300. switch (entry->type) {
  301. case DCB_OUTPUT_TMDS:
  302. case DCB_OUTPUT_DP:
  303. type = DRM_MODE_ENCODER_TMDS;
  304. break;
  305. case DCB_OUTPUT_LVDS:
  306. type = DRM_MODE_ENCODER_LVDS;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  312. if (!nv_encoder)
  313. return -ENOMEM;
  314. encoder = to_drm_encoder(nv_encoder);
  315. nv_encoder->dcb = entry;
  316. nv_encoder->or = ffs(entry->or) - 1;
  317. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  318. drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
  319. drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
  320. encoder->possible_crtcs = entry->heads;
  321. encoder->possible_clones = 0;
  322. drm_mode_connector_attach_encoder(connector, encoder);
  323. return 0;
  324. }