nv50_crtc.c 20 KB

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