nv50_crtc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 nouveau_channel *evo = nv50_display(nv_crtc->base.dev)->master;
  123. struct nouveau_connector *nv_connector;
  124. struct drm_connector *connector;
  125. int head = nv_crtc->index, ret;
  126. u32 mode = 0x00;
  127. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  128. connector = &nv_connector->base;
  129. if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
  130. if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
  131. mode = DITHERING_MODE_DYNAMIC2X2;
  132. } else {
  133. mode = nv_connector->dithering_mode;
  134. }
  135. if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
  136. if (connector->display_info.bpc >= 8)
  137. mode |= DITHERING_DEPTH_8BPC;
  138. } else {
  139. mode |= nv_connector->dithering_depth;
  140. }
  141. ret = RING_SPACE(evo, 2 + (update ? 2 : 0));
  142. if (ret == 0) {
  143. BEGIN_RING(evo, 0, NV50_EVO_CRTC(head, DITHER_CTRL), 1);
  144. OUT_RING (evo, mode);
  145. if (update) {
  146. BEGIN_RING(evo, 0, NV50_EVO_UPDATE, 1);
  147. OUT_RING (evo, 0);
  148. FIRE_RING (evo);
  149. }
  150. }
  151. return ret;
  152. }
  153. struct nouveau_connector *
  154. nouveau_crtc_connector_get(struct nouveau_crtc *nv_crtc)
  155. {
  156. struct drm_device *dev = nv_crtc->base.dev;
  157. struct drm_connector *connector;
  158. struct drm_crtc *crtc = to_drm_crtc(nv_crtc);
  159. /* The safest approach is to find an encoder with the right crtc, that
  160. * is also linked to a connector. */
  161. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  162. if (connector->encoder)
  163. if (connector->encoder->crtc == crtc)
  164. return nouveau_connector(connector);
  165. }
  166. return NULL;
  167. }
  168. static int
  169. nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  170. {
  171. struct nouveau_connector *nv_connector;
  172. struct drm_crtc *crtc = &nv_crtc->base;
  173. struct drm_device *dev = crtc->dev;
  174. struct nouveau_channel *evo = nv50_display(dev)->master;
  175. struct drm_display_mode *mode = &crtc->mode;
  176. int scaling_mode, ret;
  177. u32 ctrl = 0, oX, oY;
  178. NV_DEBUG_KMS(dev, "\n");
  179. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  180. if (!nv_connector || !nv_connector->native_mode) {
  181. NV_ERROR(dev, "no native mode, forcing panel scaling\n");
  182. scaling_mode = DRM_MODE_SCALE_NONE;
  183. } else {
  184. scaling_mode = nv_connector->scaling_mode;
  185. }
  186. /* start off at the resolution we programmed the crtc for, this
  187. * effectively handles NONE/FULL scaling
  188. */
  189. if (scaling_mode != DRM_MODE_SCALE_NONE) {
  190. oX = nv_connector->native_mode->hdisplay;
  191. oY = nv_connector->native_mode->vdisplay;
  192. } else {
  193. oX = mode->hdisplay;
  194. oY = mode->vdisplay;
  195. }
  196. /* add overscan compensation if necessary, will keep the aspect
  197. * ratio the same as the backend mode unless overridden by the
  198. * user setting both hborder and vborder properties.
  199. */
  200. if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
  201. (nv_connector->underscan == UNDERSCAN_AUTO &&
  202. nv_connector->edid &&
  203. drm_detect_hdmi_monitor(nv_connector->edid)))) {
  204. u32 bX = nv_connector->underscan_hborder;
  205. u32 bY = nv_connector->underscan_vborder;
  206. u32 aspect = (oY << 19) / oX;
  207. if (bX) {
  208. oX -= (bX * 2);
  209. if (bY) oY -= (bY * 2);
  210. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  211. } else {
  212. oX -= (oX >> 4) + 32;
  213. if (bY) oY -= (bY * 2);
  214. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  215. }
  216. }
  217. /* handle CENTER/ASPECT scaling, taking into account the areas
  218. * removed already for overscan compensation
  219. */
  220. switch (scaling_mode) {
  221. case DRM_MODE_SCALE_CENTER:
  222. oX = min((u32)mode->hdisplay, oX);
  223. oY = min((u32)mode->vdisplay, oY);
  224. /* fall-through */
  225. case DRM_MODE_SCALE_ASPECT:
  226. if (oY < oX) {
  227. u32 aspect = (mode->hdisplay << 19) / mode->vdisplay;
  228. oX = ((oY * aspect) + (aspect / 2)) >> 19;
  229. } else {
  230. u32 aspect = (mode->vdisplay << 19) / mode->hdisplay;
  231. oY = ((oX * aspect) + (aspect / 2)) >> 19;
  232. }
  233. break;
  234. default:
  235. break;
  236. }
  237. if (mode->hdisplay != oX || mode->vdisplay != oY ||
  238. mode->flags & DRM_MODE_FLAG_INTERLACE ||
  239. mode->flags & DRM_MODE_FLAG_DBLSCAN)
  240. ctrl |= NV50_EVO_CRTC_SCALE_CTRL_ACTIVE;
  241. ret = RING_SPACE(evo, 5);
  242. if (ret)
  243. return ret;
  244. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CTRL), 1);
  245. OUT_RING (evo, ctrl);
  246. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_RES1), 2);
  247. OUT_RING (evo, oY << 16 | oX);
  248. OUT_RING (evo, oY << 16 | oX);
  249. if (update) {
  250. nv50_display_flip_stop(crtc);
  251. nv50_display_sync(dev);
  252. nv50_display_flip_next(crtc, crtc->fb, NULL);
  253. }
  254. return 0;
  255. }
  256. int
  257. nv50_crtc_set_clock(struct drm_device *dev, int head, int pclk)
  258. {
  259. struct drm_nouveau_private *dev_priv = dev->dev_private;
  260. struct pll_lims pll;
  261. uint32_t reg1, reg2;
  262. int ret, N1, M1, N2, M2, P;
  263. ret = get_pll_limits(dev, PLL_VPLL0 + head, &pll);
  264. if (ret)
  265. return ret;
  266. if (pll.vco2.maxfreq) {
  267. ret = nv50_calc_pll(dev, &pll, pclk, &N1, &M1, &N2, &M2, &P);
  268. if (ret <= 0)
  269. return 0;
  270. NV_DEBUG(dev, "pclk %d out %d NM1 %d %d NM2 %d %d P %d\n",
  271. pclk, ret, N1, M1, N2, M2, P);
  272. reg1 = nv_rd32(dev, pll.reg + 4) & 0xff00ff00;
  273. reg2 = nv_rd32(dev, pll.reg + 8) & 0x8000ff00;
  274. nv_wr32(dev, pll.reg + 0, 0x10000611);
  275. nv_wr32(dev, pll.reg + 4, reg1 | (M1 << 16) | N1);
  276. nv_wr32(dev, pll.reg + 8, reg2 | (P << 28) | (M2 << 16) | N2);
  277. } else
  278. if (dev_priv->chipset < NV_C0) {
  279. ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
  280. if (ret <= 0)
  281. return 0;
  282. NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
  283. pclk, ret, N1, N2, M1, P);
  284. reg1 = nv_rd32(dev, pll.reg + 4) & 0xffc00000;
  285. nv_wr32(dev, pll.reg + 0, 0x50000610);
  286. nv_wr32(dev, pll.reg + 4, reg1 | (P << 16) | (M1 << 8) | N1);
  287. nv_wr32(dev, pll.reg + 8, N2);
  288. } else {
  289. ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
  290. if (ret <= 0)
  291. return 0;
  292. NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
  293. pclk, ret, N1, N2, M1, P);
  294. nv_mask(dev, pll.reg + 0x0c, 0x00000000, 0x00000100);
  295. nv_wr32(dev, pll.reg + 0x04, (P << 16) | (N1 << 8) | M1);
  296. nv_wr32(dev, pll.reg + 0x10, N2 << 16);
  297. }
  298. return 0;
  299. }
  300. static void
  301. nv50_crtc_destroy(struct drm_crtc *crtc)
  302. {
  303. struct drm_device *dev;
  304. struct nouveau_crtc *nv_crtc;
  305. if (!crtc)
  306. return;
  307. dev = crtc->dev;
  308. nv_crtc = nouveau_crtc(crtc);
  309. NV_DEBUG_KMS(dev, "\n");
  310. drm_crtc_cleanup(&nv_crtc->base);
  311. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  312. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  313. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  314. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  315. kfree(nv_crtc);
  316. }
  317. int
  318. nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  319. uint32_t buffer_handle, uint32_t width, uint32_t height)
  320. {
  321. struct drm_device *dev = crtc->dev;
  322. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  323. struct nouveau_bo *cursor = NULL;
  324. struct drm_gem_object *gem;
  325. int ret = 0, i;
  326. if (!buffer_handle) {
  327. nv_crtc->cursor.hide(nv_crtc, true);
  328. return 0;
  329. }
  330. if (width != 64 || height != 64)
  331. return -EINVAL;
  332. gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
  333. if (!gem)
  334. return -ENOENT;
  335. cursor = nouveau_gem_object(gem);
  336. ret = nouveau_bo_map(cursor);
  337. if (ret)
  338. goto out;
  339. /* The simple will do for now. */
  340. for (i = 0; i < 64 * 64; i++)
  341. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, nouveau_bo_rd32(cursor, i));
  342. nouveau_bo_unmap(cursor);
  343. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  344. nv_crtc->cursor.show(nv_crtc, true);
  345. out:
  346. drm_gem_object_unreference_unlocked(gem);
  347. return ret;
  348. }
  349. int
  350. nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  351. {
  352. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  353. nv_crtc->cursor.set_pos(nv_crtc, x, y);
  354. return 0;
  355. }
  356. static void
  357. nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  358. uint32_t start, uint32_t size)
  359. {
  360. int end = (start + size > 256) ? 256 : start + size, i;
  361. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  362. for (i = start; i < end; i++) {
  363. nv_crtc->lut.r[i] = r[i];
  364. nv_crtc->lut.g[i] = g[i];
  365. nv_crtc->lut.b[i] = b[i];
  366. }
  367. /* We need to know the depth before we upload, but it's possible to
  368. * get called before a framebuffer is bound. If this is the case,
  369. * mark the lut values as dirty by setting depth==0, and it'll be
  370. * uploaded on the first mode_set_base()
  371. */
  372. if (!nv_crtc->base.fb) {
  373. nv_crtc->lut.depth = 0;
  374. return;
  375. }
  376. nv50_crtc_lut_load(crtc);
  377. }
  378. static void
  379. nv50_crtc_save(struct drm_crtc *crtc)
  380. {
  381. NV_ERROR(crtc->dev, "!!\n");
  382. }
  383. static void
  384. nv50_crtc_restore(struct drm_crtc *crtc)
  385. {
  386. NV_ERROR(crtc->dev, "!!\n");
  387. }
  388. static const struct drm_crtc_funcs nv50_crtc_funcs = {
  389. .save = nv50_crtc_save,
  390. .restore = nv50_crtc_restore,
  391. .cursor_set = nv50_crtc_cursor_set,
  392. .cursor_move = nv50_crtc_cursor_move,
  393. .gamma_set = nv50_crtc_gamma_set,
  394. .set_config = drm_crtc_helper_set_config,
  395. .page_flip = nouveau_crtc_page_flip,
  396. .destroy = nv50_crtc_destroy,
  397. };
  398. static void
  399. nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
  400. {
  401. }
  402. static void
  403. nv50_crtc_prepare(struct drm_crtc *crtc)
  404. {
  405. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  406. struct drm_device *dev = crtc->dev;
  407. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  408. nv50_display_flip_stop(crtc);
  409. drm_vblank_pre_modeset(dev, nv_crtc->index);
  410. nv50_crtc_blank(nv_crtc, true);
  411. }
  412. static void
  413. nv50_crtc_commit(struct drm_crtc *crtc)
  414. {
  415. struct drm_device *dev = crtc->dev;
  416. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  417. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  418. nv50_crtc_blank(nv_crtc, false);
  419. drm_vblank_post_modeset(dev, nv_crtc->index);
  420. nv50_display_sync(dev);
  421. nv50_display_flip_next(crtc, crtc->fb, NULL);
  422. }
  423. static bool
  424. nv50_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
  425. struct drm_display_mode *adjusted_mode)
  426. {
  427. return true;
  428. }
  429. static int
  430. nv50_crtc_do_mode_set_base(struct drm_crtc *crtc,
  431. struct drm_framebuffer *passed_fb,
  432. int x, int y, bool atomic)
  433. {
  434. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  435. struct drm_device *dev = nv_crtc->base.dev;
  436. struct drm_nouveau_private *dev_priv = dev->dev_private;
  437. struct nouveau_channel *evo = nv50_display(dev)->master;
  438. struct drm_framebuffer *drm_fb;
  439. struct nouveau_framebuffer *fb;
  440. int ret;
  441. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  442. /* no fb bound */
  443. if (!atomic && !crtc->fb) {
  444. NV_DEBUG_KMS(dev, "No FB bound\n");
  445. return 0;
  446. }
  447. /* If atomic, we want to switch to the fb we were passed, so
  448. * now we update pointers to do that. (We don't pin; just
  449. * assume we're already pinned and update the base address.)
  450. */
  451. if (atomic) {
  452. drm_fb = passed_fb;
  453. fb = nouveau_framebuffer(passed_fb);
  454. } else {
  455. drm_fb = crtc->fb;
  456. fb = nouveau_framebuffer(crtc->fb);
  457. /* If not atomic, we can go ahead and pin, and unpin the
  458. * old fb we were passed.
  459. */
  460. ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
  461. if (ret)
  462. return ret;
  463. if (passed_fb) {
  464. struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
  465. nouveau_bo_unpin(ofb->nvbo);
  466. }
  467. }
  468. nv_crtc->fb.offset = fb->nvbo->bo.offset;
  469. nv_crtc->fb.tile_flags = nouveau_bo_tile_layout(fb->nvbo);
  470. nv_crtc->fb.cpp = drm_fb->bits_per_pixel / 8;
  471. if (!nv_crtc->fb.blanked && dev_priv->chipset != 0x50) {
  472. ret = RING_SPACE(evo, 2);
  473. if (ret)
  474. return ret;
  475. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_DMA), 1);
  476. OUT_RING (evo, fb->r_dma);
  477. }
  478. ret = RING_SPACE(evo, 12);
  479. if (ret)
  480. return ret;
  481. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_OFFSET), 5);
  482. OUT_RING (evo, nv_crtc->fb.offset >> 8);
  483. OUT_RING (evo, 0);
  484. OUT_RING (evo, (drm_fb->height << 16) | drm_fb->width);
  485. OUT_RING (evo, fb->r_pitch);
  486. OUT_RING (evo, fb->r_format);
  487. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLUT_MODE), 1);
  488. OUT_RING (evo, fb->base.depth == 8 ?
  489. NV50_EVO_CRTC_CLUT_MODE_OFF : NV50_EVO_CRTC_CLUT_MODE_ON);
  490. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, COLOR_CTRL), 1);
  491. OUT_RING (evo, NV50_EVO_CRTC_COLOR_CTRL_COLOR);
  492. BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_POS), 1);
  493. OUT_RING (evo, (y << 16) | x);
  494. if (nv_crtc->lut.depth != fb->base.depth) {
  495. nv_crtc->lut.depth = fb->base.depth;
  496. nv50_crtc_lut_load(crtc);
  497. }
  498. return 0;
  499. }
  500. static int
  501. nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
  502. struct drm_display_mode *mode, int x, int y,
  503. struct drm_framebuffer *old_fb)
  504. {
  505. struct drm_device *dev = crtc->dev;
  506. struct nouveau_channel *evo = nv50_display(dev)->master;
  507. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  508. u32 head = nv_crtc->index * 0x400;
  509. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  510. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  511. u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
  512. u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
  513. u32 vblan2e = 0, vblan2s = 1;
  514. int ret;
  515. /* hw timing description looks like this:
  516. *
  517. * <sync> <back porch> <---------display---------> <front porch>
  518. * ______
  519. * |____________|---------------------------|____________|
  520. *
  521. * ^ synce ^ blanke ^ blanks ^ active
  522. *
  523. * interlaced modes also have 2 additional values pointing at the end
  524. * and start of the next field's blanking period.
  525. */
  526. hactive = mode->htotal;
  527. hsynce = mode->hsync_end - mode->hsync_start - 1;
  528. hbackp = mode->htotal - mode->hsync_end;
  529. hblanke = hsynce + hbackp;
  530. hfrontp = mode->hsync_start - mode->hdisplay;
  531. hblanks = mode->htotal - hfrontp - 1;
  532. vactive = mode->vtotal * vscan / ilace;
  533. vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  534. vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  535. vblanke = vsynce + vbackp;
  536. vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  537. vblanks = vactive - vfrontp - 1;
  538. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  539. vblan2e = vactive + vsynce + vbackp;
  540. vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
  541. vactive = (vactive * 2) + 1;
  542. }
  543. ret = RING_SPACE(evo, 18);
  544. if (ret == 0) {
  545. BEGIN_RING(evo, 0, 0x0804 + head, 2);
  546. OUT_RING (evo, 0x00800000 | mode->clock);
  547. OUT_RING (evo, (ilace == 2) ? 2 : 0);
  548. BEGIN_RING(evo, 0, 0x0810 + head, 6);
  549. OUT_RING (evo, 0x00000000); /* border colour */
  550. OUT_RING (evo, (vactive << 16) | hactive);
  551. OUT_RING (evo, ( vsynce << 16) | hsynce);
  552. OUT_RING (evo, (vblanke << 16) | hblanke);
  553. OUT_RING (evo, (vblanks << 16) | hblanks);
  554. OUT_RING (evo, (vblan2e << 16) | vblan2s);
  555. BEGIN_RING(evo, 0, 0x082c + head, 1);
  556. OUT_RING (evo, 0x00000000);
  557. BEGIN_RING(evo, 0, 0x0900 + head, 1);
  558. OUT_RING (evo, 0x00000311); /* makes sync channel work */
  559. BEGIN_RING(evo, 0, 0x08c8 + head, 1);
  560. OUT_RING (evo, (umode->vdisplay << 16) | umode->hdisplay);
  561. BEGIN_RING(evo, 0, 0x08d4 + head, 1);
  562. OUT_RING (evo, 0x00000000); /* screen position */
  563. }
  564. nv_crtc->set_dither(nv_crtc, false);
  565. nv_crtc->set_scale(nv_crtc, false);
  566. return nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
  567. }
  568. static int
  569. nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  570. struct drm_framebuffer *old_fb)
  571. {
  572. int ret;
  573. nv50_display_flip_stop(crtc);
  574. ret = nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
  575. if (ret)
  576. return ret;
  577. ret = nv50_display_sync(crtc->dev);
  578. if (ret)
  579. return ret;
  580. return nv50_display_flip_next(crtc, crtc->fb, NULL);
  581. }
  582. static int
  583. nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  584. struct drm_framebuffer *fb,
  585. int x, int y, enum mode_set_atomic state)
  586. {
  587. int ret;
  588. nv50_display_flip_stop(crtc);
  589. ret = nv50_crtc_do_mode_set_base(crtc, fb, x, y, true);
  590. if (ret)
  591. return ret;
  592. return nv50_display_sync(crtc->dev);
  593. }
  594. static const struct drm_crtc_helper_funcs nv50_crtc_helper_funcs = {
  595. .dpms = nv50_crtc_dpms,
  596. .prepare = nv50_crtc_prepare,
  597. .commit = nv50_crtc_commit,
  598. .mode_fixup = nv50_crtc_mode_fixup,
  599. .mode_set = nv50_crtc_mode_set,
  600. .mode_set_base = nv50_crtc_mode_set_base,
  601. .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
  602. .load_lut = nv50_crtc_lut_load,
  603. };
  604. int
  605. nv50_crtc_create(struct drm_device *dev, int index)
  606. {
  607. struct nouveau_crtc *nv_crtc = NULL;
  608. int ret, i;
  609. NV_DEBUG_KMS(dev, "\n");
  610. nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
  611. if (!nv_crtc)
  612. return -ENOMEM;
  613. /* Default CLUT parameters, will be activated on the hw upon
  614. * first mode set.
  615. */
  616. for (i = 0; i < 256; i++) {
  617. nv_crtc->lut.r[i] = i << 8;
  618. nv_crtc->lut.g[i] = i << 8;
  619. nv_crtc->lut.b[i] = i << 8;
  620. }
  621. nv_crtc->lut.depth = 0;
  622. ret = nouveau_bo_new(dev, 4096, 0x100, TTM_PL_FLAG_VRAM,
  623. 0, 0x0000, &nv_crtc->lut.nvbo);
  624. if (!ret) {
  625. ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
  626. if (!ret)
  627. ret = nouveau_bo_map(nv_crtc->lut.nvbo);
  628. if (ret)
  629. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  630. }
  631. if (ret) {
  632. kfree(nv_crtc);
  633. return ret;
  634. }
  635. nv_crtc->index = index;
  636. /* set function pointers */
  637. nv_crtc->set_dither = nv50_crtc_set_dither;
  638. nv_crtc->set_scale = nv50_crtc_set_scale;
  639. drm_crtc_init(dev, &nv_crtc->base, &nv50_crtc_funcs);
  640. drm_crtc_helper_add(&nv_crtc->base, &nv50_crtc_helper_funcs);
  641. drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
  642. ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
  643. 0, 0x0000, &nv_crtc->cursor.nvbo);
  644. if (!ret) {
  645. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  646. if (!ret)
  647. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  648. if (ret)
  649. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  650. }
  651. nv50_cursor_init(nv_crtc);
  652. return 0;
  653. }