nv50_display.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. #define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
  27. #include "nv50_display.h"
  28. #include "nouveau_crtc.h"
  29. #include "nouveau_encoder.h"
  30. #include "nouveau_connector.h"
  31. #include "nouveau_fb.h"
  32. #include "nouveau_fbcon.h"
  33. #include "nouveau_ramht.h"
  34. #include "drm_crtc_helper.h"
  35. static void nv50_display_isr(struct drm_device *);
  36. static void nv50_display_bh(unsigned long);
  37. static inline int
  38. nv50_sor_nr(struct drm_device *dev)
  39. {
  40. struct drm_nouveau_private *dev_priv = dev->dev_private;
  41. if (dev_priv->chipset < 0x90 ||
  42. dev_priv->chipset == 0x92 ||
  43. dev_priv->chipset == 0xa0)
  44. return 2;
  45. return 4;
  46. }
  47. u32
  48. nv50_display_active_crtcs(struct drm_device *dev)
  49. {
  50. struct drm_nouveau_private *dev_priv = dev->dev_private;
  51. u32 mask = 0;
  52. int i;
  53. if (dev_priv->chipset < 0x90 ||
  54. dev_priv->chipset == 0x92 ||
  55. dev_priv->chipset == 0xa0) {
  56. for (i = 0; i < 2; i++)
  57. mask |= nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_C(i));
  58. } else {
  59. for (i = 0; i < 4; i++)
  60. mask |= nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));
  61. }
  62. for (i = 0; i < 3; i++)
  63. mask |= nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));
  64. return mask & 3;
  65. }
  66. static int
  67. evo_icmd(struct drm_device *dev, int ch, u32 mthd, u32 data)
  68. {
  69. int ret = 0;
  70. nv_mask(dev, 0x610300 + (ch * 0x08), 0x00000001, 0x00000001);
  71. nv_wr32(dev, 0x610304 + (ch * 0x08), data);
  72. nv_wr32(dev, 0x610300 + (ch * 0x08), 0x80000001 | mthd);
  73. if (!nv_wait(dev, 0x610300 + (ch * 0x08), 0x80000000, 0x00000000))
  74. ret = -EBUSY;
  75. if (ret || (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO))
  76. NV_INFO(dev, "EvoPIO: %d 0x%04x 0x%08x\n", ch, mthd, data);
  77. nv_mask(dev, 0x610300 + (ch * 0x08), 0x00000001, 0x00000000);
  78. return ret;
  79. }
  80. int
  81. nv50_display_early_init(struct drm_device *dev)
  82. {
  83. u32 ctrl = nv_rd32(dev, 0x610200);
  84. int i;
  85. /* check if master evo channel is already active, a good a sign as any
  86. * that the display engine is in a weird state (hibernate/kexec), if
  87. * it is, do our best to reset the display engine...
  88. */
  89. if ((ctrl & 0x00000003) == 0x00000003) {
  90. NV_INFO(dev, "PDISP: EVO(0) 0x%08x, resetting...\n", ctrl);
  91. /* deactivate both heads first, PDISP will disappear forever
  92. * (well, until you power cycle) on some boards as soon as
  93. * PMC_ENABLE is hit unless they are..
  94. */
  95. for (i = 0; i < 2; i++) {
  96. evo_icmd(dev, 0, 0x0880 + (i * 0x400), 0x05000000);
  97. evo_icmd(dev, 0, 0x089c + (i * 0x400), 0);
  98. evo_icmd(dev, 0, 0x0840 + (i * 0x400), 0);
  99. evo_icmd(dev, 0, 0x0844 + (i * 0x400), 0);
  100. evo_icmd(dev, 0, 0x085c + (i * 0x400), 0);
  101. evo_icmd(dev, 0, 0x0874 + (i * 0x400), 0);
  102. }
  103. evo_icmd(dev, 0, 0x0080, 0);
  104. /* reset PDISP */
  105. nv_mask(dev, 0x000200, 0x40000000, 0x00000000);
  106. nv_mask(dev, 0x000200, 0x40000000, 0x40000000);
  107. }
  108. return 0;
  109. }
  110. void
  111. nv50_display_late_takedown(struct drm_device *dev)
  112. {
  113. }
  114. int
  115. nv50_display_sync(struct drm_device *dev)
  116. {
  117. struct drm_nouveau_private *dev_priv = dev->dev_private;
  118. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  119. struct nv50_display *disp = nv50_display(dev);
  120. struct nouveau_channel *evo = disp->master;
  121. u64 start;
  122. int ret;
  123. ret = RING_SPACE(evo, 6);
  124. if (ret == 0) {
  125. BEGIN_NV04(evo, 0, 0x0084, 1);
  126. OUT_RING (evo, 0x80000000);
  127. BEGIN_NV04(evo, 0, 0x0080, 1);
  128. OUT_RING (evo, 0);
  129. BEGIN_NV04(evo, 0, 0x0084, 1);
  130. OUT_RING (evo, 0x00000000);
  131. nv_wo32(disp->ntfy, 0x000, 0x00000000);
  132. FIRE_RING (evo);
  133. start = ptimer->read(dev);
  134. do {
  135. if (nv_ro32(disp->ntfy, 0x000))
  136. return 0;
  137. } while (ptimer->read(dev) - start < 2000000000ULL);
  138. }
  139. return -EBUSY;
  140. }
  141. int
  142. nv50_display_init(struct drm_device *dev)
  143. {
  144. struct nouveau_channel *evo;
  145. int ret, i;
  146. u32 val;
  147. NV_DEBUG_KMS(dev, "\n");
  148. nv_wr32(dev, 0x00610184, nv_rd32(dev, 0x00614004));
  149. /*
  150. * I think the 0x006101XX range is some kind of main control area
  151. * that enables things.
  152. */
  153. /* CRTC? */
  154. for (i = 0; i < 2; i++) {
  155. val = nv_rd32(dev, 0x00616100 + (i * 0x800));
  156. nv_wr32(dev, 0x00610190 + (i * 0x10), val);
  157. val = nv_rd32(dev, 0x00616104 + (i * 0x800));
  158. nv_wr32(dev, 0x00610194 + (i * 0x10), val);
  159. val = nv_rd32(dev, 0x00616108 + (i * 0x800));
  160. nv_wr32(dev, 0x00610198 + (i * 0x10), val);
  161. val = nv_rd32(dev, 0x0061610c + (i * 0x800));
  162. nv_wr32(dev, 0x0061019c + (i * 0x10), val);
  163. }
  164. /* DAC */
  165. for (i = 0; i < 3; i++) {
  166. val = nv_rd32(dev, 0x0061a000 + (i * 0x800));
  167. nv_wr32(dev, 0x006101d0 + (i * 0x04), val);
  168. }
  169. /* SOR */
  170. for (i = 0; i < nv50_sor_nr(dev); i++) {
  171. val = nv_rd32(dev, 0x0061c000 + (i * 0x800));
  172. nv_wr32(dev, 0x006101e0 + (i * 0x04), val);
  173. }
  174. /* EXT */
  175. for (i = 0; i < 3; i++) {
  176. val = nv_rd32(dev, 0x0061e000 + (i * 0x800));
  177. nv_wr32(dev, 0x006101f0 + (i * 0x04), val);
  178. }
  179. for (i = 0; i < 3; i++) {
  180. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(i), 0x00550000 |
  181. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  182. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL1(i), 0x00000001);
  183. }
  184. /* The precise purpose is unknown, i suspect it has something to do
  185. * with text mode.
  186. */
  187. if (nv_rd32(dev, NV50_PDISPLAY_INTR_1) & 0x100) {
  188. nv_wr32(dev, NV50_PDISPLAY_INTR_1, 0x100);
  189. nv_wr32(dev, 0x006194e8, nv_rd32(dev, 0x006194e8) & ~1);
  190. if (!nv_wait(dev, 0x006194e8, 2, 0)) {
  191. NV_ERROR(dev, "timeout: (0x6194e8 & 2) != 0\n");
  192. NV_ERROR(dev, "0x6194e8 = 0x%08x\n",
  193. nv_rd32(dev, 0x6194e8));
  194. return -EBUSY;
  195. }
  196. }
  197. for (i = 0; i < 2; i++) {
  198. nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0x2000);
  199. if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  200. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
  201. NV_ERROR(dev, "timeout: CURSOR_CTRL2_STATUS == 0\n");
  202. NV_ERROR(dev, "CURSOR_CTRL2 = 0x%08x\n",
  203. nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
  204. return -EBUSY;
  205. }
  206. nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  207. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_ON);
  208. if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  209. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS,
  210. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS_ACTIVE)) {
  211. NV_ERROR(dev, "timeout: "
  212. "CURSOR_CTRL2_STATUS_ACTIVE(%d)\n", i);
  213. NV_ERROR(dev, "CURSOR_CTRL2(%d) = 0x%08x\n", i,
  214. nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
  215. return -EBUSY;
  216. }
  217. }
  218. nv_wr32(dev, NV50_PDISPLAY_PIO_CTRL, 0x00000000);
  219. nv_mask(dev, NV50_PDISPLAY_INTR_0, 0x00000000, 0x00000000);
  220. nv_wr32(dev, NV50_PDISPLAY_INTR_EN_0, 0x00000000);
  221. nv_mask(dev, NV50_PDISPLAY_INTR_1, 0x00000000, 0x00000000);
  222. nv_wr32(dev, NV50_PDISPLAY_INTR_EN_1,
  223. NV50_PDISPLAY_INTR_EN_1_CLK_UNK10 |
  224. NV50_PDISPLAY_INTR_EN_1_CLK_UNK20 |
  225. NV50_PDISPLAY_INTR_EN_1_CLK_UNK40);
  226. ret = nv50_evo_init(dev);
  227. if (ret)
  228. return ret;
  229. evo = nv50_display(dev)->master;
  230. nv_wr32(dev, NV50_PDISPLAY_OBJECTS, (evo->ramin->vinst >> 8) | 9);
  231. ret = RING_SPACE(evo, 3);
  232. if (ret)
  233. return ret;
  234. BEGIN_NV04(evo, 0, NV50_EVO_UNK84, 2);
  235. OUT_RING (evo, NV50_EVO_UNK84_NOTIFY_DISABLED);
  236. OUT_RING (evo, NvEvoSync);
  237. return nv50_display_sync(dev);
  238. }
  239. void
  240. nv50_display_fini(struct drm_device *dev)
  241. {
  242. struct nv50_display *disp = nv50_display(dev);
  243. struct nouveau_channel *evo = disp->master;
  244. struct drm_crtc *drm_crtc;
  245. int ret, i;
  246. NV_DEBUG_KMS(dev, "\n");
  247. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  248. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  249. nv50_crtc_blank(crtc, true);
  250. }
  251. ret = RING_SPACE(evo, 2);
  252. if (ret == 0) {
  253. BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
  254. OUT_RING(evo, 0);
  255. }
  256. FIRE_RING(evo);
  257. /* Almost like ack'ing a vblank interrupt, maybe in the spirit of
  258. * cleaning up?
  259. */
  260. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  261. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  262. uint32_t mask = NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(crtc->index);
  263. if (!crtc->base.enabled)
  264. continue;
  265. nv_wr32(dev, NV50_PDISPLAY_INTR_1, mask);
  266. if (!nv_wait(dev, NV50_PDISPLAY_INTR_1, mask, mask)) {
  267. NV_ERROR(dev, "timeout: (0x610024 & 0x%08x) == "
  268. "0x%08x\n", mask, mask);
  269. NV_ERROR(dev, "0x610024 = 0x%08x\n",
  270. nv_rd32(dev, NV50_PDISPLAY_INTR_1));
  271. }
  272. }
  273. for (i = 0; i < 2; i++) {
  274. nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0);
  275. if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  276. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
  277. NV_ERROR(dev, "timeout: CURSOR_CTRL2_STATUS == 0\n");
  278. NV_ERROR(dev, "CURSOR_CTRL2 = 0x%08x\n",
  279. nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
  280. }
  281. }
  282. nv50_evo_fini(dev);
  283. for (i = 0; i < 3; i++) {
  284. if (!nv_wait(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i),
  285. NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
  286. NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", i);
  287. NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", i,
  288. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i)));
  289. }
  290. }
  291. /* disable interrupts. */
  292. nv_wr32(dev, NV50_PDISPLAY_INTR_EN_1, 0x00000000);
  293. }
  294. int
  295. nv50_display_create(struct drm_device *dev)
  296. {
  297. struct drm_nouveau_private *dev_priv = dev->dev_private;
  298. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  299. struct drm_connector *connector, *ct;
  300. struct nv50_display *priv;
  301. int ret, i;
  302. NV_DEBUG_KMS(dev, "\n");
  303. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  304. if (!priv)
  305. return -ENOMEM;
  306. dev_priv->engine.display.priv = priv;
  307. /* Create CRTC objects */
  308. for (i = 0; i < 2; i++) {
  309. ret = nv50_crtc_create(dev, i);
  310. if (ret)
  311. return ret;
  312. }
  313. /* We setup the encoders from the BIOS table */
  314. for (i = 0 ; i < dcb->entries; i++) {
  315. struct dcb_entry *entry = &dcb->entry[i];
  316. if (entry->location != DCB_LOC_ON_CHIP) {
  317. NV_WARN(dev, "Off-chip encoder %d/%d unsupported\n",
  318. entry->type, ffs(entry->or) - 1);
  319. continue;
  320. }
  321. connector = nouveau_connector_create(dev, entry->connector);
  322. if (IS_ERR(connector))
  323. continue;
  324. switch (entry->type) {
  325. case OUTPUT_TMDS:
  326. case OUTPUT_LVDS:
  327. case OUTPUT_DP:
  328. nv50_sor_create(connector, entry);
  329. break;
  330. case OUTPUT_ANALOG:
  331. nv50_dac_create(connector, entry);
  332. break;
  333. default:
  334. NV_WARN(dev, "DCB encoder %d unknown\n", entry->type);
  335. continue;
  336. }
  337. }
  338. list_for_each_entry_safe(connector, ct,
  339. &dev->mode_config.connector_list, head) {
  340. if (!connector->encoder_ids[0]) {
  341. NV_WARN(dev, "%s has no encoders, removing\n",
  342. drm_get_connector_name(connector));
  343. connector->funcs->destroy(connector);
  344. }
  345. }
  346. tasklet_init(&priv->tasklet, nv50_display_bh, (unsigned long)dev);
  347. nouveau_irq_register(dev, 26, nv50_display_isr);
  348. ret = nv50_evo_create(dev);
  349. if (ret) {
  350. nv50_display_destroy(dev);
  351. return ret;
  352. }
  353. return 0;
  354. }
  355. void
  356. nv50_display_destroy(struct drm_device *dev)
  357. {
  358. struct nv50_display *disp = nv50_display(dev);
  359. NV_DEBUG_KMS(dev, "\n");
  360. nv50_evo_destroy(dev);
  361. nouveau_irq_unregister(dev, 26);
  362. kfree(disp);
  363. }
  364. void
  365. nv50_display_flip_stop(struct drm_crtc *crtc)
  366. {
  367. struct nv50_display *disp = nv50_display(crtc->dev);
  368. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  369. struct nv50_display_crtc *dispc = &disp->crtc[nv_crtc->index];
  370. struct nouveau_channel *evo = dispc->sync;
  371. int ret;
  372. ret = RING_SPACE(evo, 8);
  373. if (ret) {
  374. WARN_ON(1);
  375. return;
  376. }
  377. BEGIN_NV04(evo, 0, 0x0084, 1);
  378. OUT_RING (evo, 0x00000000);
  379. BEGIN_NV04(evo, 0, 0x0094, 1);
  380. OUT_RING (evo, 0x00000000);
  381. BEGIN_NV04(evo, 0, 0x00c0, 1);
  382. OUT_RING (evo, 0x00000000);
  383. BEGIN_NV04(evo, 0, 0x0080, 1);
  384. OUT_RING (evo, 0x00000000);
  385. FIRE_RING (evo);
  386. }
  387. int
  388. nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  389. struct nouveau_channel *chan)
  390. {
  391. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  392. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  393. struct nv50_display *disp = nv50_display(crtc->dev);
  394. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  395. struct nv50_display_crtc *dispc = &disp->crtc[nv_crtc->index];
  396. struct nouveau_channel *evo = dispc->sync;
  397. int ret;
  398. ret = RING_SPACE(evo, chan ? 25 : 27);
  399. if (unlikely(ret))
  400. return ret;
  401. /* synchronise with the rendering channel, if necessary */
  402. if (likely(chan)) {
  403. ret = RING_SPACE(chan, 10);
  404. if (ret) {
  405. WIND_RING(evo);
  406. return ret;
  407. }
  408. if (dev_priv->chipset < 0xc0) {
  409. BEGIN_NV04(chan, 0, 0x0060, 2);
  410. OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
  411. OUT_RING (chan, dispc->sem.offset);
  412. BEGIN_NV04(chan, 0, 0x006c, 1);
  413. OUT_RING (chan, 0xf00d0000 | dispc->sem.value);
  414. BEGIN_NV04(chan, 0, 0x0064, 2);
  415. OUT_RING (chan, dispc->sem.offset ^ 0x10);
  416. OUT_RING (chan, 0x74b1e000);
  417. BEGIN_NV04(chan, 0, 0x0060, 1);
  418. if (dev_priv->chipset < 0x84)
  419. OUT_RING (chan, NvSema);
  420. else
  421. OUT_RING (chan, chan->vram_handle);
  422. } else {
  423. u64 offset = chan->dispc_vma[nv_crtc->index].offset;
  424. offset += dispc->sem.offset;
  425. BEGIN_NVC0(chan, 0, 0x0010, 4);
  426. OUT_RING (chan, upper_32_bits(offset));
  427. OUT_RING (chan, lower_32_bits(offset));
  428. OUT_RING (chan, 0xf00d0000 | dispc->sem.value);
  429. OUT_RING (chan, 0x1002);
  430. BEGIN_NVC0(chan, 0, 0x0010, 4);
  431. OUT_RING (chan, upper_32_bits(offset));
  432. OUT_RING (chan, lower_32_bits(offset ^ 0x10));
  433. OUT_RING (chan, 0x74b1e000);
  434. OUT_RING (chan, 0x1001);
  435. }
  436. FIRE_RING (chan);
  437. } else {
  438. nouveau_bo_wr32(dispc->sem.bo, dispc->sem.offset / 4,
  439. 0xf00d0000 | dispc->sem.value);
  440. }
  441. /* queue the flip on the crtc's "display sync" channel */
  442. BEGIN_NV04(evo, 0, 0x0100, 1);
  443. OUT_RING (evo, 0xfffe0000);
  444. if (chan) {
  445. BEGIN_NV04(evo, 0, 0x0084, 1);
  446. OUT_RING (evo, 0x00000100);
  447. } else {
  448. BEGIN_NV04(evo, 0, 0x0084, 1);
  449. OUT_RING (evo, 0x00000010);
  450. /* allows gamma somehow, PDISP will bitch at you if
  451. * you don't wait for vblank before changing this..
  452. */
  453. BEGIN_NV04(evo, 0, 0x00e0, 1);
  454. OUT_RING (evo, 0x40000000);
  455. }
  456. BEGIN_NV04(evo, 0, 0x0088, 4);
  457. OUT_RING (evo, dispc->sem.offset);
  458. OUT_RING (evo, 0xf00d0000 | dispc->sem.value);
  459. OUT_RING (evo, 0x74b1e000);
  460. OUT_RING (evo, NvEvoSync);
  461. BEGIN_NV04(evo, 0, 0x00a0, 2);
  462. OUT_RING (evo, 0x00000000);
  463. OUT_RING (evo, 0x00000000);
  464. BEGIN_NV04(evo, 0, 0x00c0, 1);
  465. OUT_RING (evo, nv_fb->r_dma);
  466. BEGIN_NV04(evo, 0, 0x0110, 2);
  467. OUT_RING (evo, 0x00000000);
  468. OUT_RING (evo, 0x00000000);
  469. BEGIN_NV04(evo, 0, 0x0800, 5);
  470. OUT_RING (evo, nv_fb->nvbo->bo.offset >> 8);
  471. OUT_RING (evo, 0);
  472. OUT_RING (evo, (fb->height << 16) | fb->width);
  473. OUT_RING (evo, nv_fb->r_pitch);
  474. OUT_RING (evo, nv_fb->r_format);
  475. BEGIN_NV04(evo, 0, 0x0080, 1);
  476. OUT_RING (evo, 0x00000000);
  477. FIRE_RING (evo);
  478. dispc->sem.offset ^= 0x10;
  479. dispc->sem.value++;
  480. return 0;
  481. }
  482. static u16
  483. nv50_display_script_select(struct drm_device *dev, struct dcb_entry *dcb,
  484. u32 mc, int pxclk)
  485. {
  486. struct drm_nouveau_private *dev_priv = dev->dev_private;
  487. struct nouveau_connector *nv_connector = NULL;
  488. struct drm_encoder *encoder;
  489. struct nvbios *bios = &dev_priv->vbios;
  490. u32 script = 0, or;
  491. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  492. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  493. if (nv_encoder->dcb != dcb)
  494. continue;
  495. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  496. break;
  497. }
  498. or = ffs(dcb->or) - 1;
  499. switch (dcb->type) {
  500. case OUTPUT_LVDS:
  501. script = (mc >> 8) & 0xf;
  502. if (bios->fp_no_ddc) {
  503. if (bios->fp.dual_link)
  504. script |= 0x0100;
  505. if (bios->fp.if_is_24bit)
  506. script |= 0x0200;
  507. } else {
  508. /* determine number of lvds links */
  509. if (nv_connector && nv_connector->edid &&
  510. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  511. /* http://www.spwg.org */
  512. if (((u8 *)nv_connector->edid)[121] == 2)
  513. script |= 0x0100;
  514. } else
  515. if (pxclk >= bios->fp.duallink_transition_clk) {
  516. script |= 0x0100;
  517. }
  518. /* determine panel depth */
  519. if (script & 0x0100) {
  520. if (bios->fp.strapless_is_24bit & 2)
  521. script |= 0x0200;
  522. } else {
  523. if (bios->fp.strapless_is_24bit & 1)
  524. script |= 0x0200;
  525. }
  526. if (nv_connector && nv_connector->edid &&
  527. (nv_connector->edid->revision >= 4) &&
  528. (nv_connector->edid->input & 0x70) >= 0x20)
  529. script |= 0x0200;
  530. }
  531. if (nouveau_uscript_lvds >= 0) {
  532. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  533. "for output LVDS-%d\n", script,
  534. nouveau_uscript_lvds, or);
  535. script = nouveau_uscript_lvds;
  536. }
  537. break;
  538. case OUTPUT_TMDS:
  539. script = (mc >> 8) & 0xf;
  540. if (pxclk >= 165000)
  541. script |= 0x0100;
  542. if (nouveau_uscript_tmds >= 0) {
  543. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  544. "for output TMDS-%d\n", script,
  545. nouveau_uscript_tmds, or);
  546. script = nouveau_uscript_tmds;
  547. }
  548. break;
  549. case OUTPUT_DP:
  550. script = (mc >> 8) & 0xf;
  551. break;
  552. case OUTPUT_ANALOG:
  553. script = 0xff;
  554. break;
  555. default:
  556. NV_ERROR(dev, "modeset on unsupported output type!\n");
  557. break;
  558. }
  559. return script;
  560. }
  561. static void
  562. nv50_display_vblank_crtc_handler(struct drm_device *dev, int crtc)
  563. {
  564. struct drm_nouveau_private *dev_priv = dev->dev_private;
  565. struct nouveau_channel *chan, *tmp;
  566. list_for_each_entry_safe(chan, tmp, &dev_priv->vbl_waiting,
  567. nvsw.vbl_wait) {
  568. if (chan->nvsw.vblsem_head != crtc)
  569. continue;
  570. nouveau_bo_wr32(chan->notifier_bo, chan->nvsw.vblsem_offset,
  571. chan->nvsw.vblsem_rval);
  572. list_del(&chan->nvsw.vbl_wait);
  573. drm_vblank_put(dev, crtc);
  574. }
  575. drm_handle_vblank(dev, crtc);
  576. }
  577. static void
  578. nv50_display_vblank_handler(struct drm_device *dev, uint32_t intr)
  579. {
  580. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_0)
  581. nv50_display_vblank_crtc_handler(dev, 0);
  582. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_1)
  583. nv50_display_vblank_crtc_handler(dev, 1);
  584. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_VBLANK_CRTC);
  585. }
  586. static void
  587. nv50_display_unk10_handler(struct drm_device *dev)
  588. {
  589. struct drm_nouveau_private *dev_priv = dev->dev_private;
  590. struct nv50_display *disp = nv50_display(dev);
  591. u32 unk30 = nv_rd32(dev, 0x610030), mc;
  592. int i, crtc, or = 0, type = OUTPUT_ANY;
  593. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  594. disp->irq.dcb = NULL;
  595. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) & ~8);
  596. /* Determine which CRTC we're dealing with, only 1 ever will be
  597. * signalled at the same time with the current nouveau code.
  598. */
  599. crtc = ffs((unk30 & 0x00000060) >> 5) - 1;
  600. if (crtc < 0)
  601. goto ack;
  602. /* Nothing needs to be done for the encoder */
  603. crtc = ffs((unk30 & 0x00000180) >> 7) - 1;
  604. if (crtc < 0)
  605. goto ack;
  606. /* Find which encoder was connected to the CRTC */
  607. for (i = 0; type == OUTPUT_ANY && i < 3; i++) {
  608. mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));
  609. NV_DEBUG_KMS(dev, "DAC-%d mc: 0x%08x\n", i, mc);
  610. if (!(mc & (1 << crtc)))
  611. continue;
  612. switch ((mc & 0x00000f00) >> 8) {
  613. case 0: type = OUTPUT_ANALOG; break;
  614. case 1: type = OUTPUT_TV; break;
  615. default:
  616. NV_ERROR(dev, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
  617. goto ack;
  618. }
  619. or = i;
  620. }
  621. for (i = 0; type == OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
  622. if (dev_priv->chipset < 0x90 ||
  623. dev_priv->chipset == 0x92 ||
  624. dev_priv->chipset == 0xa0)
  625. mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_C(i));
  626. else
  627. mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));
  628. NV_DEBUG_KMS(dev, "SOR-%d mc: 0x%08x\n", i, mc);
  629. if (!(mc & (1 << crtc)))
  630. continue;
  631. switch ((mc & 0x00000f00) >> 8) {
  632. case 0: type = OUTPUT_LVDS; break;
  633. case 1: type = OUTPUT_TMDS; break;
  634. case 2: type = OUTPUT_TMDS; break;
  635. case 5: type = OUTPUT_TMDS; break;
  636. case 8: type = OUTPUT_DP; break;
  637. case 9: type = OUTPUT_DP; break;
  638. default:
  639. NV_ERROR(dev, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
  640. goto ack;
  641. }
  642. or = i;
  643. }
  644. /* There was no encoder to disable */
  645. if (type == OUTPUT_ANY)
  646. goto ack;
  647. /* Disable the encoder */
  648. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  649. struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
  650. if (dcb->type == type && (dcb->or & (1 << or))) {
  651. nouveau_bios_run_display_table(dev, 0, -1, dcb, -1);
  652. disp->irq.dcb = dcb;
  653. goto ack;
  654. }
  655. }
  656. NV_ERROR(dev, "no dcb for %d %d 0x%08x\n", or, type, mc);
  657. ack:
  658. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
  659. nv_wr32(dev, 0x610030, 0x80000000);
  660. }
  661. static void
  662. nv50_display_unk20_handler(struct drm_device *dev)
  663. {
  664. struct drm_nouveau_private *dev_priv = dev->dev_private;
  665. struct nv50_display *disp = nv50_display(dev);
  666. u32 unk30 = nv_rd32(dev, 0x610030), tmp, pclk, script, mc = 0;
  667. struct dcb_entry *dcb;
  668. int i, crtc, or = 0, type = OUTPUT_ANY;
  669. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  670. dcb = disp->irq.dcb;
  671. if (dcb) {
  672. nouveau_bios_run_display_table(dev, 0, -2, dcb, -1);
  673. disp->irq.dcb = NULL;
  674. }
  675. /* CRTC clock change requested? */
  676. crtc = ffs((unk30 & 0x00000600) >> 9) - 1;
  677. if (crtc >= 0) {
  678. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(crtc, CLOCK));
  679. pclk &= 0x003fffff;
  680. if (pclk)
  681. nv50_crtc_set_clock(dev, crtc, pclk);
  682. tmp = nv_rd32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc));
  683. tmp &= ~0x000000f;
  684. nv_wr32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc), tmp);
  685. }
  686. /* Nothing needs to be done for the encoder */
  687. crtc = ffs((unk30 & 0x00000180) >> 7) - 1;
  688. if (crtc < 0)
  689. goto ack;
  690. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(crtc, CLOCK)) & 0x003fffff;
  691. /* Find which encoder is connected to the CRTC */
  692. for (i = 0; type == OUTPUT_ANY && i < 3; i++) {
  693. mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_P(i));
  694. NV_DEBUG_KMS(dev, "DAC-%d mc: 0x%08x\n", i, mc);
  695. if (!(mc & (1 << crtc)))
  696. continue;
  697. switch ((mc & 0x00000f00) >> 8) {
  698. case 0: type = OUTPUT_ANALOG; break;
  699. case 1: type = OUTPUT_TV; break;
  700. default:
  701. NV_ERROR(dev, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
  702. goto ack;
  703. }
  704. or = i;
  705. }
  706. for (i = 0; type == OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
  707. if (dev_priv->chipset < 0x90 ||
  708. dev_priv->chipset == 0x92 ||
  709. dev_priv->chipset == 0xa0)
  710. mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_P(i));
  711. else
  712. mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_P(i));
  713. NV_DEBUG_KMS(dev, "SOR-%d mc: 0x%08x\n", i, mc);
  714. if (!(mc & (1 << crtc)))
  715. continue;
  716. switch ((mc & 0x00000f00) >> 8) {
  717. case 0: type = OUTPUT_LVDS; break;
  718. case 1: type = OUTPUT_TMDS; break;
  719. case 2: type = OUTPUT_TMDS; break;
  720. case 5: type = OUTPUT_TMDS; break;
  721. case 8: type = OUTPUT_DP; break;
  722. case 9: type = OUTPUT_DP; break;
  723. default:
  724. NV_ERROR(dev, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
  725. goto ack;
  726. }
  727. or = i;
  728. }
  729. if (type == OUTPUT_ANY)
  730. goto ack;
  731. /* Enable the encoder */
  732. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  733. dcb = &dev_priv->vbios.dcb.entry[i];
  734. if (dcb->type == type && (dcb->or & (1 << or)))
  735. break;
  736. }
  737. if (i == dev_priv->vbios.dcb.entries) {
  738. NV_ERROR(dev, "no dcb for %d %d 0x%08x\n", or, type, mc);
  739. goto ack;
  740. }
  741. script = nv50_display_script_select(dev, dcb, mc, pclk);
  742. nouveau_bios_run_display_table(dev, script, pclk, dcb, -1);
  743. if (type == OUTPUT_DP) {
  744. int link = !(dcb->dpconf.sor.link & 1);
  745. if ((mc & 0x000f0000) == 0x00020000)
  746. nv50_sor_dp_calc_tu(dev, or, link, pclk, 18);
  747. else
  748. nv50_sor_dp_calc_tu(dev, or, link, pclk, 24);
  749. }
  750. if (dcb->type != OUTPUT_ANALOG) {
  751. tmp = nv_rd32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
  752. tmp &= ~0x00000f0f;
  753. if (script & 0x0100)
  754. tmp |= 0x00000101;
  755. nv_wr32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
  756. } else {
  757. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
  758. }
  759. disp->irq.dcb = dcb;
  760. disp->irq.pclk = pclk;
  761. disp->irq.script = script;
  762. ack:
  763. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
  764. nv_wr32(dev, 0x610030, 0x80000000);
  765. }
  766. /* If programming a TMDS output on a SOR that can also be configured for
  767. * DisplayPort, make sure NV50_SOR_DP_CTRL_ENABLE is forced off.
  768. *
  769. * It looks like the VBIOS TMDS scripts make an attempt at this, however,
  770. * the VBIOS scripts on at least one board I have only switch it off on
  771. * link 0, causing a blank display if the output has previously been
  772. * programmed for DisplayPort.
  773. */
  774. static void
  775. nv50_display_unk40_dp_set_tmds(struct drm_device *dev, struct dcb_entry *dcb)
  776. {
  777. int or = ffs(dcb->or) - 1, link = !(dcb->dpconf.sor.link & 1);
  778. struct drm_encoder *encoder;
  779. u32 tmp;
  780. if (dcb->type != OUTPUT_TMDS)
  781. return;
  782. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  783. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  784. if (nv_encoder->dcb->type == OUTPUT_DP &&
  785. nv_encoder->dcb->or & (1 << or)) {
  786. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  787. tmp &= ~NV50_SOR_DP_CTRL_ENABLED;
  788. nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
  789. break;
  790. }
  791. }
  792. }
  793. static void
  794. nv50_display_unk40_handler(struct drm_device *dev)
  795. {
  796. struct nv50_display *disp = nv50_display(dev);
  797. struct dcb_entry *dcb = disp->irq.dcb;
  798. u16 script = disp->irq.script;
  799. u32 unk30 = nv_rd32(dev, 0x610030), pclk = disp->irq.pclk;
  800. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  801. disp->irq.dcb = NULL;
  802. if (!dcb)
  803. goto ack;
  804. nouveau_bios_run_display_table(dev, script, -pclk, dcb, -1);
  805. nv50_display_unk40_dp_set_tmds(dev, dcb);
  806. ack:
  807. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK40);
  808. nv_wr32(dev, 0x610030, 0x80000000);
  809. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) | 8);
  810. }
  811. static void
  812. nv50_display_bh(unsigned long data)
  813. {
  814. struct drm_device *dev = (struct drm_device *)data;
  815. for (;;) {
  816. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  817. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  818. NV_DEBUG_KMS(dev, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
  819. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK10)
  820. nv50_display_unk10_handler(dev);
  821. else
  822. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK20)
  823. nv50_display_unk20_handler(dev);
  824. else
  825. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK40)
  826. nv50_display_unk40_handler(dev);
  827. else
  828. break;
  829. }
  830. nv_wr32(dev, NV03_PMC_INTR_EN_0, 1);
  831. }
  832. static void
  833. nv50_display_error_handler(struct drm_device *dev)
  834. {
  835. u32 channels = (nv_rd32(dev, NV50_PDISPLAY_INTR_0) & 0x001f0000) >> 16;
  836. u32 addr, data;
  837. int chid;
  838. for (chid = 0; chid < 5; chid++) {
  839. if (!(channels & (1 << chid)))
  840. continue;
  841. nv_wr32(dev, NV50_PDISPLAY_INTR_0, 0x00010000 << chid);
  842. addr = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_ADDR(chid));
  843. data = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_DATA(chid));
  844. NV_ERROR(dev, "EvoCh %d Mthd 0x%04x Data 0x%08x "
  845. "(0x%04x 0x%02x)\n", chid,
  846. addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
  847. nv_wr32(dev, NV50_PDISPLAY_TRAPPED_ADDR(chid), 0x90000000);
  848. }
  849. }
  850. static void
  851. nv50_display_isr(struct drm_device *dev)
  852. {
  853. struct nv50_display *disp = nv50_display(dev);
  854. uint32_t delayed = 0;
  855. while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_DISPLAY) {
  856. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  857. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  858. uint32_t clock;
  859. NV_DEBUG_KMS(dev, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
  860. if (!intr0 && !(intr1 & ~delayed))
  861. break;
  862. if (intr0 & 0x001f0000) {
  863. nv50_display_error_handler(dev);
  864. intr0 &= ~0x001f0000;
  865. }
  866. if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
  867. nv50_display_vblank_handler(dev, intr1);
  868. intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  869. }
  870. clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
  871. NV50_PDISPLAY_INTR_1_CLK_UNK20 |
  872. NV50_PDISPLAY_INTR_1_CLK_UNK40));
  873. if (clock) {
  874. nv_wr32(dev, NV03_PMC_INTR_EN_0, 0);
  875. tasklet_schedule(&disp->tasklet);
  876. delayed |= clock;
  877. intr1 &= ~clock;
  878. }
  879. if (intr0) {
  880. NV_ERROR(dev, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
  881. nv_wr32(dev, NV50_PDISPLAY_INTR_0, intr0);
  882. }
  883. if (intr1) {
  884. NV_ERROR(dev,
  885. "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
  886. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr1);
  887. }
  888. }
  889. }