nv50_sor.c 14 KB

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