nv50_crtc.c 22 KB

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