nvd0_display.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*
  2. * Copyright 2011 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include "drmP.h"
  26. #include "drm_crtc_helper.h"
  27. #include "nouveau_drv.h"
  28. #include "nouveau_connector.h"
  29. #include "nouveau_encoder.h"
  30. #include "nouveau_crtc.h"
  31. #include "nouveau_dma.h"
  32. #include "nouveau_fb.h"
  33. #include "nv50_display.h"
  34. struct nvd0_display {
  35. struct nouveau_gpuobj *mem;
  36. struct {
  37. dma_addr_t handle;
  38. u32 *ptr;
  39. } evo[1];
  40. struct tasklet_struct tasklet;
  41. u32 modeset;
  42. };
  43. static struct nvd0_display *
  44. nvd0_display(struct drm_device *dev)
  45. {
  46. struct drm_nouveau_private *dev_priv = dev->dev_private;
  47. return dev_priv->engine.display.priv;
  48. }
  49. static inline int
  50. evo_icmd(struct drm_device *dev, int id, u32 mthd, u32 data)
  51. {
  52. int ret = 0;
  53. nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000001);
  54. nv_wr32(dev, 0x610704 + (id * 0x10), data);
  55. nv_mask(dev, 0x610704 + (id * 0x10), 0x80000ffc, 0x80000000 | mthd);
  56. if (!nv_wait(dev, 0x610704 + (id * 0x10), 0x80000000, 0x00000000))
  57. ret = -EBUSY;
  58. nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000000);
  59. return ret;
  60. }
  61. static u32 *
  62. evo_wait(struct drm_device *dev, int id, int nr)
  63. {
  64. struct nvd0_display *disp = nvd0_display(dev);
  65. u32 put = nv_rd32(dev, 0x640000 + (id * 0x1000)) / 4;
  66. if (put + nr >= (PAGE_SIZE / 4)) {
  67. disp->evo[id].ptr[put] = 0x20000000;
  68. nv_wr32(dev, 0x640000 + (id * 0x1000), 0x00000000);
  69. if (!nv_wait(dev, 0x640004 + (id * 0x1000), ~0, 0x00000000)) {
  70. NV_ERROR(dev, "evo %d dma stalled\n", id);
  71. return NULL;
  72. }
  73. put = 0;
  74. }
  75. return disp->evo[id].ptr + put;
  76. }
  77. static void
  78. evo_kick(u32 *push, struct drm_device *dev, int id)
  79. {
  80. struct nvd0_display *disp = nvd0_display(dev);
  81. nv_wr32(dev, 0x640000 + (id * 0x1000), (push - disp->evo[id].ptr) << 2);
  82. }
  83. #define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
  84. #define evo_data(p,d) *((p)++) = (d)
  85. static struct drm_crtc *
  86. nvd0_display_crtc_get(struct drm_encoder *encoder)
  87. {
  88. return nouveau_encoder(encoder)->crtc;
  89. }
  90. /******************************************************************************
  91. * CRTC
  92. *****************************************************************************/
  93. static int
  94. nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
  95. {
  96. struct drm_device *dev = nv_crtc->base.dev;
  97. struct nouveau_connector *nv_connector;
  98. struct drm_connector *connector;
  99. u32 *push, mode = 0x00;
  100. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  101. connector = &nv_connector->base;
  102. if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
  103. if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
  104. mode = DITHERING_MODE_DYNAMIC2X2;
  105. } else {
  106. mode = nv_connector->dithering_mode;
  107. }
  108. if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
  109. if (connector->display_info.bpc >= 8)
  110. mode |= DITHERING_DEPTH_8BPC;
  111. } else {
  112. mode |= nv_connector->dithering_depth;
  113. }
  114. push = evo_wait(dev, 0, 4);
  115. if (push) {
  116. evo_mthd(push, 0x0490 + (nv_crtc->index * 0x300), 1);
  117. evo_data(push, mode);
  118. if (update) {
  119. evo_mthd(push, 0x0080, 1);
  120. evo_data(push, 0x00000000);
  121. }
  122. evo_kick(push, dev, 0);
  123. }
  124. return 0;
  125. }
  126. static int
  127. nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  128. {
  129. struct drm_display_mode *mode = &nv_crtc->base.mode;
  130. struct drm_device *dev = nv_crtc->base.dev;
  131. struct nouveau_connector *nv_connector;
  132. u32 *push, outX, outY;
  133. outX = mode->hdisplay;
  134. outY = mode->vdisplay;
  135. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  136. if (nv_connector && nv_connector->native_mode) {
  137. struct drm_display_mode *native = nv_connector->native_mode;
  138. u32 xratio = (native->hdisplay << 19) / mode->hdisplay;
  139. u32 yratio = (native->vdisplay << 19) / mode->vdisplay;
  140. switch (nv_connector->scaling_mode) {
  141. case DRM_MODE_SCALE_ASPECT:
  142. if (xratio > yratio) {
  143. outX = (mode->hdisplay * yratio) >> 19;
  144. outY = (mode->vdisplay * yratio) >> 19;
  145. } else {
  146. outX = (mode->hdisplay * xratio) >> 19;
  147. outY = (mode->vdisplay * xratio) >> 19;
  148. }
  149. break;
  150. case DRM_MODE_SCALE_FULLSCREEN:
  151. outX = native->hdisplay;
  152. outY = native->vdisplay;
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. push = evo_wait(dev, 0, 16);
  159. if (push) {
  160. evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
  161. evo_data(push, (outY << 16) | outX);
  162. evo_data(push, (outY << 16) | outX);
  163. evo_data(push, (outY << 16) | outX);
  164. evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
  165. evo_data(push, 0x00000000);
  166. evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
  167. evo_data(push, (mode->vdisplay << 16) | mode->hdisplay);
  168. if (update) {
  169. evo_mthd(push, 0x0080, 1);
  170. evo_data(push, 0x00000000);
  171. }
  172. evo_kick(push, dev, 0);
  173. }
  174. return 0;
  175. }
  176. static int
  177. nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
  178. int x, int y, bool update)
  179. {
  180. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
  181. u32 *push;
  182. push = evo_wait(fb->dev, 0, 16);
  183. if (push) {
  184. evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
  185. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  186. evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
  187. evo_data(push, (fb->height << 16) | fb->width);
  188. evo_data(push, nvfb->r_pitch);
  189. evo_data(push, nvfb->r_format);
  190. evo_data(push, nvfb->r_dma);
  191. evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
  192. evo_data(push, (y << 16) | x);
  193. if (update) {
  194. evo_mthd(push, 0x0080, 1);
  195. evo_data(push, 0x00000000);
  196. }
  197. evo_kick(push, fb->dev, 0);
  198. }
  199. nv_crtc->fb.tile_flags = nvfb->r_dma;
  200. return 0;
  201. }
  202. static void
  203. nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc, bool show, bool update)
  204. {
  205. struct drm_device *dev = nv_crtc->base.dev;
  206. u32 *push = evo_wait(dev, 0, 16);
  207. if (push) {
  208. if (show) {
  209. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
  210. evo_data(push, 0x85000000);
  211. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  212. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  213. evo_data(push, NvEvoVRAM);
  214. } else {
  215. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
  216. evo_data(push, 0x05000000);
  217. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  218. evo_data(push, 0x00000000);
  219. }
  220. if (update) {
  221. evo_mthd(push, 0x0080, 1);
  222. evo_data(push, 0x00000000);
  223. }
  224. evo_kick(push, dev, 0);
  225. }
  226. }
  227. static void
  228. nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
  229. {
  230. }
  231. static void
  232. nvd0_crtc_prepare(struct drm_crtc *crtc)
  233. {
  234. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  235. u32 *push;
  236. push = evo_wait(crtc->dev, 0, 2);
  237. if (push) {
  238. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  239. evo_data(push, 0x00000000);
  240. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
  241. evo_data(push, 0x03000000);
  242. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  243. evo_data(push, 0x00000000);
  244. evo_kick(push, crtc->dev, 0);
  245. }
  246. nvd0_crtc_cursor_show(nv_crtc, false, false);
  247. }
  248. static void
  249. nvd0_crtc_commit(struct drm_crtc *crtc)
  250. {
  251. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  252. u32 *push;
  253. push = evo_wait(crtc->dev, 0, 32);
  254. if (push) {
  255. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  256. evo_data(push, nv_crtc->fb.tile_flags);
  257. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
  258. evo_data(push, 0x83000000);
  259. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  260. evo_data(push, 0x00000000);
  261. evo_data(push, 0x00000000);
  262. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  263. evo_data(push, NvEvoVRAM);
  264. evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
  265. evo_data(push, 0xffffff00);
  266. evo_kick(push, crtc->dev, 0);
  267. }
  268. nvd0_crtc_cursor_show(nv_crtc, nv_crtc->cursor.visible, true);
  269. }
  270. static bool
  271. nvd0_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
  272. struct drm_display_mode *adjusted_mode)
  273. {
  274. return true;
  275. }
  276. static int
  277. nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
  278. {
  279. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
  280. int ret;
  281. ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
  282. if (ret)
  283. return ret;
  284. if (old_fb) {
  285. nvfb = nouveau_framebuffer(old_fb);
  286. nouveau_bo_unpin(nvfb->nvbo);
  287. }
  288. return 0;
  289. }
  290. static int
  291. nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
  292. struct drm_display_mode *mode, int x, int y,
  293. struct drm_framebuffer *old_fb)
  294. {
  295. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  296. struct nouveau_connector *nv_connector;
  297. u32 htotal = mode->htotal;
  298. u32 vtotal = mode->vtotal;
  299. u32 hsyncw = mode->hsync_end - mode->hsync_start - 1;
  300. u32 vsyncw = mode->vsync_end - mode->vsync_start - 1;
  301. u32 hfrntp = mode->hsync_start - mode->hdisplay;
  302. u32 vfrntp = mode->vsync_start - mode->vdisplay;
  303. u32 hbackp = mode->htotal - mode->hsync_end;
  304. u32 vbackp = mode->vtotal - mode->vsync_end;
  305. u32 hss2be = hsyncw + hbackp;
  306. u32 vss2be = vsyncw + vbackp;
  307. u32 hss2de = htotal - hfrntp;
  308. u32 vss2de = vtotal - vfrntp;
  309. u32 syncs, *push;
  310. int ret;
  311. syncs = 0x00000001;
  312. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  313. syncs |= 0x00000008;
  314. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  315. syncs |= 0x00000010;
  316. ret = nvd0_crtc_swap_fbs(crtc, old_fb);
  317. if (ret)
  318. return ret;
  319. push = evo_wait(crtc->dev, 0, 64);
  320. if (push) {
  321. evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 5);
  322. evo_data(push, 0x00000000);
  323. evo_data(push, (vtotal << 16) | htotal);
  324. evo_data(push, (vsyncw << 16) | hsyncw);
  325. evo_data(push, (vss2be << 16) | hss2be);
  326. evo_data(push, (vss2de << 16) | hss2de);
  327. evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
  328. evo_data(push, 0x00000000); /* ??? */
  329. evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
  330. evo_data(push, mode->clock * 1000);
  331. evo_data(push, 0x00200000); /* ??? */
  332. evo_data(push, mode->clock * 1000);
  333. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 1);
  334. evo_data(push, syncs);
  335. evo_kick(push, crtc->dev, 0);
  336. }
  337. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  338. nvd0_crtc_set_dither(nv_crtc, false);
  339. nvd0_crtc_set_scale(nv_crtc, false);
  340. nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
  341. return 0;
  342. }
  343. static int
  344. nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  345. struct drm_framebuffer *old_fb)
  346. {
  347. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  348. int ret;
  349. if (!crtc->fb) {
  350. NV_DEBUG_KMS(crtc->dev, "No FB bound\n");
  351. return 0;
  352. }
  353. ret = nvd0_crtc_swap_fbs(crtc, old_fb);
  354. if (ret)
  355. return ret;
  356. nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
  357. return 0;
  358. }
  359. static int
  360. nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  361. struct drm_framebuffer *fb, int x, int y,
  362. enum mode_set_atomic state)
  363. {
  364. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  365. nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
  366. return 0;
  367. }
  368. static void
  369. nvd0_crtc_lut_load(struct drm_crtc *crtc)
  370. {
  371. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  372. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  373. int i;
  374. for (i = 0; i < 256; i++) {
  375. writew(0x6000 + (nv_crtc->lut.r[i] >> 2), lut + (i * 0x20) + 0);
  376. writew(0x6000 + (nv_crtc->lut.g[i] >> 2), lut + (i * 0x20) + 2);
  377. writew(0x6000 + (nv_crtc->lut.b[i] >> 2), lut + (i * 0x20) + 4);
  378. }
  379. }
  380. static int
  381. nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  382. uint32_t handle, uint32_t width, uint32_t height)
  383. {
  384. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  385. struct drm_device *dev = crtc->dev;
  386. struct drm_gem_object *gem;
  387. struct nouveau_bo *nvbo;
  388. bool visible = (handle != 0);
  389. int i, ret = 0;
  390. if (visible) {
  391. if (width != 64 || height != 64)
  392. return -EINVAL;
  393. gem = drm_gem_object_lookup(dev, file_priv, handle);
  394. if (unlikely(!gem))
  395. return -ENOENT;
  396. nvbo = nouveau_gem_object(gem);
  397. ret = nouveau_bo_map(nvbo);
  398. if (ret == 0) {
  399. for (i = 0; i < 64 * 64; i++) {
  400. u32 v = nouveau_bo_rd32(nvbo, i);
  401. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
  402. }
  403. nouveau_bo_unmap(nvbo);
  404. }
  405. drm_gem_object_unreference_unlocked(gem);
  406. }
  407. if (visible != nv_crtc->cursor.visible) {
  408. nvd0_crtc_cursor_show(nv_crtc, visible, true);
  409. nv_crtc->cursor.visible = visible;
  410. }
  411. return ret;
  412. }
  413. static int
  414. nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  415. {
  416. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  417. const u32 data = (y << 16) | x;
  418. nv_wr32(crtc->dev, 0x64d084 + (nv_crtc->index * 0x1000), data);
  419. nv_wr32(crtc->dev, 0x64d080 + (nv_crtc->index * 0x1000), 0x00000000);
  420. return 0;
  421. }
  422. static void
  423. nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  424. uint32_t start, uint32_t size)
  425. {
  426. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  427. u32 end = max(start + size, (u32)256);
  428. u32 i;
  429. for (i = start; i < end; i++) {
  430. nv_crtc->lut.r[i] = r[i];
  431. nv_crtc->lut.g[i] = g[i];
  432. nv_crtc->lut.b[i] = b[i];
  433. }
  434. nvd0_crtc_lut_load(crtc);
  435. }
  436. static void
  437. nvd0_crtc_destroy(struct drm_crtc *crtc)
  438. {
  439. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  440. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  441. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  442. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  443. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  444. drm_crtc_cleanup(crtc);
  445. kfree(crtc);
  446. }
  447. static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
  448. .dpms = nvd0_crtc_dpms,
  449. .prepare = nvd0_crtc_prepare,
  450. .commit = nvd0_crtc_commit,
  451. .mode_fixup = nvd0_crtc_mode_fixup,
  452. .mode_set = nvd0_crtc_mode_set,
  453. .mode_set_base = nvd0_crtc_mode_set_base,
  454. .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
  455. .load_lut = nvd0_crtc_lut_load,
  456. };
  457. static const struct drm_crtc_funcs nvd0_crtc_func = {
  458. .cursor_set = nvd0_crtc_cursor_set,
  459. .cursor_move = nvd0_crtc_cursor_move,
  460. .gamma_set = nvd0_crtc_gamma_set,
  461. .set_config = drm_crtc_helper_set_config,
  462. .destroy = nvd0_crtc_destroy,
  463. };
  464. static void
  465. nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
  466. {
  467. }
  468. static void
  469. nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
  470. {
  471. }
  472. static int
  473. nvd0_crtc_create(struct drm_device *dev, int index)
  474. {
  475. struct nouveau_crtc *nv_crtc;
  476. struct drm_crtc *crtc;
  477. int ret, i;
  478. nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
  479. if (!nv_crtc)
  480. return -ENOMEM;
  481. nv_crtc->index = index;
  482. nv_crtc->set_dither = nvd0_crtc_set_dither;
  483. nv_crtc->set_scale = nvd0_crtc_set_scale;
  484. nv_crtc->cursor.set_offset = nvd0_cursor_set_offset;
  485. nv_crtc->cursor.set_pos = nvd0_cursor_set_pos;
  486. for (i = 0; i < 256; i++) {
  487. nv_crtc->lut.r[i] = i << 8;
  488. nv_crtc->lut.g[i] = i << 8;
  489. nv_crtc->lut.b[i] = i << 8;
  490. }
  491. crtc = &nv_crtc->base;
  492. drm_crtc_init(dev, crtc, &nvd0_crtc_func);
  493. drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
  494. drm_mode_crtc_set_gamma_size(crtc, 256);
  495. ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
  496. 0, 0x0000, &nv_crtc->cursor.nvbo);
  497. if (!ret) {
  498. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  499. if (!ret)
  500. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  501. if (ret)
  502. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  503. }
  504. if (ret)
  505. goto out;
  506. ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
  507. 0, 0x0000, &nv_crtc->lut.nvbo);
  508. if (!ret) {
  509. ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
  510. if (!ret)
  511. ret = nouveau_bo_map(nv_crtc->lut.nvbo);
  512. if (ret)
  513. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  514. }
  515. if (ret)
  516. goto out;
  517. nvd0_crtc_lut_load(crtc);
  518. out:
  519. if (ret)
  520. nvd0_crtc_destroy(crtc);
  521. return ret;
  522. }
  523. /******************************************************************************
  524. * DAC
  525. *****************************************************************************/
  526. static void
  527. nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
  528. {
  529. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  530. struct drm_device *dev = encoder->dev;
  531. int or = nv_encoder->or;
  532. u32 dpms_ctrl;
  533. dpms_ctrl = 0x80000000;
  534. if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
  535. dpms_ctrl |= 0x00000001;
  536. if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
  537. dpms_ctrl |= 0x00000004;
  538. nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
  539. nv_mask(dev, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
  540. nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
  541. }
  542. static bool
  543. nvd0_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  544. struct drm_display_mode *adjusted_mode)
  545. {
  546. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  547. struct nouveau_connector *nv_connector;
  548. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  549. if (nv_connector && nv_connector->native_mode) {
  550. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  551. int id = adjusted_mode->base.id;
  552. *adjusted_mode = *nv_connector->native_mode;
  553. adjusted_mode->base.id = id;
  554. }
  555. }
  556. return true;
  557. }
  558. static void
  559. nvd0_dac_prepare(struct drm_encoder *encoder)
  560. {
  561. }
  562. static void
  563. nvd0_dac_commit(struct drm_encoder *encoder)
  564. {
  565. }
  566. static void
  567. nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  568. struct drm_display_mode *adjusted_mode)
  569. {
  570. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  571. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  572. u32 *push;
  573. nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  574. push = evo_wait(encoder->dev, 0, 4);
  575. if (push) {
  576. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 2);
  577. evo_data(push, 1 << nv_crtc->index);
  578. evo_data(push, 0x00ff);
  579. evo_kick(push, encoder->dev, 0);
  580. }
  581. nv_encoder->crtc = encoder->crtc;
  582. }
  583. static void
  584. nvd0_dac_disconnect(struct drm_encoder *encoder)
  585. {
  586. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  587. struct drm_device *dev = encoder->dev;
  588. u32 *push;
  589. if (nv_encoder->crtc) {
  590. nvd0_crtc_prepare(nv_encoder->crtc);
  591. push = evo_wait(dev, 0, 4);
  592. if (push) {
  593. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
  594. evo_data(push, 0x00000000);
  595. evo_mthd(push, 0x0080, 1);
  596. evo_data(push, 0x00000000);
  597. evo_kick(push, dev, 0);
  598. }
  599. nv_encoder->crtc = NULL;
  600. }
  601. }
  602. static enum drm_connector_status
  603. nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  604. {
  605. enum drm_connector_status status = connector_status_disconnected;
  606. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  607. struct drm_device *dev = encoder->dev;
  608. int or = nv_encoder->or;
  609. u32 load;
  610. nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00100000);
  611. udelay(9500);
  612. nv_wr32(dev, 0x61a00c + (or * 0x800), 0x80000000);
  613. load = nv_rd32(dev, 0x61a00c + (or * 0x800));
  614. if ((load & 0x38000000) == 0x38000000)
  615. status = connector_status_connected;
  616. nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00000000);
  617. return status;
  618. }
  619. static void
  620. nvd0_dac_destroy(struct drm_encoder *encoder)
  621. {
  622. drm_encoder_cleanup(encoder);
  623. kfree(encoder);
  624. }
  625. static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
  626. .dpms = nvd0_dac_dpms,
  627. .mode_fixup = nvd0_dac_mode_fixup,
  628. .prepare = nvd0_dac_prepare,
  629. .commit = nvd0_dac_commit,
  630. .mode_set = nvd0_dac_mode_set,
  631. .disable = nvd0_dac_disconnect,
  632. .get_crtc = nvd0_display_crtc_get,
  633. .detect = nvd0_dac_detect
  634. };
  635. static const struct drm_encoder_funcs nvd0_dac_func = {
  636. .destroy = nvd0_dac_destroy,
  637. };
  638. static int
  639. nvd0_dac_create(struct drm_connector *connector, struct dcb_entry *dcbe)
  640. {
  641. struct drm_device *dev = connector->dev;
  642. struct nouveau_encoder *nv_encoder;
  643. struct drm_encoder *encoder;
  644. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  645. if (!nv_encoder)
  646. return -ENOMEM;
  647. nv_encoder->dcb = dcbe;
  648. nv_encoder->or = ffs(dcbe->or) - 1;
  649. encoder = to_drm_encoder(nv_encoder);
  650. encoder->possible_crtcs = dcbe->heads;
  651. encoder->possible_clones = 0;
  652. drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
  653. drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
  654. drm_mode_connector_attach_encoder(connector, encoder);
  655. return 0;
  656. }
  657. /******************************************************************************
  658. * SOR
  659. *****************************************************************************/
  660. static void
  661. nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
  662. {
  663. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  664. struct drm_device *dev = encoder->dev;
  665. struct drm_encoder *partner;
  666. int or = nv_encoder->or;
  667. u32 dpms_ctrl;
  668. nv_encoder->last_dpms = mode;
  669. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  670. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  671. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  672. continue;
  673. if (nv_partner != nv_encoder &&
  674. nv_partner->dcb->or == nv_encoder->dcb->or) {
  675. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  676. return;
  677. break;
  678. }
  679. }
  680. dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
  681. dpms_ctrl |= 0x80000000;
  682. nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
  683. nv_mask(dev, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
  684. nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
  685. nv_wait(dev, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
  686. }
  687. static bool
  688. nvd0_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  689. struct drm_display_mode *adjusted_mode)
  690. {
  691. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  692. struct nouveau_connector *nv_connector;
  693. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  694. if (nv_connector && nv_connector->native_mode) {
  695. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  696. int id = adjusted_mode->base.id;
  697. *adjusted_mode = *nv_connector->native_mode;
  698. adjusted_mode->base.id = id;
  699. }
  700. }
  701. return true;
  702. }
  703. static void
  704. nvd0_sor_prepare(struct drm_encoder *encoder)
  705. {
  706. }
  707. static void
  708. nvd0_sor_commit(struct drm_encoder *encoder)
  709. {
  710. }
  711. static void
  712. nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  713. struct drm_display_mode *mode)
  714. {
  715. struct drm_nouveau_private *dev_priv = encoder->dev->dev_private;
  716. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  717. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  718. struct nouveau_connector *nv_connector;
  719. struct nvbios *bios = &dev_priv->vbios;
  720. u32 mode_ctrl = (1 << nv_crtc->index);
  721. u32 *push, or_config;
  722. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  723. switch (nv_encoder->dcb->type) {
  724. case OUTPUT_TMDS:
  725. if (nv_encoder->dcb->sorconf.link & 1) {
  726. if (mode->clock < 165000)
  727. mode_ctrl |= 0x00000100;
  728. else
  729. mode_ctrl |= 0x00000500;
  730. } else {
  731. mode_ctrl |= 0x00000200;
  732. }
  733. or_config = (mode_ctrl & 0x00000f00) >> 8;
  734. if (mode->clock >= 165000)
  735. or_config |= 0x0100;
  736. break;
  737. case OUTPUT_LVDS:
  738. or_config = (mode_ctrl & 0x00000f00) >> 8;
  739. if (bios->fp_no_ddc) {
  740. if (bios->fp.dual_link)
  741. or_config |= 0x0100;
  742. if (bios->fp.if_is_24bit)
  743. or_config |= 0x0200;
  744. } else {
  745. if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS_SPWG) {
  746. if (((u8 *)nv_connector->edid)[121] == 2)
  747. or_config |= 0x0100;
  748. } else
  749. if (mode->clock >= bios->fp.duallink_transition_clk) {
  750. or_config |= 0x0100;
  751. }
  752. if (or_config & 0x0100) {
  753. if (bios->fp.strapless_is_24bit & 2)
  754. or_config |= 0x0200;
  755. } else {
  756. if (bios->fp.strapless_is_24bit & 1)
  757. or_config |= 0x0200;
  758. }
  759. if (nv_connector->base.display_info.bpc == 8)
  760. or_config |= 0x0200;
  761. }
  762. break;
  763. default:
  764. BUG_ON(1);
  765. break;
  766. }
  767. nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  768. push = evo_wait(encoder->dev, 0, 4);
  769. if (push) {
  770. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 2);
  771. evo_data(push, mode_ctrl);
  772. evo_data(push, or_config);
  773. evo_kick(push, encoder->dev, 0);
  774. }
  775. nv_encoder->crtc = encoder->crtc;
  776. }
  777. static void
  778. nvd0_sor_disconnect(struct drm_encoder *encoder)
  779. {
  780. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  781. struct drm_device *dev = encoder->dev;
  782. u32 *push;
  783. if (nv_encoder->crtc) {
  784. nvd0_crtc_prepare(nv_encoder->crtc);
  785. push = evo_wait(dev, 0, 4);
  786. if (push) {
  787. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  788. evo_data(push, 0x00000000);
  789. evo_mthd(push, 0x0080, 1);
  790. evo_data(push, 0x00000000);
  791. evo_kick(push, dev, 0);
  792. }
  793. nv_encoder->crtc = NULL;
  794. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  795. }
  796. }
  797. static void
  798. nvd0_sor_destroy(struct drm_encoder *encoder)
  799. {
  800. drm_encoder_cleanup(encoder);
  801. kfree(encoder);
  802. }
  803. static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
  804. .dpms = nvd0_sor_dpms,
  805. .mode_fixup = nvd0_sor_mode_fixup,
  806. .prepare = nvd0_sor_prepare,
  807. .commit = nvd0_sor_commit,
  808. .mode_set = nvd0_sor_mode_set,
  809. .disable = nvd0_sor_disconnect,
  810. .get_crtc = nvd0_display_crtc_get,
  811. };
  812. static const struct drm_encoder_funcs nvd0_sor_func = {
  813. .destroy = nvd0_sor_destroy,
  814. };
  815. static int
  816. nvd0_sor_create(struct drm_connector *connector, struct dcb_entry *dcbe)
  817. {
  818. struct drm_device *dev = connector->dev;
  819. struct nouveau_encoder *nv_encoder;
  820. struct drm_encoder *encoder;
  821. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  822. if (!nv_encoder)
  823. return -ENOMEM;
  824. nv_encoder->dcb = dcbe;
  825. nv_encoder->or = ffs(dcbe->or) - 1;
  826. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  827. encoder = to_drm_encoder(nv_encoder);
  828. encoder->possible_crtcs = dcbe->heads;
  829. encoder->possible_clones = 0;
  830. drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
  831. drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
  832. drm_mode_connector_attach_encoder(connector, encoder);
  833. return 0;
  834. }
  835. /******************************************************************************
  836. * IRQ
  837. *****************************************************************************/
  838. static struct dcb_entry *
  839. lookup_dcb(struct drm_device *dev, int id, u32 mc)
  840. {
  841. struct drm_nouveau_private *dev_priv = dev->dev_private;
  842. int type, or, i;
  843. if (id < 4) {
  844. type = OUTPUT_ANALOG;
  845. or = id;
  846. } else {
  847. switch (mc & 0x00000f00) {
  848. case 0x00000000: type = OUTPUT_LVDS; break;
  849. case 0x00000100: type = OUTPUT_TMDS; break;
  850. case 0x00000200: type = OUTPUT_TMDS; break;
  851. case 0x00000500: type = OUTPUT_TMDS; break;
  852. default:
  853. NV_ERROR(dev, "PDISP: unknown SOR mc 0x%08x\n", mc);
  854. return NULL;
  855. }
  856. or = id - 4;
  857. }
  858. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  859. struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
  860. if (dcb->type == type && (dcb->or & (1 << or)))
  861. return dcb;
  862. }
  863. NV_ERROR(dev, "PDISP: DCB for %d/0x%08x not found\n", id, mc);
  864. return NULL;
  865. }
  866. static void
  867. nvd0_display_unk1_handler(struct drm_device *dev, u32 crtc, u32 mask)
  868. {
  869. struct dcb_entry *dcb;
  870. int i;
  871. for (i = 0; mask && i < 8; i++) {
  872. u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
  873. if (!(mcc & (1 << crtc)))
  874. continue;
  875. dcb = lookup_dcb(dev, i, mcc);
  876. if (!dcb)
  877. continue;
  878. nouveau_bios_run_display_table(dev, 0x0000, -1, dcb, crtc);
  879. }
  880. nv_wr32(dev, 0x6101d4, 0x00000000);
  881. nv_wr32(dev, 0x6109d4, 0x00000000);
  882. nv_wr32(dev, 0x6101d0, 0x80000000);
  883. }
  884. static void
  885. nvd0_display_unk2_handler(struct drm_device *dev, u32 crtc, u32 mask)
  886. {
  887. struct dcb_entry *dcb;
  888. u32 or, tmp, pclk;
  889. int i;
  890. for (i = 0; mask && i < 8; i++) {
  891. u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
  892. if (!(mcc & (1 << crtc)))
  893. continue;
  894. dcb = lookup_dcb(dev, i, mcc);
  895. if (!dcb)
  896. continue;
  897. nouveau_bios_run_display_table(dev, 0x0000, -2, dcb, crtc);
  898. }
  899. pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
  900. if (mask & 0x00010000) {
  901. nv50_crtc_set_clock(dev, crtc, pclk);
  902. }
  903. for (i = 0; mask && i < 8; i++) {
  904. u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
  905. u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
  906. if (!(mcp & (1 << crtc)))
  907. continue;
  908. dcb = lookup_dcb(dev, i, mcp);
  909. if (!dcb)
  910. continue;
  911. or = ffs(dcb->or) - 1;
  912. nouveau_bios_run_display_table(dev, cfg, pclk, dcb, crtc);
  913. nv_wr32(dev, 0x612200 + (crtc * 0x800), 0x00000000);
  914. switch (dcb->type) {
  915. case OUTPUT_ANALOG:
  916. nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
  917. break;
  918. case OUTPUT_TMDS:
  919. case OUTPUT_LVDS:
  920. if (cfg & 0x00000100)
  921. tmp = 0x00000101;
  922. else
  923. tmp = 0x00000000;
  924. nv_mask(dev, 0x612300 + (or * 0x800), 0x00000707, tmp);
  925. break;
  926. default:
  927. break;
  928. }
  929. break;
  930. }
  931. nv_wr32(dev, 0x6101d4, 0x00000000);
  932. nv_wr32(dev, 0x6109d4, 0x00000000);
  933. nv_wr32(dev, 0x6101d0, 0x80000000);
  934. }
  935. static void
  936. nvd0_display_unk4_handler(struct drm_device *dev, u32 crtc, u32 mask)
  937. {
  938. struct dcb_entry *dcb;
  939. int pclk, i;
  940. pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
  941. for (i = 0; mask && i < 8; i++) {
  942. u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
  943. u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
  944. if (!(mcp & (1 << crtc)))
  945. continue;
  946. dcb = lookup_dcb(dev, i, mcp);
  947. if (!dcb)
  948. continue;
  949. nouveau_bios_run_display_table(dev, cfg, -pclk, dcb, crtc);
  950. }
  951. nv_wr32(dev, 0x6101d4, 0x00000000);
  952. nv_wr32(dev, 0x6109d4, 0x00000000);
  953. nv_wr32(dev, 0x6101d0, 0x80000000);
  954. }
  955. static void
  956. nvd0_display_bh(unsigned long data)
  957. {
  958. struct drm_device *dev = (struct drm_device *)data;
  959. struct nvd0_display *disp = nvd0_display(dev);
  960. u32 mask, crtc;
  961. int i;
  962. if (drm_debug & (DRM_UT_DRIVER | DRM_UT_KMS)) {
  963. NV_INFO(dev, "PDISP: modeset req %d\n", disp->modeset);
  964. NV_INFO(dev, " STAT: 0x%08x 0x%08x 0x%08x\n",
  965. nv_rd32(dev, 0x6101d0),
  966. nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
  967. for (i = 0; i < 8; i++) {
  968. NV_INFO(dev, " %s%d: 0x%08x 0x%08x\n",
  969. i < 4 ? "DAC" : "SOR", i,
  970. nv_rd32(dev, 0x640180 + (i * 0x20)),
  971. nv_rd32(dev, 0x660180 + (i * 0x20)));
  972. }
  973. }
  974. mask = nv_rd32(dev, 0x6101d4);
  975. crtc = 0;
  976. if (!mask) {
  977. mask = nv_rd32(dev, 0x6109d4);
  978. crtc = 1;
  979. }
  980. if (disp->modeset & 0x00000001)
  981. nvd0_display_unk1_handler(dev, crtc, mask);
  982. if (disp->modeset & 0x00000002)
  983. nvd0_display_unk2_handler(dev, crtc, mask);
  984. if (disp->modeset & 0x00000004)
  985. nvd0_display_unk4_handler(dev, crtc, mask);
  986. }
  987. static void
  988. nvd0_display_intr(struct drm_device *dev)
  989. {
  990. struct nvd0_display *disp = nvd0_display(dev);
  991. u32 intr = nv_rd32(dev, 0x610088);
  992. if (intr & 0x00000002) {
  993. u32 stat = nv_rd32(dev, 0x61009c);
  994. int chid = ffs(stat) - 1;
  995. if (chid >= 0) {
  996. u32 mthd = nv_rd32(dev, 0x6101f0 + (chid * 12));
  997. u32 data = nv_rd32(dev, 0x6101f4 + (chid * 12));
  998. u32 unkn = nv_rd32(dev, 0x6101f8 + (chid * 12));
  999. NV_INFO(dev, "EvoCh: chid %d mthd 0x%04x data 0x%08x "
  1000. "0x%08x 0x%08x\n",
  1001. chid, (mthd & 0x0000ffc), data, mthd, unkn);
  1002. nv_wr32(dev, 0x61009c, (1 << chid));
  1003. nv_wr32(dev, 0x6101f0 + (chid * 12), 0x90000000);
  1004. }
  1005. intr &= ~0x00000002;
  1006. }
  1007. if (intr & 0x00100000) {
  1008. u32 stat = nv_rd32(dev, 0x6100ac);
  1009. if (stat & 0x00000007) {
  1010. disp->modeset = stat;
  1011. tasklet_schedule(&disp->tasklet);
  1012. nv_wr32(dev, 0x6100ac, (stat & 0x00000007));
  1013. stat &= ~0x00000007;
  1014. }
  1015. if (stat) {
  1016. NV_INFO(dev, "PDISP: unknown intr24 0x%08x\n", stat);
  1017. nv_wr32(dev, 0x6100ac, stat);
  1018. }
  1019. intr &= ~0x00100000;
  1020. }
  1021. if (intr & 0x01000000) {
  1022. u32 stat = nv_rd32(dev, 0x6100bc);
  1023. nv_wr32(dev, 0x6100bc, stat);
  1024. intr &= ~0x01000000;
  1025. }
  1026. if (intr & 0x02000000) {
  1027. u32 stat = nv_rd32(dev, 0x6108bc);
  1028. nv_wr32(dev, 0x6108bc, stat);
  1029. intr &= ~0x02000000;
  1030. }
  1031. if (intr)
  1032. NV_INFO(dev, "PDISP: unknown intr 0x%08x\n", intr);
  1033. }
  1034. /******************************************************************************
  1035. * Init
  1036. *****************************************************************************/
  1037. static void
  1038. nvd0_display_fini(struct drm_device *dev)
  1039. {
  1040. int i;
  1041. /* fini cursors */
  1042. for (i = 14; i >= 13; i--) {
  1043. if (!(nv_rd32(dev, 0x610490 + (i * 0x10)) & 0x00000001))
  1044. continue;
  1045. nv_mask(dev, 0x610490 + (i * 0x10), 0x00000001, 0x00000000);
  1046. nv_wait(dev, 0x610490 + (i * 0x10), 0x00010000, 0x00000000);
  1047. nv_mask(dev, 0x610090, 1 << i, 0x00000000);
  1048. nv_mask(dev, 0x6100a0, 1 << i, 0x00000000);
  1049. }
  1050. /* fini master */
  1051. if (nv_rd32(dev, 0x610490) & 0x00000010) {
  1052. nv_mask(dev, 0x610490, 0x00000010, 0x00000000);
  1053. nv_mask(dev, 0x610490, 0x00000003, 0x00000000);
  1054. nv_wait(dev, 0x610490, 0x80000000, 0x00000000);
  1055. nv_mask(dev, 0x610090, 0x00000001, 0x00000000);
  1056. nv_mask(dev, 0x6100a0, 0x00000001, 0x00000000);
  1057. }
  1058. }
  1059. int
  1060. nvd0_display_init(struct drm_device *dev)
  1061. {
  1062. struct nvd0_display *disp = nvd0_display(dev);
  1063. u32 *push;
  1064. int i;
  1065. if (nv_rd32(dev, 0x6100ac) & 0x00000100) {
  1066. nv_wr32(dev, 0x6100ac, 0x00000100);
  1067. nv_mask(dev, 0x6194e8, 0x00000001, 0x00000000);
  1068. if (!nv_wait(dev, 0x6194e8, 0x00000002, 0x00000000)) {
  1069. NV_ERROR(dev, "PDISP: 0x6194e8 0x%08x\n",
  1070. nv_rd32(dev, 0x6194e8));
  1071. return -EBUSY;
  1072. }
  1073. }
  1074. /* nfi what these are exactly, i do know that SOR_MODE_CTRL won't
  1075. * work at all unless you do the SOR part below.
  1076. */
  1077. for (i = 0; i < 3; i++) {
  1078. u32 dac = nv_rd32(dev, 0x61a000 + (i * 0x800));
  1079. nv_wr32(dev, 0x6101c0 + (i * 0x800), dac);
  1080. }
  1081. for (i = 0; i < 4; i++) {
  1082. u32 sor = nv_rd32(dev, 0x61c000 + (i * 0x800));
  1083. nv_wr32(dev, 0x6301c4 + (i * 0x800), sor);
  1084. }
  1085. for (i = 0; i < 2; i++) {
  1086. u32 crtc0 = nv_rd32(dev, 0x616104 + (i * 0x800));
  1087. u32 crtc1 = nv_rd32(dev, 0x616108 + (i * 0x800));
  1088. u32 crtc2 = nv_rd32(dev, 0x61610c + (i * 0x800));
  1089. nv_wr32(dev, 0x6101b4 + (i * 0x800), crtc0);
  1090. nv_wr32(dev, 0x6101b8 + (i * 0x800), crtc1);
  1091. nv_wr32(dev, 0x6101bc + (i * 0x800), crtc2);
  1092. }
  1093. /* point at our hash table / objects, enable interrupts */
  1094. nv_wr32(dev, 0x610010, (disp->mem->vinst >> 8) | 9);
  1095. nv_mask(dev, 0x6100b0, 0x00000307, 0x00000307);
  1096. /* init master */
  1097. nv_wr32(dev, 0x610494, (disp->evo[0].handle >> 8) | 3);
  1098. nv_wr32(dev, 0x610498, 0x00010000);
  1099. nv_wr32(dev, 0x61049c, 0x00000001);
  1100. nv_mask(dev, 0x610490, 0x00000010, 0x00000010);
  1101. nv_wr32(dev, 0x640000, 0x00000000);
  1102. nv_wr32(dev, 0x610490, 0x01000013);
  1103. if (!nv_wait(dev, 0x610490, 0x80000000, 0x00000000)) {
  1104. NV_ERROR(dev, "PDISP: master 0x%08x\n",
  1105. nv_rd32(dev, 0x610490));
  1106. return -EBUSY;
  1107. }
  1108. nv_mask(dev, 0x610090, 0x00000001, 0x00000001);
  1109. nv_mask(dev, 0x6100a0, 0x00000001, 0x00000001);
  1110. /* init cursors */
  1111. for (i = 13; i <= 14; i++) {
  1112. nv_wr32(dev, 0x610490 + (i * 0x10), 0x00000001);
  1113. if (!nv_wait(dev, 0x610490 + (i * 0x10), 0x00010000, 0x00010000)) {
  1114. NV_ERROR(dev, "PDISP: curs%d 0x%08x\n", i,
  1115. nv_rd32(dev, 0x610490 + (i * 0x10)));
  1116. return -EBUSY;
  1117. }
  1118. nv_mask(dev, 0x610090, 1 << i, 1 << i);
  1119. nv_mask(dev, 0x6100a0, 1 << i, 1 << i);
  1120. }
  1121. push = evo_wait(dev, 0, 32);
  1122. if (!push)
  1123. return -EBUSY;
  1124. evo_mthd(push, 0x0088, 1);
  1125. evo_data(push, NvEvoSync);
  1126. evo_mthd(push, 0x0084, 1);
  1127. evo_data(push, 0x00000000);
  1128. evo_mthd(push, 0x0084, 1);
  1129. evo_data(push, 0x80000000);
  1130. evo_mthd(push, 0x008c, 1);
  1131. evo_data(push, 0x00000000);
  1132. evo_kick(push, dev, 0);
  1133. return 0;
  1134. }
  1135. void
  1136. nvd0_display_destroy(struct drm_device *dev)
  1137. {
  1138. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1139. struct nvd0_display *disp = nvd0_display(dev);
  1140. struct pci_dev *pdev = dev->pdev;
  1141. nvd0_display_fini(dev);
  1142. pci_free_consistent(pdev, PAGE_SIZE, disp->evo[0].ptr, disp->evo[0].handle);
  1143. nouveau_gpuobj_ref(NULL, &disp->mem);
  1144. nouveau_irq_unregister(dev, 26);
  1145. dev_priv->engine.display.priv = NULL;
  1146. kfree(disp);
  1147. }
  1148. int
  1149. nvd0_display_create(struct drm_device *dev)
  1150. {
  1151. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1152. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  1153. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  1154. struct drm_connector *connector, *tmp;
  1155. struct pci_dev *pdev = dev->pdev;
  1156. struct nvd0_display *disp;
  1157. struct dcb_entry *dcbe;
  1158. int ret, i;
  1159. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  1160. if (!disp)
  1161. return -ENOMEM;
  1162. dev_priv->engine.display.priv = disp;
  1163. /* create crtc objects to represent the hw heads */
  1164. for (i = 0; i < 2; i++) {
  1165. ret = nvd0_crtc_create(dev, i);
  1166. if (ret)
  1167. goto out;
  1168. }
  1169. /* create encoder/connector objects based on VBIOS DCB table */
  1170. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  1171. connector = nouveau_connector_create(dev, dcbe->connector);
  1172. if (IS_ERR(connector))
  1173. continue;
  1174. if (dcbe->location != DCB_LOC_ON_CHIP) {
  1175. NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
  1176. dcbe->type, ffs(dcbe->or) - 1);
  1177. continue;
  1178. }
  1179. switch (dcbe->type) {
  1180. case OUTPUT_TMDS:
  1181. case OUTPUT_LVDS:
  1182. nvd0_sor_create(connector, dcbe);
  1183. break;
  1184. case OUTPUT_ANALOG:
  1185. nvd0_dac_create(connector, dcbe);
  1186. break;
  1187. default:
  1188. NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
  1189. dcbe->type, ffs(dcbe->or) - 1);
  1190. continue;
  1191. }
  1192. }
  1193. /* cull any connectors we created that don't have an encoder */
  1194. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  1195. if (connector->encoder_ids[0])
  1196. continue;
  1197. NV_WARN(dev, "%s has no encoders, removing\n",
  1198. drm_get_connector_name(connector));
  1199. connector->funcs->destroy(connector);
  1200. }
  1201. /* setup interrupt handling */
  1202. tasklet_init(&disp->tasklet, nvd0_display_bh, (unsigned long)dev);
  1203. nouveau_irq_register(dev, 26, nvd0_display_intr);
  1204. /* hash table and dma objects for the memory areas we care about */
  1205. ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
  1206. NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
  1207. if (ret)
  1208. goto out;
  1209. nv_wo32(disp->mem, 0x1000, 0x00000049);
  1210. nv_wo32(disp->mem, 0x1004, (disp->mem->vinst + 0x2000) >> 8);
  1211. nv_wo32(disp->mem, 0x1008, (disp->mem->vinst + 0x2fff) >> 8);
  1212. nv_wo32(disp->mem, 0x100c, 0x00000000);
  1213. nv_wo32(disp->mem, 0x1010, 0x00000000);
  1214. nv_wo32(disp->mem, 0x1014, 0x00000000);
  1215. nv_wo32(disp->mem, 0x0000, NvEvoSync);
  1216. nv_wo32(disp->mem, 0x0004, (0x1000 << 9) | 0x00000001);
  1217. nv_wo32(disp->mem, 0x1020, 0x00000049);
  1218. nv_wo32(disp->mem, 0x1024, 0x00000000);
  1219. nv_wo32(disp->mem, 0x1028, (dev_priv->vram_size - 1) >> 8);
  1220. nv_wo32(disp->mem, 0x102c, 0x00000000);
  1221. nv_wo32(disp->mem, 0x1030, 0x00000000);
  1222. nv_wo32(disp->mem, 0x1034, 0x00000000);
  1223. nv_wo32(disp->mem, 0x0008, NvEvoVRAM);
  1224. nv_wo32(disp->mem, 0x000c, (0x1020 << 9) | 0x00000001);
  1225. nv_wo32(disp->mem, 0x1040, 0x00000009);
  1226. nv_wo32(disp->mem, 0x1044, 0x00000000);
  1227. nv_wo32(disp->mem, 0x1048, (dev_priv->vram_size - 1) >> 8);
  1228. nv_wo32(disp->mem, 0x104c, 0x00000000);
  1229. nv_wo32(disp->mem, 0x1050, 0x00000000);
  1230. nv_wo32(disp->mem, 0x1054, 0x00000000);
  1231. nv_wo32(disp->mem, 0x0010, NvEvoVRAM_LP);
  1232. nv_wo32(disp->mem, 0x0014, (0x1040 << 9) | 0x00000001);
  1233. nv_wo32(disp->mem, 0x1060, 0x0fe00009);
  1234. nv_wo32(disp->mem, 0x1064, 0x00000000);
  1235. nv_wo32(disp->mem, 0x1068, (dev_priv->vram_size - 1) >> 8);
  1236. nv_wo32(disp->mem, 0x106c, 0x00000000);
  1237. nv_wo32(disp->mem, 0x1070, 0x00000000);
  1238. nv_wo32(disp->mem, 0x1074, 0x00000000);
  1239. nv_wo32(disp->mem, 0x0018, NvEvoFB32);
  1240. nv_wo32(disp->mem, 0x001c, (0x1060 << 9) | 0x00000001);
  1241. pinstmem->flush(dev);
  1242. /* push buffers for evo channels */
  1243. disp->evo[0].ptr =
  1244. pci_alloc_consistent(pdev, PAGE_SIZE, &disp->evo[0].handle);
  1245. if (!disp->evo[0].ptr) {
  1246. ret = -ENOMEM;
  1247. goto out;
  1248. }
  1249. ret = nvd0_display_init(dev);
  1250. if (ret)
  1251. goto out;
  1252. out:
  1253. if (ret)
  1254. nvd0_display_destroy(dev);
  1255. return ret;
  1256. }