nv50_crtc.c 21 KB

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