nv50_crtc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. #include "nouveau_reg.h"
  29. #include "nouveau_drm.h"
  30. #include "nouveau_dma.h"
  31. #include "nouveau_gem.h"
  32. #include "nouveau_hw.h"
  33. #include "nouveau_encoder.h"
  34. #include "nouveau_crtc.h"
  35. #include "nouveau_connector.h"
  36. #include "nv50_display.h"
  37. #include <subdev/clock.h>
  38. static void
  39. nv50_crtc_lut_load(struct drm_crtc *crtc)
  40. {
  41. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  42. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  43. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  44. int i;
  45. NV_DEBUG(drm, "\n");
  46. for (i = 0; i < 256; i++) {
  47. writew(nv_crtc->lut.r[i] >> 2, lut + 8*i + 0);
  48. writew(nv_crtc->lut.g[i] >> 2, lut + 8*i + 2);
  49. writew(nv_crtc->lut.b[i] >> 2, lut + 8*i + 4);
  50. }
  51. if (nv_crtc->lut.depth == 30) {
  52. writew(nv_crtc->lut.r[i - 1] >> 2, lut + 8*i + 0);
  53. writew(nv_crtc->lut.g[i - 1] >> 2, lut + 8*i + 2);
  54. writew(nv_crtc->lut.b[i - 1] >> 2, lut + 8*i + 4);
  55. }
  56. }
  57. int
  58. nv50_crtc_blank(struct nouveau_crtc *nv_crtc, bool blanked)
  59. {
  60. struct drm_device *dev = nv_crtc->base.dev;
  61. struct nouveau_drm *drm = nouveau_drm(dev);
  62. struct nouveau_channel *evo = nv50_display(dev)->master;
  63. int index = nv_crtc->index, ret;
  64. NV_DEBUG(drm, "index %d\n", nv_crtc->index);
  65. NV_DEBUG(drm, "%s\n", blanked ? "blanked" : "unblanked");
  66. if (blanked) {
  67. nv_crtc->cursor.hide(nv_crtc, false);
  68. ret = RING_SPACE(evo, nv_device(drm->device)->chipset != 0x50 ? 7 : 5);
  69. if (ret) {
  70. NV_ERROR(drm, "no space while blanking crtc\n");
  71. return ret;
  72. }
  73. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(index, CLUT_MODE), 2);
  74. OUT_RING(evo, NV50_EVO_CRTC_CLUT_MODE_BLANK);
  75. OUT_RING(evo, 0);
  76. if (nv_device(drm->device)->chipset != 0x50) {
  77. BEGIN_NV04(evo, 0, NV84_EVO_CRTC(index, CLUT_DMA), 1);
  78. OUT_RING(evo, NV84_EVO_CRTC_CLUT_DMA_HANDLE_NONE);
  79. }
  80. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(index, FB_DMA), 1);
  81. OUT_RING(evo, NV50_EVO_CRTC_FB_DMA_HANDLE_NONE);
  82. } else {
  83. if (nv_crtc->cursor.visible)
  84. nv_crtc->cursor.show(nv_crtc, false);
  85. else
  86. nv_crtc->cursor.hide(nv_crtc, false);
  87. ret = RING_SPACE(evo, nv_device(drm->device)->chipset != 0x50 ? 10 : 8);
  88. if (ret) {
  89. NV_ERROR(drm, "no space while unblanking crtc\n");
  90. return ret;
  91. }
  92. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(index, CLUT_MODE), 2);
  93. OUT_RING(evo, nv_crtc->lut.depth == 8 ?
  94. NV50_EVO_CRTC_CLUT_MODE_OFF :
  95. NV50_EVO_CRTC_CLUT_MODE_ON);
  96. OUT_RING(evo, nv_crtc->lut.nvbo->bo.offset >> 8);
  97. if (nv_device(drm->device)->chipset != 0x50) {
  98. BEGIN_NV04(evo, 0, NV84_EVO_CRTC(index, CLUT_DMA), 1);
  99. OUT_RING(evo, NvEvoVRAM);
  100. }
  101. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(index, FB_OFFSET), 2);
  102. OUT_RING(evo, nv_crtc->fb.offset >> 8);
  103. OUT_RING(evo, 0);
  104. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(index, FB_DMA), 1);
  105. if (nv_device(drm->device)->chipset != 0x50)
  106. if (nv_crtc->fb.tile_flags == 0x7a00 ||
  107. nv_crtc->fb.tile_flags == 0xfe00)
  108. OUT_RING(evo, NvEvoFB32);
  109. else
  110. if (nv_crtc->fb.tile_flags == 0x7000)
  111. OUT_RING(evo, NvEvoFB16);
  112. else
  113. OUT_RING(evo, NvEvoVRAM_LP);
  114. else
  115. OUT_RING(evo, NvEvoVRAM_LP);
  116. }
  117. nv_crtc->fb.blanked = blanked;
  118. return 0;
  119. }
  120. static int
  121. nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
  122. {
  123. struct nouveau_channel *evo = nv50_display(nv_crtc->base.dev)->master;
  124. struct nouveau_connector *nv_connector;
  125. struct drm_connector *connector;
  126. int head = nv_crtc->index, ret;
  127. u32 mode = 0x00;
  128. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  129. connector = &nv_connector->base;
  130. if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
  131. if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
  132. mode = DITHERING_MODE_DYNAMIC2X2;
  133. } else {
  134. mode = nv_connector->dithering_mode;
  135. }
  136. if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
  137. if (connector->display_info.bpc >= 8)
  138. mode |= DITHERING_DEPTH_8BPC;
  139. } else {
  140. mode |= nv_connector->dithering_depth;
  141. }
  142. ret = RING_SPACE(evo, 2 + (update ? 2 : 0));
  143. if (ret == 0) {
  144. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(head, DITHER_CTRL), 1);
  145. OUT_RING (evo, mode);
  146. if (update) {
  147. BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
  148. OUT_RING (evo, 0);
  149. FIRE_RING (evo);
  150. }
  151. }
  152. return ret;
  153. }
  154. static int
  155. nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
  156. {
  157. struct drm_device *dev = nv_crtc->base.dev;
  158. struct nouveau_drm *drm = nouveau_drm(dev);
  159. struct nouveau_channel *evo = nv50_display(dev)->master;
  160. int ret;
  161. int adj;
  162. u32 hue, vib;
  163. NV_DEBUG(drm, "vibrance = %i, hue = %i\n",
  164. nv_crtc->color_vibrance, nv_crtc->vibrant_hue);
  165. ret = RING_SPACE(evo, 2 + (update ? 2 : 0));
  166. if (ret) {
  167. NV_ERROR(drm, "no space while setting color vibrance\n");
  168. return ret;
  169. }
  170. adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
  171. vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
  172. hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
  173. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(nv_crtc->index, COLOR_CTRL), 1);
  174. OUT_RING (evo, (hue << 20) | (vib << 8));
  175. if (update) {
  176. BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
  177. OUT_RING (evo, 0);
  178. FIRE_RING (evo);
  179. }
  180. return 0;
  181. }
  182. struct nouveau_connector *
  183. nouveau_crtc_connector_get(struct nouveau_crtc *nv_crtc)
  184. {
  185. struct drm_device *dev = nv_crtc->base.dev;
  186. struct drm_connector *connector;
  187. struct drm_crtc *crtc = to_drm_crtc(nv_crtc);
  188. /* The safest approach is to find an encoder with the right crtc, that
  189. * is also linked to a connector. */
  190. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  191. if (connector->encoder)
  192. if (connector->encoder->crtc == crtc)
  193. return nouveau_connector(connector);
  194. }
  195. return NULL;
  196. }
  197. static int
  198. nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  199. {
  200. struct nouveau_connector *nv_connector;
  201. struct drm_crtc *crtc = &nv_crtc->base;
  202. struct drm_device *dev = crtc->dev;
  203. struct nouveau_drm *drm = nouveau_drm(dev);
  204. struct nouveau_channel *evo = nv50_display(dev)->master;
  205. struct drm_display_mode *umode = &crtc->mode;
  206. struct drm_display_mode *omode;
  207. int scaling_mode, ret;
  208. u32 ctrl = 0, oX, oY;
  209. NV_DEBUG(drm, "\n");
  210. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  211. if (!nv_connector || !nv_connector->native_mode) {
  212. NV_ERROR(drm, "no native mode, forcing panel scaling\n");
  213. scaling_mode = DRM_MODE_SCALE_NONE;
  214. } else {
  215. scaling_mode = nv_connector->scaling_mode;
  216. }
  217. /* start off at the resolution we programmed the crtc for, this
  218. * effectively handles NONE/FULL scaling
  219. */
  220. if (scaling_mode != DRM_MODE_SCALE_NONE)
  221. omode = nv_connector->native_mode;
  222. else
  223. omode = umode;
  224. oX = omode->hdisplay;
  225. oY = omode->vdisplay;
  226. if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
  227. oY *= 2;
  228. /* add overscan compensation if necessary, will keep the aspect
  229. * ratio the same as the backend mode unless overridden by the
  230. * user setting both hborder and vborder properties.
  231. */
  232. if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
  233. (nv_connector->underscan == UNDERSCAN_AUTO &&
  234. nv_connector->edid &&
  235. drm_detect_hdmi_monitor(nv_connector->edid)))) {
  236. u32 bX = nv_connector->underscan_hborder;
  237. u32 bY = nv_connector->underscan_vborder;
  238. u32 aspect = (oY << 19) / oX;
  239. if (bX) {
  240. oX -= (bX * 2);
  241. if (bY) oY -= (bY * 2);
  242. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  243. } else {
  244. oX -= (oX >> 4) + 32;
  245. if (bY) oY -= (bY * 2);
  246. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  247. }
  248. }
  249. /* handle CENTER/ASPECT scaling, taking into account the areas
  250. * removed already for overscan compensation
  251. */
  252. switch (scaling_mode) {
  253. case DRM_MODE_SCALE_CENTER:
  254. oX = min((u32)umode->hdisplay, oX);
  255. oY = min((u32)umode->vdisplay, oY);
  256. /* fall-through */
  257. case DRM_MODE_SCALE_ASPECT:
  258. if (oY < oX) {
  259. u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
  260. oX = ((oY * aspect) + (aspect / 2)) >> 19;
  261. } else {
  262. u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
  263. oY = ((oX * aspect) + (aspect / 2)) >> 19;
  264. }
  265. break;
  266. default:
  267. break;
  268. }
  269. if (umode->hdisplay != oX || umode->vdisplay != oY ||
  270. umode->flags & DRM_MODE_FLAG_INTERLACE ||
  271. umode->flags & DRM_MODE_FLAG_DBLSCAN)
  272. ctrl |= NV50_EVO_CRTC_SCALE_CTRL_ACTIVE;
  273. ret = RING_SPACE(evo, 5);
  274. if (ret)
  275. return ret;
  276. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CTRL), 1);
  277. OUT_RING (evo, ctrl);
  278. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_RES1), 2);
  279. OUT_RING (evo, oY << 16 | oX);
  280. OUT_RING (evo, oY << 16 | oX);
  281. if (update) {
  282. nv50_display_flip_stop(crtc);
  283. nv50_display_sync(dev);
  284. nv50_display_flip_next(crtc, crtc->fb, NULL);
  285. }
  286. return 0;
  287. }
  288. int
  289. nv50_crtc_set_clock(struct drm_device *dev, int head, int pclk)
  290. {
  291. struct nouveau_device *device = nouveau_dev(dev);
  292. struct nouveau_clock *clk = nouveau_clock(device);
  293. return clk->pll_set(clk, PLL_VPLL0 + head, pclk);
  294. }
  295. static void
  296. nv50_crtc_destroy(struct drm_crtc *crtc)
  297. {
  298. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  299. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  300. NV_DEBUG(drm, "\n");
  301. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  302. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  303. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  304. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  305. drm_crtc_cleanup(&nv_crtc->base);
  306. kfree(nv_crtc);
  307. }
  308. int
  309. nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  310. uint32_t buffer_handle, uint32_t width, uint32_t height)
  311. {
  312. struct drm_device *dev = crtc->dev;
  313. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  314. struct nouveau_bo *cursor = NULL;
  315. struct drm_gem_object *gem;
  316. int ret = 0, i;
  317. if (!buffer_handle) {
  318. nv_crtc->cursor.hide(nv_crtc, true);
  319. return 0;
  320. }
  321. if (width != 64 || height != 64)
  322. return -EINVAL;
  323. gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
  324. if (!gem)
  325. return -ENOENT;
  326. cursor = nouveau_gem_object(gem);
  327. ret = nouveau_bo_map(cursor);
  328. if (ret)
  329. goto out;
  330. /* The simple will do for now. */
  331. for (i = 0; i < 64 * 64; i++)
  332. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, nouveau_bo_rd32(cursor, i));
  333. nouveau_bo_unmap(cursor);
  334. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  335. nv_crtc->cursor.show(nv_crtc, true);
  336. out:
  337. drm_gem_object_unreference_unlocked(gem);
  338. return ret;
  339. }
  340. int
  341. nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  342. {
  343. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  344. nv_crtc->cursor.set_pos(nv_crtc, x, y);
  345. return 0;
  346. }
  347. static void
  348. nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  349. uint32_t start, uint32_t size)
  350. {
  351. int end = (start + size > 256) ? 256 : start + size, i;
  352. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  353. for (i = start; i < end; i++) {
  354. nv_crtc->lut.r[i] = r[i];
  355. nv_crtc->lut.g[i] = g[i];
  356. nv_crtc->lut.b[i] = b[i];
  357. }
  358. /* We need to know the depth before we upload, but it's possible to
  359. * get called before a framebuffer is bound. If this is the case,
  360. * mark the lut values as dirty by setting depth==0, and it'll be
  361. * uploaded on the first mode_set_base()
  362. */
  363. if (!nv_crtc->base.fb) {
  364. nv_crtc->lut.depth = 0;
  365. return;
  366. }
  367. nv50_crtc_lut_load(crtc);
  368. }
  369. static void
  370. nv50_crtc_save(struct drm_crtc *crtc)
  371. {
  372. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  373. NV_ERROR(drm, "!!\n");
  374. }
  375. static void
  376. nv50_crtc_restore(struct drm_crtc *crtc)
  377. {
  378. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  379. NV_ERROR(drm, "!!\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. struct nouveau_drm *drm = nouveau_drm(dev);
  401. NV_DEBUG(drm, "index %d\n", nv_crtc->index);
  402. nv50_display_flip_stop(crtc);
  403. drm_vblank_pre_modeset(dev, nv_crtc->index);
  404. nv50_crtc_blank(nv_crtc, true);
  405. }
  406. static void
  407. nv50_crtc_commit(struct drm_crtc *crtc)
  408. {
  409. struct drm_device *dev = crtc->dev;
  410. struct nouveau_drm *drm = nouveau_drm(dev);
  411. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  412. NV_DEBUG(drm, "index %d\n", nv_crtc->index);
  413. nv50_crtc_blank(nv_crtc, false);
  414. drm_vblank_post_modeset(dev, nv_crtc->index);
  415. nv50_display_sync(dev);
  416. nv50_display_flip_next(crtc, crtc->fb, NULL);
  417. }
  418. static bool
  419. nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
  420. struct drm_display_mode *adjusted_mode)
  421. {
  422. return true;
  423. }
  424. static int
  425. nv50_crtc_do_mode_set_base(struct drm_crtc *crtc,
  426. struct drm_framebuffer *passed_fb,
  427. int x, int y, bool atomic)
  428. {
  429. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  430. struct drm_device *dev = nv_crtc->base.dev;
  431. struct nouveau_drm *drm = nouveau_drm(dev);
  432. struct nouveau_channel *evo = nv50_display(dev)->master;
  433. struct drm_framebuffer *drm_fb;
  434. struct nouveau_framebuffer *fb;
  435. int ret;
  436. NV_DEBUG(drm, "index %d\n", nv_crtc->index);
  437. /* no fb bound */
  438. if (!atomic && !crtc->fb) {
  439. NV_DEBUG(drm, "No FB bound\n");
  440. return 0;
  441. }
  442. /* If atomic, we want to switch to the fb we were passed, so
  443. * now we update pointers to do that. (We don't pin; just
  444. * assume we're already pinned and update the base address.)
  445. */
  446. if (atomic) {
  447. drm_fb = passed_fb;
  448. fb = nouveau_framebuffer(passed_fb);
  449. } else {
  450. drm_fb = crtc->fb;
  451. fb = nouveau_framebuffer(crtc->fb);
  452. /* If not atomic, we can go ahead and pin, and unpin the
  453. * old fb we were passed.
  454. */
  455. ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
  456. if (ret)
  457. return ret;
  458. if (passed_fb) {
  459. struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
  460. nouveau_bo_unpin(ofb->nvbo);
  461. }
  462. }
  463. nv_crtc->fb.offset = fb->nvbo->bo.offset;
  464. nv_crtc->fb.tile_flags = nouveau_bo_tile_layout(fb->nvbo);
  465. nv_crtc->fb.cpp = drm_fb->bits_per_pixel / 8;
  466. if (!nv_crtc->fb.blanked && nv_device(drm->device)->chipset != 0x50) {
  467. ret = RING_SPACE(evo, 2);
  468. if (ret)
  469. return ret;
  470. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_DMA), 1);
  471. OUT_RING (evo, fb->r_dma);
  472. }
  473. ret = RING_SPACE(evo, 12);
  474. if (ret)
  475. return ret;
  476. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_OFFSET), 5);
  477. OUT_RING (evo, nv_crtc->fb.offset >> 8);
  478. OUT_RING (evo, 0);
  479. OUT_RING (evo, (drm_fb->height << 16) | drm_fb->width);
  480. OUT_RING (evo, fb->r_pitch);
  481. OUT_RING (evo, fb->r_format);
  482. BEGIN_NV04(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLUT_MODE), 1);
  483. OUT_RING (evo, fb->base.depth == 8 ?
  484. NV50_EVO_CRTC_CLUT_MODE_OFF : NV50_EVO_CRTC_CLUT_MODE_ON);
  485. BEGIN_NV04(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 *umode,
  495. struct drm_display_mode *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. u32 head = nv_crtc->index * 0x400;
  502. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  503. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  504. u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
  505. u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
  506. u32 vblan2e = 0, vblan2s = 1;
  507. int ret;
  508. /* hw timing description looks like this:
  509. *
  510. * <sync> <back porch> <---------display---------> <front porch>
  511. * ______
  512. * |____________|---------------------------|____________|
  513. *
  514. * ^ synce ^ blanke ^ blanks ^ active
  515. *
  516. * interlaced modes also have 2 additional values pointing at the end
  517. * and start of the next field's blanking period.
  518. */
  519. hactive = mode->htotal;
  520. hsynce = mode->hsync_end - mode->hsync_start - 1;
  521. hbackp = mode->htotal - mode->hsync_end;
  522. hblanke = hsynce + hbackp;
  523. hfrontp = mode->hsync_start - mode->hdisplay;
  524. hblanks = mode->htotal - hfrontp - 1;
  525. vactive = mode->vtotal * vscan / ilace;
  526. vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  527. vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  528. vblanke = vsynce + vbackp;
  529. vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  530. vblanks = vactive - vfrontp - 1;
  531. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  532. vblan2e = vactive + vsynce + vbackp;
  533. vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
  534. vactive = (vactive * 2) + 1;
  535. }
  536. ret = RING_SPACE(evo, 18);
  537. if (ret == 0) {
  538. BEGIN_NV04(evo, 0, 0x0804 + head, 2);
  539. OUT_RING (evo, 0x00800000 | mode->clock);
  540. OUT_RING (evo, (ilace == 2) ? 2 : 0);
  541. BEGIN_NV04(evo, 0, 0x0810 + head, 6);
  542. OUT_RING (evo, 0x00000000); /* border colour */
  543. OUT_RING (evo, (vactive << 16) | hactive);
  544. OUT_RING (evo, ( vsynce << 16) | hsynce);
  545. OUT_RING (evo, (vblanke << 16) | hblanke);
  546. OUT_RING (evo, (vblanks << 16) | hblanks);
  547. OUT_RING (evo, (vblan2e << 16) | vblan2s);
  548. BEGIN_NV04(evo, 0, 0x082c + head, 1);
  549. OUT_RING (evo, 0x00000000);
  550. BEGIN_NV04(evo, 0, 0x0900 + head, 1);
  551. OUT_RING (evo, 0x00000311); /* makes sync channel work */
  552. BEGIN_NV04(evo, 0, 0x08c8 + head, 1);
  553. OUT_RING (evo, (umode->vdisplay << 16) | umode->hdisplay);
  554. BEGIN_NV04(evo, 0, 0x08d4 + head, 1);
  555. OUT_RING (evo, 0x00000000); /* screen position */
  556. }
  557. nv_crtc->set_dither(nv_crtc, false);
  558. nv_crtc->set_scale(nv_crtc, false);
  559. nv_crtc->set_color_vibrance(nv_crtc, false);
  560. return nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
  561. }
  562. static int
  563. nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  564. struct drm_framebuffer *old_fb)
  565. {
  566. int ret;
  567. nv50_display_flip_stop(crtc);
  568. ret = nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
  569. if (ret)
  570. return ret;
  571. ret = nv50_display_sync(crtc->dev);
  572. if (ret)
  573. return ret;
  574. return nv50_display_flip_next(crtc, crtc->fb, NULL);
  575. }
  576. static int
  577. nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  578. struct drm_framebuffer *fb,
  579. int x, int y, enum mode_set_atomic state)
  580. {
  581. int ret;
  582. nv50_display_flip_stop(crtc);
  583. ret = nv50_crtc_do_mode_set_base(crtc, fb, x, y, true);
  584. if (ret)
  585. return ret;
  586. return nv50_display_sync(crtc->dev);
  587. }
  588. static const struct drm_crtc_helper_funcs nv50_crtc_helper_funcs = {
  589. .dpms = nv50_crtc_dpms,
  590. .prepare = nv50_crtc_prepare,
  591. .commit = nv50_crtc_commit,
  592. .mode_fixup = nv50_crtc_mode_fixup,
  593. .mode_set = nv50_crtc_mode_set,
  594. .mode_set_base = nv50_crtc_mode_set_base,
  595. .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
  596. .load_lut = nv50_crtc_lut_load,
  597. };
  598. int
  599. nv50_crtc_create(struct drm_device *dev, int index)
  600. {
  601. struct nouveau_drm *drm = nouveau_drm(dev);
  602. struct nouveau_crtc *nv_crtc = NULL;
  603. int ret, i;
  604. NV_DEBUG(drm, "\n");
  605. nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
  606. if (!nv_crtc)
  607. return -ENOMEM;
  608. nv_crtc->index = index;
  609. nv_crtc->set_dither = nv50_crtc_set_dither;
  610. nv_crtc->set_scale = nv50_crtc_set_scale;
  611. nv_crtc->set_color_vibrance = nv50_crtc_set_color_vibrance;
  612. nv_crtc->color_vibrance = 50;
  613. nv_crtc->vibrant_hue = 0;
  614. nv_crtc->lut.depth = 0;
  615. for (i = 0; i < 256; i++) {
  616. nv_crtc->lut.r[i] = i << 8;
  617. nv_crtc->lut.g[i] = i << 8;
  618. nv_crtc->lut.b[i] = i << 8;
  619. }
  620. drm_crtc_init(dev, &nv_crtc->base, &nv50_crtc_funcs);
  621. drm_crtc_helper_add(&nv_crtc->base, &nv50_crtc_helper_funcs);
  622. drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
  623. ret = nouveau_bo_new(dev, 4096, 0x100, TTM_PL_FLAG_VRAM,
  624. 0, 0x0000, NULL, &nv_crtc->lut.nvbo);
  625. if (!ret) {
  626. ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
  627. if (!ret)
  628. ret = nouveau_bo_map(nv_crtc->lut.nvbo);
  629. if (ret)
  630. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  631. }
  632. if (ret)
  633. goto out;
  634. ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
  635. 0, 0x0000, NULL, &nv_crtc->cursor.nvbo);
  636. if (!ret) {
  637. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  638. if (!ret)
  639. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  640. if (ret)
  641. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  642. }
  643. if (ret)
  644. goto out;
  645. nv50_cursor_init(nv_crtc);
  646. out:
  647. if (ret)
  648. nv50_crtc_destroy(&nv_crtc->base);
  649. return ret;
  650. }