nv50_sor.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 u32
  39. nv50_sor_dp_lane_map(struct drm_device *dev, struct dcb_output *dcb, u8 lane)
  40. {
  41. struct nouveau_drm *drm = nouveau_drm(dev);
  42. static const u8 nvaf[] = { 24, 16, 8, 0 }; /* thanks, apple.. */
  43. static const u8 nv50[] = { 16, 8, 0, 24 };
  44. if (nv_device(drm->device)->chipset == 0xaf)
  45. return nvaf[lane];
  46. return nv50[lane];
  47. }
  48. static void
  49. nv50_sor_dp_train_set(struct drm_device *dev, struct dcb_output *dcb, u8 pattern)
  50. {
  51. struct nouveau_device *device = nouveau_dev(dev);
  52. u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  53. nv_mask(device, NV50_SOR_DP_CTRL(or, link), 0x0f000000, pattern << 24);
  54. }
  55. static void
  56. nv50_sor_dp_train_adj(struct drm_device *dev, struct dcb_output *dcb,
  57. u8 lane, u8 swing, u8 preem)
  58. {
  59. struct nouveau_device *device = nouveau_dev(dev);
  60. struct nouveau_drm *drm = nouveau_drm(dev);
  61. u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  62. u32 shift = nv50_sor_dp_lane_map(dev, dcb, lane);
  63. u32 mask = 0x000000ff << shift;
  64. u8 *table, *entry, *config;
  65. table = nouveau_dp_bios_data(dev, dcb, &entry);
  66. if (!table || (table[0] != 0x20 && table[0] != 0x21)) {
  67. NV_ERROR(drm, "PDISP: unsupported DP table for chipset\n");
  68. return;
  69. }
  70. config = entry + table[4];
  71. while (config[0] != swing || config[1] != preem) {
  72. config += table[5];
  73. if (config >= entry + table[4] + entry[4] * table[5])
  74. return;
  75. }
  76. nv_mask(device, NV50_SOR_DP_UNK118(or, link), mask, config[2] << shift);
  77. nv_mask(device, NV50_SOR_DP_UNK120(or, link), mask, config[3] << shift);
  78. nv_mask(device, NV50_SOR_DP_UNK130(or, link), 0x0000ff00, config[4] << 8);
  79. }
  80. static void
  81. nv50_sor_dp_link_set(struct drm_device *dev, struct dcb_output *dcb, int crtc,
  82. int link_nr, u32 link_bw, bool enhframe)
  83. {
  84. struct nouveau_device *device = nouveau_dev(dev);
  85. struct nouveau_drm *drm = nouveau_drm(dev);
  86. u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  87. u32 dpctrl = nv_rd32(device, NV50_SOR_DP_CTRL(or, link)) & ~0x001f4000;
  88. u32 clksor = nv_rd32(device, 0x614300 + (or * 0x800)) & ~0x000c0000;
  89. u8 *table, *entry, mask;
  90. int i;
  91. table = nouveau_dp_bios_data(dev, dcb, &entry);
  92. if (!table || (table[0] != 0x20 && table[0] != 0x21)) {
  93. NV_ERROR(drm, "PDISP: unsupported DP table for chipset\n");
  94. return;
  95. }
  96. entry = ROMPTR(dev, entry[10]);
  97. if (entry) {
  98. while (link_bw < ROM16(entry[0]) * 10)
  99. entry += 4;
  100. nouveau_bios_run_init_table(dev, ROM16(entry[2]), dcb, crtc);
  101. }
  102. dpctrl |= ((1 << link_nr) - 1) << 16;
  103. if (enhframe)
  104. dpctrl |= 0x00004000;
  105. if (link_bw > 162000)
  106. clksor |= 0x00040000;
  107. nv_wr32(device, 0x614300 + (or * 0x800), clksor);
  108. nv_wr32(device, NV50_SOR_DP_CTRL(or, link), dpctrl);
  109. mask = 0;
  110. for (i = 0; i < link_nr; i++)
  111. mask |= 1 << (nv50_sor_dp_lane_map(dev, dcb, i) >> 3);
  112. nv_mask(device, NV50_SOR_DP_UNK130(or, link), 0x0000000f, mask);
  113. }
  114. static void
  115. nv50_sor_dp_link_get(struct drm_device *dev, u32 or, u32 link, u32 *nr, u32 *bw)
  116. {
  117. struct nouveau_device *device = nouveau_dev(dev);
  118. u32 dpctrl = nv_rd32(device, NV50_SOR_DP_CTRL(or, link)) & 0x000f0000;
  119. u32 clksor = nv_rd32(device, 0x614300 + (or * 0x800));
  120. if (clksor & 0x000c0000)
  121. *bw = 270000;
  122. else
  123. *bw = 162000;
  124. if (dpctrl > 0x00030000) *nr = 4;
  125. else if (dpctrl > 0x00010000) *nr = 2;
  126. else *nr = 1;
  127. }
  128. void
  129. nv50_sor_dp_calc_tu(struct drm_device *dev, int or, int link, u32 clk, u32 bpp)
  130. {
  131. struct nouveau_device *device = nouveau_dev(dev);
  132. struct nouveau_drm *drm = nouveau_drm(dev);
  133. const u32 symbol = 100000;
  134. int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
  135. int TU, VTUi, VTUf, VTUa;
  136. u64 link_data_rate, link_ratio, unk;
  137. u32 best_diff = 64 * symbol;
  138. u32 link_nr, link_bw, r;
  139. /* calculate packed data rate for each lane */
  140. nv50_sor_dp_link_get(dev, or, link, &link_nr, &link_bw);
  141. link_data_rate = (clk * bpp / 8) / link_nr;
  142. /* calculate ratio of packed data rate to link symbol rate */
  143. link_ratio = link_data_rate * symbol;
  144. r = do_div(link_ratio, link_bw);
  145. for (TU = 64; TU >= 32; TU--) {
  146. /* calculate average number of valid symbols in each TU */
  147. u32 tu_valid = link_ratio * TU;
  148. u32 calc, diff;
  149. /* find a hw representation for the fraction.. */
  150. VTUi = tu_valid / symbol;
  151. calc = VTUi * symbol;
  152. diff = tu_valid - calc;
  153. if (diff) {
  154. if (diff >= (symbol / 2)) {
  155. VTUf = symbol / (symbol - diff);
  156. if (symbol - (VTUf * diff))
  157. VTUf++;
  158. if (VTUf <= 15) {
  159. VTUa = 1;
  160. calc += symbol - (symbol / VTUf);
  161. } else {
  162. VTUa = 0;
  163. VTUf = 1;
  164. calc += symbol;
  165. }
  166. } else {
  167. VTUa = 0;
  168. VTUf = min((int)(symbol / diff), 15);
  169. calc += symbol / VTUf;
  170. }
  171. diff = calc - tu_valid;
  172. } else {
  173. /* no remainder, but the hw doesn't like the fractional
  174. * part to be zero. decrement the integer part and
  175. * have the fraction add a whole symbol back
  176. */
  177. VTUa = 0;
  178. VTUf = 1;
  179. VTUi--;
  180. }
  181. if (diff < best_diff) {
  182. best_diff = diff;
  183. bestTU = TU;
  184. bestVTUa = VTUa;
  185. bestVTUf = VTUf;
  186. bestVTUi = VTUi;
  187. if (diff == 0)
  188. break;
  189. }
  190. }
  191. if (!bestTU) {
  192. NV_ERROR(drm, "DP: unable to find suitable config\n");
  193. return;
  194. }
  195. /* XXX close to vbios numbers, but not right */
  196. unk = (symbol - link_ratio) * bestTU;
  197. unk *= link_ratio;
  198. r = do_div(unk, symbol);
  199. r = do_div(unk, symbol);
  200. unk += 6;
  201. nv_mask(device, NV50_SOR_DP_CTRL(or, link), 0x000001fc, bestTU << 2);
  202. nv_mask(device, NV50_SOR_DP_SCFG(or, link), 0x010f7f3f, bestVTUa << 24 |
  203. bestVTUf << 16 |
  204. bestVTUi << 8 |
  205. unk);
  206. }
  207. static void
  208. nv50_sor_disconnect(struct drm_encoder *encoder)
  209. {
  210. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  211. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  212. struct drm_device *dev = encoder->dev;
  213. struct nouveau_channel *evo = nv50_display(dev)->master;
  214. int ret;
  215. if (!nv_encoder->crtc)
  216. return;
  217. nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
  218. NV_DEBUG(drm, "Disconnecting SOR %d\n", nv_encoder->or);
  219. ret = RING_SPACE(evo, 4);
  220. if (ret) {
  221. NV_ERROR(drm, "no space while disconnecting SOR\n");
  222. return;
  223. }
  224. BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  225. OUT_RING (evo, 0);
  226. BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
  227. OUT_RING (evo, 0);
  228. nouveau_hdmi_mode_set(encoder, NULL);
  229. nv_encoder->crtc = NULL;
  230. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  231. }
  232. static void
  233. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  234. {
  235. struct nv50_display *priv = nv50_display(encoder->dev);
  236. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  237. struct drm_device *dev = encoder->dev;
  238. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  239. struct drm_encoder *enc;
  240. int or = nv_encoder->or;
  241. NV_DEBUG(drm, "or %d type %d mode %d\n", or, nv_encoder->dcb->type, mode);
  242. nv_encoder->last_dpms = mode;
  243. list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
  244. struct nouveau_encoder *nvenc = nouveau_encoder(enc);
  245. if (nvenc == nv_encoder ||
  246. (nvenc->dcb->type != DCB_OUTPUT_TMDS &&
  247. nvenc->dcb->type != DCB_OUTPUT_LVDS &&
  248. nvenc->dcb->type != DCB_OUTPUT_DP) ||
  249. nvenc->dcb->or != nv_encoder->dcb->or)
  250. continue;
  251. if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
  252. return;
  253. }
  254. nv_call(priv->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
  255. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  256. struct dp_train_func func = {
  257. .link_set = nv50_sor_dp_link_set,
  258. .train_set = nv50_sor_dp_train_set,
  259. .train_adj = nv50_sor_dp_train_adj
  260. };
  261. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
  262. }
  263. }
  264. static void
  265. nv50_sor_save(struct drm_encoder *encoder)
  266. {
  267. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  268. NV_ERROR(drm, "!!\n");
  269. }
  270. static void
  271. nv50_sor_restore(struct drm_encoder *encoder)
  272. {
  273. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  274. NV_ERROR(drm, "!!\n");
  275. }
  276. static bool
  277. nv50_sor_mode_fixup(struct drm_encoder *encoder,
  278. const struct drm_display_mode *mode,
  279. struct drm_display_mode *adjusted_mode)
  280. {
  281. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  282. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  283. struct nouveau_connector *connector;
  284. NV_DEBUG(drm, "or %d\n", nv_encoder->or);
  285. connector = nouveau_encoder_connector_get(nv_encoder);
  286. if (!connector) {
  287. NV_ERROR(drm, "Encoder has no connector\n");
  288. return false;
  289. }
  290. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  291. connector->native_mode)
  292. drm_mode_copy(adjusted_mode, connector->native_mode);
  293. return true;
  294. }
  295. static void
  296. nv50_sor_prepare(struct drm_encoder *encoder)
  297. {
  298. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  299. nv50_sor_disconnect(encoder);
  300. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  301. /* avoid race between link training and supervisor intr */
  302. nv50_display_sync(encoder->dev);
  303. }
  304. }
  305. static void
  306. nv50_sor_commit(struct drm_encoder *encoder)
  307. {
  308. }
  309. static void
  310. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  311. struct drm_display_mode *mode)
  312. {
  313. struct nouveau_channel *evo = nv50_display(encoder->dev)->master;
  314. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  315. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  316. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  317. struct nouveau_connector *nv_connector;
  318. uint32_t mode_ctl = 0;
  319. int ret;
  320. NV_DEBUG(drm, "or %d type %d -> crtc %d\n",
  321. nv_encoder->or, nv_encoder->dcb->type, crtc->index);
  322. nv_encoder->crtc = encoder->crtc;
  323. switch (nv_encoder->dcb->type) {
  324. case DCB_OUTPUT_TMDS:
  325. if (nv_encoder->dcb->sorconf.link & 1) {
  326. if (mode->clock < 165000)
  327. mode_ctl = 0x0100;
  328. else
  329. mode_ctl = 0x0500;
  330. } else
  331. mode_ctl = 0x0200;
  332. nouveau_hdmi_mode_set(encoder, mode);
  333. break;
  334. case DCB_OUTPUT_DP:
  335. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  336. if (nv_connector && nv_connector->base.display_info.bpc == 6) {
  337. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  338. mode_ctl |= 0x00020000;
  339. } else {
  340. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  341. mode_ctl |= 0x00050000;
  342. }
  343. if (nv_encoder->dcb->sorconf.link & 1)
  344. mode_ctl |= 0x00000800;
  345. else
  346. mode_ctl |= 0x00000900;
  347. break;
  348. default:
  349. break;
  350. }
  351. if (crtc->index == 1)
  352. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
  353. else
  354. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
  355. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  356. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
  357. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  358. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
  359. nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  360. ret = RING_SPACE(evo, 2);
  361. if (ret) {
  362. NV_ERROR(drm, "no space while connecting SOR\n");
  363. nv_encoder->crtc = NULL;
  364. return;
  365. }
  366. BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  367. OUT_RING(evo, mode_ctl);
  368. }
  369. static struct drm_crtc *
  370. nv50_sor_crtc_get(struct drm_encoder *encoder)
  371. {
  372. return nouveau_encoder(encoder)->crtc;
  373. }
  374. static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
  375. .dpms = nv50_sor_dpms,
  376. .save = nv50_sor_save,
  377. .restore = nv50_sor_restore,
  378. .mode_fixup = nv50_sor_mode_fixup,
  379. .prepare = nv50_sor_prepare,
  380. .commit = nv50_sor_commit,
  381. .mode_set = nv50_sor_mode_set,
  382. .get_crtc = nv50_sor_crtc_get,
  383. .detect = NULL,
  384. .disable = nv50_sor_disconnect
  385. };
  386. static void
  387. nv50_sor_destroy(struct drm_encoder *encoder)
  388. {
  389. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  390. drm_encoder_cleanup(encoder);
  391. kfree(nv_encoder);
  392. }
  393. static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
  394. .destroy = nv50_sor_destroy,
  395. };
  396. int
  397. nv50_sor_create(struct drm_connector *connector, struct dcb_output *entry)
  398. {
  399. struct nouveau_encoder *nv_encoder = NULL;
  400. struct drm_device *dev = connector->dev;
  401. struct drm_encoder *encoder;
  402. int type;
  403. switch (entry->type) {
  404. case DCB_OUTPUT_TMDS:
  405. case DCB_OUTPUT_DP:
  406. type = DRM_MODE_ENCODER_TMDS;
  407. break;
  408. case DCB_OUTPUT_LVDS:
  409. type = DRM_MODE_ENCODER_LVDS;
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  415. if (!nv_encoder)
  416. return -ENOMEM;
  417. encoder = to_drm_encoder(nv_encoder);
  418. nv_encoder->dcb = entry;
  419. nv_encoder->or = ffs(entry->or) - 1;
  420. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  421. drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
  422. drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
  423. encoder->possible_crtcs = entry->heads;
  424. encoder->possible_clones = 0;
  425. drm_mode_connector_attach_encoder(connector, encoder);
  426. return 0;
  427. }