nv50_crtc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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_mode.h"
  28. #include "drm_crtc_helper.h"
  29. #define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
  30. #include "nouveau_reg.h"
  31. #include "nouveau_drv.h"
  32. #include "nouveau_hw.h"
  33. #include "nouveau_encoder.h"
  34. #include "nouveau_crtc.h"
  35. #include "nouveau_fb.h"
  36. #include "nouveau_connector.h"
  37. #include "nv50_display.h"
  38. static void
  39. nv50_crtc_lut_load(struct drm_crtc *crtc)
  40. {
  41. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  42. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  43. int i;
  44. NV_DEBUG_KMS(crtc->dev, "\n");
  45. for (i = 0; i < 256; i++) {
  46. writew(nv_crtc->lut.r[i] >> 2, lut + 8*i + 0);
  47. writew(nv_crtc->lut.g[i] >> 2, lut + 8*i + 2);
  48. writew(nv_crtc->lut.b[i] >> 2, lut + 8*i + 4);
  49. }
  50. if (nv_crtc->lut.depth == 30) {
  51. writew(nv_crtc->lut.r[i - 1] >> 2, lut + 8*i + 0);
  52. writew(nv_crtc->lut.g[i - 1] >> 2, lut + 8*i + 2);
  53. writew(nv_crtc->lut.b[i - 1] >> 2, lut + 8*i + 4);
  54. }
  55. }
  56. int
  57. nv50_crtc_blank(struct nouveau_crtc *nv_crtc, bool blanked)
  58. {
  59. struct drm_device *dev = nv_crtc->base.dev;
  60. struct drm_nouveau_private *dev_priv = dev->dev_private;
  61. struct nouveau_channel *evo = nv50_display(dev)->master;
  62. int index = nv_crtc->index, ret;
  63. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  64. NV_DEBUG_KMS(dev, "%s\n", blanked ? "blanked" : "unblanked");
  65. if (blanked) {
  66. nv_crtc->cursor.hide(nv_crtc, false);
  67. ret = RING_SPACE(evo, dev_priv->chipset != 0x50 ? 7 : 5);
  68. if (ret) {
  69. NV_ERROR(dev, "no space while blanking crtc\n");
  70. return ret;
  71. }
  72. BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, CLUT_MODE), 2);
  73. OUT_RING(evo, NV50_EVO_CRTC_CLUT_MODE_BLANK);
  74. OUT_RING(evo, 0);
  75. if (dev_priv->chipset != 0x50) {
  76. BEGIN_RING(evo, 0, NV84_EVO_CRTC(index, CLUT_DMA), 1);
  77. OUT_RING(evo, NV84_EVO_CRTC_CLUT_DMA_HANDLE_NONE);
  78. }
  79. BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, FB_DMA), 1);
  80. OUT_RING(evo, NV50_EVO_CRTC_FB_DMA_HANDLE_NONE);
  81. } else {
  82. if (nv_crtc->cursor.visible)
  83. nv_crtc->cursor.show(nv_crtc, false);
  84. else
  85. nv_crtc->cursor.hide(nv_crtc, false);
  86. ret = RING_SPACE(evo, dev_priv->chipset != 0x50 ? 10 : 8);
  87. if (ret) {
  88. NV_ERROR(dev, "no space while unblanking crtc\n");
  89. return ret;
  90. }
  91. BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, CLUT_MODE), 2);
  92. OUT_RING(evo, nv_crtc->lut.depth == 8 ?
  93. NV50_EVO_CRTC_CLUT_MODE_OFF :
  94. NV50_EVO_CRTC_CLUT_MODE_ON);
  95. OUT_RING(evo, nv_crtc->lut.nvbo->bo.offset >> 8);
  96. if (dev_priv->chipset != 0x50) {
  97. BEGIN_RING(evo, 0, NV84_EVO_CRTC(index, CLUT_DMA), 1);
  98. OUT_RING(evo, NvEvoVRAM);
  99. }
  100. BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, FB_OFFSET), 2);
  101. OUT_RING(evo, nv_crtc->fb.offset >> 8);
  102. OUT_RING(evo, 0);
  103. BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, FB_DMA), 1);
  104. if (dev_priv->chipset != 0x50)
  105. if (nv_crtc->fb.tile_flags == 0x7a00 ||
  106. nv_crtc->fb.tile_flags == 0xfe00)
  107. OUT_RING(evo, NvEvoFB32);
  108. else
  109. if (nv_crtc->fb.tile_flags == 0x7000)
  110. OUT_RING(evo, NvEvoFB16);
  111. else
  112. OUT_RING(evo, NvEvoVRAM_LP);
  113. else
  114. OUT_RING(evo, NvEvoVRAM_LP);
  115. }
  116. nv_crtc->fb.blanked = blanked;
  117. return 0;
  118. }
  119. static int
  120. nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
  121. {
  122. struct drm_device *dev = nv_crtc->base.dev;
  123. struct nouveau_channel *evo = nv50_display(dev)->master;
  124. struct nouveau_connector *nv_connector =
  125. nouveau_crtc_connector_get(nv_crtc);
  126. int ret;
  127. NV_DEBUG_KMS(dev, "\n");
  128. ret = RING_SPACE(evo, 2 + (update ? 2 : 0));
  129. if (ret) {
  130. NV_ERROR(dev, "no space while setting dither\n");
  131. return ret;
  132. }
  133. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, DITHER_CTRL), 1);
  134. if (nv_connector->use_dithering)
  135. OUT_RING(evo, NV50_EVO_CRTC_DITHER_CTRL_ON);
  136. else
  137. OUT_RING(evo, NV50_EVO_CRTC_DITHER_CTRL_OFF);
  138. if (update) {
  139. BEGIN_RING(evo, 0, NV50_EVO_UPDATE, 1);
  140. OUT_RING(evo, 0);
  141. FIRE_RING(evo);
  142. }
  143. return 0;
  144. }
  145. struct nouveau_connector *
  146. nouveau_crtc_connector_get(struct nouveau_crtc *nv_crtc)
  147. {
  148. struct drm_device *dev = nv_crtc->base.dev;
  149. struct drm_connector *connector;
  150. struct drm_crtc *crtc = to_drm_crtc(nv_crtc);
  151. /* The safest approach is to find an encoder with the right crtc, that
  152. * is also linked to a connector. */
  153. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  154. if (connector->encoder)
  155. if (connector->encoder->crtc == crtc)
  156. return nouveau_connector(connector);
  157. }
  158. return NULL;
  159. }
  160. static int
  161. nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  162. {
  163. struct nouveau_connector *nv_connector;
  164. struct drm_crtc *crtc = &nv_crtc->base;
  165. struct drm_device *dev = crtc->dev;
  166. struct nouveau_channel *evo = nv50_display(dev)->master;
  167. struct drm_display_mode *mode = &crtc->mode;
  168. int scaling_mode, ret;
  169. u32 ctrl = 0, oX, oY;
  170. NV_DEBUG_KMS(dev, "\n");
  171. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  172. if (!nv_connector || !nv_connector->native_mode) {
  173. NV_ERROR(dev, "no native mode, forcing panel scaling\n");
  174. scaling_mode = DRM_MODE_SCALE_NONE;
  175. } else {
  176. scaling_mode = nv_connector->scaling_mode;
  177. }
  178. /* start off at the resolution we programmed the crtc for, this
  179. * effectively handles NONE/FULL scaling
  180. */
  181. if (scaling_mode != DRM_MODE_SCALE_NONE) {
  182. oX = nv_connector->native_mode->hdisplay;
  183. oY = nv_connector->native_mode->vdisplay;
  184. } else {
  185. oX = mode->hdisplay;
  186. oY = mode->vdisplay;
  187. }
  188. /* add overscan compensation if necessary, will keep the aspect
  189. * ratio the same as the backend mode unless overridden by the
  190. * user setting both hborder and vborder properties.
  191. */
  192. if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
  193. (nv_connector->underscan == UNDERSCAN_AUTO &&
  194. nv_connector->edid &&
  195. drm_detect_hdmi_monitor(nv_connector->edid)))) {
  196. u32 bX = nv_connector->underscan_hborder;
  197. u32 bY = nv_connector->underscan_vborder;
  198. u32 aspect = (oY << 19) / oX;
  199. if (bX) {
  200. oX -= (bX * 2);
  201. if (bY) oY -= (bY * 2);
  202. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  203. } else {
  204. oX -= (oX >> 4) + 32;
  205. if (bY) oY -= (bY * 2);
  206. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  207. }
  208. }
  209. /* handle CENTER/ASPECT scaling, taking into account the areas
  210. * removed already for overscan compensation
  211. */
  212. switch (scaling_mode) {
  213. case DRM_MODE_SCALE_CENTER:
  214. oX = min((u32)mode->hdisplay, oX);
  215. oY = min((u32)mode->vdisplay, oY);
  216. /* fall-through */
  217. case DRM_MODE_SCALE_ASPECT:
  218. if (oY < oX) {
  219. u32 aspect = (mode->hdisplay << 19) / mode->vdisplay;
  220. oX = ((oY * aspect) + (aspect / 2)) >> 19;
  221. } else {
  222. u32 aspect = (mode->vdisplay << 19) / mode->hdisplay;
  223. oY = ((oX * aspect) + (aspect / 2)) >> 19;
  224. }
  225. break;
  226. default:
  227. break;
  228. }
  229. if (mode->hdisplay != oX || mode->vdisplay != oY ||
  230. mode->flags & DRM_MODE_FLAG_INTERLACE ||
  231. mode->flags & DRM_MODE_FLAG_DBLSCAN)
  232. ctrl |= NV50_EVO_CRTC_SCALE_CTRL_ACTIVE;
  233. ret = RING_SPACE(evo, 5);
  234. if (ret)
  235. return ret;
  236. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CTRL), 1);
  237. OUT_RING (evo, ctrl);
  238. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_RES1), 2);
  239. OUT_RING (evo, oY << 16 | oX);
  240. OUT_RING (evo, oY << 16 | oX);
  241. if (update) {
  242. nv50_display_flip_stop(crtc);
  243. nv50_display_sync(dev);
  244. nv50_display_flip_next(crtc, crtc->fb, NULL);
  245. }
  246. return 0;
  247. }
  248. int
  249. nv50_crtc_set_clock(struct drm_device *dev, int head, int pclk)
  250. {
  251. struct drm_nouveau_private *dev_priv = dev->dev_private;
  252. struct pll_lims pll;
  253. uint32_t reg1, reg2;
  254. int ret, N1, M1, N2, M2, P;
  255. ret = get_pll_limits(dev, PLL_VPLL0 + head, &pll);
  256. if (ret)
  257. return ret;
  258. if (pll.vco2.maxfreq) {
  259. ret = nv50_calc_pll(dev, &pll, pclk, &N1, &M1, &N2, &M2, &P);
  260. if (ret <= 0)
  261. return 0;
  262. NV_DEBUG(dev, "pclk %d out %d NM1 %d %d NM2 %d %d P %d\n",
  263. pclk, ret, N1, M1, N2, M2, P);
  264. reg1 = nv_rd32(dev, pll.reg + 4) & 0xff00ff00;
  265. reg2 = nv_rd32(dev, pll.reg + 8) & 0x8000ff00;
  266. nv_wr32(dev, pll.reg + 0, 0x10000611);
  267. nv_wr32(dev, pll.reg + 4, reg1 | (M1 << 16) | N1);
  268. nv_wr32(dev, pll.reg + 8, reg2 | (P << 28) | (M2 << 16) | N2);
  269. } else
  270. if (dev_priv->chipset < NV_C0) {
  271. ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
  272. if (ret <= 0)
  273. return 0;
  274. NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
  275. pclk, ret, N1, N2, M1, P);
  276. reg1 = nv_rd32(dev, pll.reg + 4) & 0xffc00000;
  277. nv_wr32(dev, pll.reg + 0, 0x50000610);
  278. nv_wr32(dev, pll.reg + 4, reg1 | (P << 16) | (M1 << 8) | N1);
  279. nv_wr32(dev, pll.reg + 8, N2);
  280. } else {
  281. ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
  282. if (ret <= 0)
  283. return 0;
  284. NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
  285. pclk, ret, N1, N2, M1, P);
  286. nv_mask(dev, pll.reg + 0x0c, 0x00000000, 0x00000100);
  287. nv_wr32(dev, pll.reg + 0x04, (P << 16) | (N1 << 8) | M1);
  288. nv_wr32(dev, pll.reg + 0x10, N2 << 16);
  289. }
  290. return 0;
  291. }
  292. static void
  293. nv50_crtc_destroy(struct drm_crtc *crtc)
  294. {
  295. struct drm_device *dev;
  296. struct nouveau_crtc *nv_crtc;
  297. if (!crtc)
  298. return;
  299. dev = crtc->dev;
  300. nv_crtc = nouveau_crtc(crtc);
  301. NV_DEBUG_KMS(dev, "\n");
  302. drm_crtc_cleanup(&nv_crtc->base);
  303. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  304. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  305. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  306. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  307. kfree(nv_crtc->mode);
  308. kfree(nv_crtc);
  309. }
  310. int
  311. nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  312. uint32_t buffer_handle, uint32_t width, uint32_t height)
  313. {
  314. struct drm_device *dev = crtc->dev;
  315. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  316. struct nouveau_bo *cursor = NULL;
  317. struct drm_gem_object *gem;
  318. int ret = 0, i;
  319. if (!buffer_handle) {
  320. nv_crtc->cursor.hide(nv_crtc, true);
  321. return 0;
  322. }
  323. if (width != 64 || height != 64)
  324. return -EINVAL;
  325. gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
  326. if (!gem)
  327. return -ENOENT;
  328. cursor = nouveau_gem_object(gem);
  329. ret = nouveau_bo_map(cursor);
  330. if (ret)
  331. goto out;
  332. /* The simple will do for now. */
  333. for (i = 0; i < 64 * 64; i++)
  334. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, nouveau_bo_rd32(cursor, i));
  335. nouveau_bo_unmap(cursor);
  336. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  337. nv_crtc->cursor.show(nv_crtc, true);
  338. out:
  339. drm_gem_object_unreference_unlocked(gem);
  340. return ret;
  341. }
  342. int
  343. nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  344. {
  345. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  346. nv_crtc->cursor.set_pos(nv_crtc, x, y);
  347. return 0;
  348. }
  349. static void
  350. nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  351. uint32_t start, uint32_t size)
  352. {
  353. int end = (start + size > 256) ? 256 : start + size, i;
  354. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  355. for (i = start; i < end; i++) {
  356. nv_crtc->lut.r[i] = r[i];
  357. nv_crtc->lut.g[i] = g[i];
  358. nv_crtc->lut.b[i] = b[i];
  359. }
  360. /* We need to know the depth before we upload, but it's possible to
  361. * get called before a framebuffer is bound. If this is the case,
  362. * mark the lut values as dirty by setting depth==0, and it'll be
  363. * uploaded on the first mode_set_base()
  364. */
  365. if (!nv_crtc->base.fb) {
  366. nv_crtc->lut.depth = 0;
  367. return;
  368. }
  369. nv50_crtc_lut_load(crtc);
  370. }
  371. static void
  372. nv50_crtc_save(struct drm_crtc *crtc)
  373. {
  374. NV_ERROR(crtc->dev, "!!\n");
  375. }
  376. static void
  377. nv50_crtc_restore(struct drm_crtc *crtc)
  378. {
  379. NV_ERROR(crtc->dev, "!!\n");
  380. }
  381. static const struct drm_crtc_funcs nv50_crtc_funcs = {
  382. .save = nv50_crtc_save,
  383. .restore = nv50_crtc_restore,
  384. .cursor_set = nv50_crtc_cursor_set,
  385. .cursor_move = nv50_crtc_cursor_move,
  386. .gamma_set = nv50_crtc_gamma_set,
  387. .set_config = drm_crtc_helper_set_config,
  388. .page_flip = nouveau_crtc_page_flip,
  389. .destroy = nv50_crtc_destroy,
  390. };
  391. static void
  392. nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
  393. {
  394. }
  395. static void
  396. nv50_crtc_prepare(struct drm_crtc *crtc)
  397. {
  398. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  399. struct drm_device *dev = crtc->dev;
  400. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  401. nv50_display_flip_stop(crtc);
  402. drm_vblank_pre_modeset(dev, nv_crtc->index);
  403. nv50_crtc_blank(nv_crtc, true);
  404. }
  405. static void
  406. nv50_crtc_commit(struct drm_crtc *crtc)
  407. {
  408. struct drm_device *dev = crtc->dev;
  409. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  410. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  411. nv50_crtc_blank(nv_crtc, false);
  412. drm_vblank_post_modeset(dev, nv_crtc->index);
  413. nv50_display_sync(dev);
  414. nv50_display_flip_next(crtc, crtc->fb, NULL);
  415. }
  416. static bool
  417. nv50_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
  418. struct drm_display_mode *adjusted_mode)
  419. {
  420. return true;
  421. }
  422. static int
  423. nv50_crtc_do_mode_set_base(struct drm_crtc *crtc,
  424. struct drm_framebuffer *passed_fb,
  425. int x, int y, bool atomic)
  426. {
  427. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  428. struct drm_device *dev = nv_crtc->base.dev;
  429. struct drm_nouveau_private *dev_priv = dev->dev_private;
  430. struct nouveau_channel *evo = nv50_display(dev)->master;
  431. struct drm_framebuffer *drm_fb;
  432. struct nouveau_framebuffer *fb;
  433. int ret;
  434. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  435. /* no fb bound */
  436. if (!atomic && !crtc->fb) {
  437. NV_DEBUG_KMS(dev, "No FB bound\n");
  438. return 0;
  439. }
  440. /* If atomic, we want to switch to the fb we were passed, so
  441. * now we update pointers to do that. (We don't pin; just
  442. * assume we're already pinned and update the base address.)
  443. */
  444. if (atomic) {
  445. drm_fb = passed_fb;
  446. fb = nouveau_framebuffer(passed_fb);
  447. } else {
  448. drm_fb = crtc->fb;
  449. fb = nouveau_framebuffer(crtc->fb);
  450. /* If not atomic, we can go ahead and pin, and unpin the
  451. * old fb we were passed.
  452. */
  453. ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
  454. if (ret)
  455. return ret;
  456. if (passed_fb) {
  457. struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
  458. nouveau_bo_unpin(ofb->nvbo);
  459. }
  460. }
  461. nv_crtc->fb.offset = fb->nvbo->bo.offset;
  462. nv_crtc->fb.tile_flags = nouveau_bo_tile_layout(fb->nvbo);
  463. nv_crtc->fb.cpp = drm_fb->bits_per_pixel / 8;
  464. if (!nv_crtc->fb.blanked && dev_priv->chipset != 0x50) {
  465. ret = RING_SPACE(evo, 2);
  466. if (ret)
  467. return ret;
  468. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_DMA), 1);
  469. OUT_RING (evo, fb->r_dma);
  470. }
  471. ret = RING_SPACE(evo, 12);
  472. if (ret)
  473. return ret;
  474. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_OFFSET), 5);
  475. OUT_RING (evo, nv_crtc->fb.offset >> 8);
  476. OUT_RING (evo, 0);
  477. OUT_RING (evo, (drm_fb->height << 16) | drm_fb->width);
  478. OUT_RING (evo, fb->r_pitch);
  479. OUT_RING (evo, fb->r_format);
  480. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLUT_MODE), 1);
  481. OUT_RING (evo, fb->base.depth == 8 ?
  482. NV50_EVO_CRTC_CLUT_MODE_OFF : NV50_EVO_CRTC_CLUT_MODE_ON);
  483. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, COLOR_CTRL), 1);
  484. OUT_RING (evo, NV50_EVO_CRTC_COLOR_CTRL_COLOR);
  485. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_POS), 1);
  486. OUT_RING (evo, (y << 16) | x);
  487. if (nv_crtc->lut.depth != fb->base.depth) {
  488. nv_crtc->lut.depth = fb->base.depth;
  489. nv50_crtc_lut_load(crtc);
  490. }
  491. return 0;
  492. }
  493. static int
  494. nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  495. struct drm_display_mode *adjusted_mode, int x, int y,
  496. struct drm_framebuffer *old_fb)
  497. {
  498. struct drm_device *dev = crtc->dev;
  499. struct nouveau_channel *evo = nv50_display(dev)->master;
  500. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  501. struct nouveau_connector *nv_connector = NULL;
  502. uint32_t hsync_dur, vsync_dur, hsync_start_to_end, vsync_start_to_end;
  503. uint32_t hunk1, vunk1, vunk2a, vunk2b;
  504. int ret;
  505. /* Find the connector attached to this CRTC */
  506. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  507. *nv_crtc->mode = *adjusted_mode;
  508. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  509. hsync_dur = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
  510. vsync_dur = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
  511. hsync_start_to_end = adjusted_mode->htotal - adjusted_mode->hsync_start;
  512. vsync_start_to_end = adjusted_mode->vtotal - adjusted_mode->vsync_start;
  513. /* I can't give this a proper name, anyone else can? */
  514. hunk1 = adjusted_mode->htotal -
  515. adjusted_mode->hsync_start + adjusted_mode->hdisplay;
  516. vunk1 = adjusted_mode->vtotal -
  517. adjusted_mode->vsync_start + adjusted_mode->vdisplay;
  518. /* Another strange value, this time only for interlaced adjusted_modes. */
  519. vunk2a = 2 * adjusted_mode->vtotal -
  520. adjusted_mode->vsync_start + adjusted_mode->vdisplay;
  521. vunk2b = adjusted_mode->vtotal -
  522. adjusted_mode->vsync_start + adjusted_mode->vtotal;
  523. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  524. vsync_dur /= 2;
  525. vsync_start_to_end /= 2;
  526. vunk1 /= 2;
  527. vunk2a /= 2;
  528. vunk2b /= 2;
  529. /* magic */
  530. if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN) {
  531. vsync_start_to_end -= 1;
  532. vunk1 -= 1;
  533. vunk2a -= 1;
  534. vunk2b -= 1;
  535. }
  536. }
  537. ret = RING_SPACE(evo, 19);
  538. if (ret)
  539. return ret;
  540. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLOCK), 2);
  541. OUT_RING(evo, adjusted_mode->clock | 0x800000);
  542. OUT_RING(evo, (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 0);
  543. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, DISPLAY_START), 5);
  544. OUT_RING(evo, 0);
  545. OUT_RING(evo, (adjusted_mode->vtotal << 16) | adjusted_mode->htotal);
  546. OUT_RING(evo, (vsync_dur - 1) << 16 | (hsync_dur - 1));
  547. OUT_RING(evo, (vsync_start_to_end - 1) << 16 |
  548. (hsync_start_to_end - 1));
  549. OUT_RING(evo, (vunk1 - 1) << 16 | (hunk1 - 1));
  550. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  551. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, UNK0824), 1);
  552. OUT_RING(evo, (vunk2b - 1) << 16 | (vunk2a - 1));
  553. } else {
  554. OUT_RING(evo, 0);
  555. OUT_RING(evo, 0);
  556. }
  557. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, UNK082C), 1);
  558. OUT_RING (evo, 0);
  559. /* required to make display sync channel not hate life */
  560. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, UNK900), 1);
  561. OUT_RING (evo, 0x00000311);
  562. /* This is the actual resolution of the mode. */
  563. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, REAL_RES), 1);
  564. OUT_RING(evo, (mode->vdisplay << 16) | mode->hdisplay);
  565. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CENTER_OFFSET), 1);
  566. OUT_RING(evo, NV50_EVO_CRTC_SCALE_CENTER_OFFSET_VAL(0, 0));
  567. nv_crtc->set_dither(nv_crtc, false);
  568. nv_crtc->set_scale(nv_crtc, false);
  569. return nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
  570. }
  571. static int
  572. nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  573. struct drm_framebuffer *old_fb)
  574. {
  575. int ret;
  576. nv50_display_flip_stop(crtc);
  577. ret = nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
  578. if (ret)
  579. return ret;
  580. ret = nv50_display_sync(crtc->dev);
  581. if (ret)
  582. return ret;
  583. return nv50_display_flip_next(crtc, crtc->fb, NULL);
  584. }
  585. static int
  586. nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  587. struct drm_framebuffer *fb,
  588. int x, int y, enum mode_set_atomic state)
  589. {
  590. int ret;
  591. nv50_display_flip_stop(crtc);
  592. ret = nv50_crtc_do_mode_set_base(crtc, fb, x, y, true);
  593. if (ret)
  594. return ret;
  595. return nv50_display_sync(crtc->dev);
  596. }
  597. static const struct drm_crtc_helper_funcs nv50_crtc_helper_funcs = {
  598. .dpms = nv50_crtc_dpms,
  599. .prepare = nv50_crtc_prepare,
  600. .commit = nv50_crtc_commit,
  601. .mode_fixup = nv50_crtc_mode_fixup,
  602. .mode_set = nv50_crtc_mode_set,
  603. .mode_set_base = nv50_crtc_mode_set_base,
  604. .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
  605. .load_lut = nv50_crtc_lut_load,
  606. };
  607. int
  608. nv50_crtc_create(struct drm_device *dev, int index)
  609. {
  610. struct nouveau_crtc *nv_crtc = NULL;
  611. int ret, i;
  612. NV_DEBUG_KMS(dev, "\n");
  613. nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
  614. if (!nv_crtc)
  615. return -ENOMEM;
  616. nv_crtc->mode = kzalloc(sizeof(*nv_crtc->mode), GFP_KERNEL);
  617. if (!nv_crtc->mode) {
  618. kfree(nv_crtc);
  619. return -ENOMEM;
  620. }
  621. /* Default CLUT parameters, will be activated on the hw upon
  622. * first mode set.
  623. */
  624. for (i = 0; i < 256; i++) {
  625. nv_crtc->lut.r[i] = i << 8;
  626. nv_crtc->lut.g[i] = i << 8;
  627. nv_crtc->lut.b[i] = i << 8;
  628. }
  629. nv_crtc->lut.depth = 0;
  630. ret = nouveau_bo_new(dev, 4096, 0x100, TTM_PL_FLAG_VRAM,
  631. 0, 0x0000, &nv_crtc->lut.nvbo);
  632. if (!ret) {
  633. ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
  634. if (!ret)
  635. ret = nouveau_bo_map(nv_crtc->lut.nvbo);
  636. if (ret)
  637. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  638. }
  639. if (ret) {
  640. kfree(nv_crtc->mode);
  641. kfree(nv_crtc);
  642. return ret;
  643. }
  644. nv_crtc->index = index;
  645. /* set function pointers */
  646. nv_crtc->set_dither = nv50_crtc_set_dither;
  647. nv_crtc->set_scale = nv50_crtc_set_scale;
  648. drm_crtc_init(dev, &nv_crtc->base, &nv50_crtc_funcs);
  649. drm_crtc_helper_add(&nv_crtc->base, &nv50_crtc_helper_funcs);
  650. drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
  651. ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
  652. 0, 0x0000, &nv_crtc->cursor.nvbo);
  653. if (!ret) {
  654. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  655. if (!ret)
  656. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  657. if (ret)
  658. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  659. }
  660. nv50_cursor_init(nv_crtc);
  661. return 0;
  662. }